Skip to content

Conversation

@Amansingh0807
Copy link
Contributor

type: pre_commit_static_analysis_report
description: Results of running static analysis checks when committing changes. report:

  • task: lint_filenames status: passed
  • task: lint_editorconfig status: passed
  • task: lint_markdown status: passed
  • task: lint_package_json status: passed
  • task: lint_repl_help status: passed
  • task: lint_javascript_src status: passed
  • task: lint_javascript_cli status: na
  • task: lint_javascript_examples status: passed
  • task: lint_javascript_tests status: passed
  • task: lint_javascript_benchmarks status: passed
  • task: lint_python status: na
  • task: lint_r status: na
  • task: lint_c_src status: passed
  • task: lint_c_examples status: passed
  • task: lint_c_benchmarks status: passed
  • task: lint_c_tests_fixtures status: na
  • task: lint_shell status: na
  • task: lint_typescript_declarations status: passed
  • task: lint_typescript_tests status: passed
  • task: lint_license_headers status: passed ---

Resolves a part of #649

Description

What is the purpose of this pull request?

This pull request:

  • This PR adds a C implementation for @stdlib/math/base/special/truncbf.

The package computes the value of a single-precision floating-point number rounded toward zero to n digits in an arbitrary base b.

Implementation Details

Function Signature

float stdlib_base_truncbf( const float x, const int32_t n, const int32_t b );
function truncbf( x, n, b )

Algorithm

The implementation uses the following formula:

truncbf(x, n, b) = trunc(x × b^n) / b^n

It leverages the recently added C implementations of powf (for scaling) and truncf (for truncation) to ensure single-precision accuracy.

Examples

- truncbf( 3.14159f, 2, 10 ); // returns 3.14f
- truncbf( 15.75f, 2, 2 );    // returns 15.75f (Binary base)
- truncbf( 255.99f, 1, 16 );  // returns 255.9375f (Hex base)

Related Issues

Does this pull request have any related issues?

This pull request has the following related issues:

Questions

Any questions for reviewers of this pull request?

No.

Other

Any other information relevant to this pull request? This may include screenshots, references, and/or implementation notes.

No.

Checklist

Please ensure the following tasks are completed before submitting this pull request.

AI Assistance

When authoring the changes proposed in this PR, did you use any kind of AI assistance?

  • Yes
  • No

If you answered "yes" above, how did you use AI assistance?

  • Code generation (e.g., when writing an implementation or fixing a bug)
  • Test/benchmark generation
  • Documentation (including examples)
  • Research and understanding

Disclosure

If you answered "yes" to using AI assistance, please provide a short disclosure indicating how you used AI assistance. This helps reviewers determine how much scrutiny to apply when reviewing your contribution. Example disclosures: "This PR was written primarily by Claude Code." or "I consulted ChatGPT to understand the codebase, but the proposed changes were fully authored manually by myself.".


@stdlib-js/reviewers

- Add C implementation for truncbf (single-precision floating-point rounding)
- Add JavaScript wrapper with native addon support
- Add comprehensive tests (21 test cases, all passing)
- Add benchmarks for C, native, Julia, and JavaScript
- Add TypeScript declarations and documentation
- Add examples for both C and JavaScript
- Implement using stdlib's own pow/trunc functions (no math.h dependency)
- Support rounding to n digits in arbitrary base b
- Handle all edge cases (NaN, ±0, ±Infinity, base=0)
Closes stdlib-js#649
- Replace pow with powf in JavaScript and C implementations
- Replace trunc with truncf in JavaScript and C implementations
- Update variable types from double to float in C code
- Update manifest.json dependencies to powf and truncf
- Ensures proper single-precision floating-point operations throughout
- Add runtime dependencies (powf, truncf, is-nanf, is-infinitef, float64-to-float32)
- Add devDependencies for testing (tape, istanbul, tap-min, constants, abs)
- Add detailed parameter specifications with example values (20 per param)
- Add domain definitions and random value generators
- Add proper schema version (math/[email protected])
- Include base_alias (truncb) and alias (truncbf) fields
- Add test configuration (hermitian: false, symmetric: false)
- Complete metadata for automated tooling and documentation
@stdlib-bot stdlib-bot added Math Issue or pull request specific to math functionality. Needs Review A pull request which needs code review. labels Dec 9, 2025
- Revert C code to use pow/trunc (powf/truncf headers not available in build)
- Use double precision internally, cast final result to float32
- Fix TypeScript doc examples to show actual float32 precision values
- Update manifest.json dependencies back to pow/trunc
- Add Apache-2.0 license headers to binding.gyp and include.gypi
Fixes:
- Compilation errors (missing powf.h, truncf.h)
- TypeScript lint errors (incorrect expected values)
- License header lint errors
…t values for float32 precision, remove duplicate keyword
@kgryte kgryte added the Feature Issue or pull request for adding a new feature. label Dec 9, 2025
* limitations under the License.
*/

#include "stdlib/math/base/special/truncbf.h"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines 20 to 21
#include "stdlib/math/base/special/pow.h"
#include "stdlib/math/base/special/trunc.h"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to use single-precision packages.

"task": "build",
"wasm": false,
"src": [
"./src/main.c",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to study other packages to see what gets counted as source and what doesn't.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah Sure, i will do it.

@stdlib-bot
Copy link
Contributor

stdlib-bot commented Dec 9, 2025

Coverage Report

Package Statements Branches Functions Lines
math/base/special/truncbf $\color{red}197/199$
$\color{green}+0.00%$
$\color{red}11/12$
$\color{green}+0.00%$
$\color{green}2/2$
$\color{green}+0.00%$
$\color{red}197/199$
$\color{green}+0.00%$

The above coverage report was generated for the changes in this PR.

@Amansingh0807
Copy link
Contributor Author

@kgryte I have addressed all your feedback:

  1. Macro Usage: Switched addon.c to use the @stdlib/math/base/napi/ternary macro (and updated dependencies in package.json).
  2. Single-Precision: Updated main.c to use single-precision functions (powf, truncf) and headers.
  3. Manifest: Cleaned up the src array in manifest.json to only include main.c.

Additionally, I fixed the benchmarks to use @stdlib/math/base/special/trunc and resolved all linting/build errors. All checks are passing now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Feature Issue or pull request for adding a new feature. Math Issue or pull request specific to math functionality. Needs Review A pull request which needs code review.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants