stdlib icon indicating copy to clipboard operation
stdlib copied to clipboard

feat: add `math/base/special/truncbf`

Open Amansingh0807 opened this issue 2 weeks ago • 2 comments

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:

  • Resolves a part of #649

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
  • [x] 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

Amansingh0807 avatar Dec 09 '25 10:12 Amansingh0807

Coverage Report

Package Statements Branches Functions Lines
math/base/special/truncbf $\color{red}202/204$
$\color{green}+0.00%$
$\color{red}13/14$
$\color{green}+0.00%$
$\color{green}2/2$
$\color{green}+0.00%$
$\color{red}202/204$
$\color{green}+0.00%$

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

stdlib-bot avatar Dec 09 '25 11:12 stdlib-bot

@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.

Amansingh0807 avatar Dec 09 '25 14:12 Amansingh0807

Hi @kgryte Just a quick update: I have squashed the commit history into a single clean commit to facilitate the review.

In addition to the requested fixes (macros, single-precision types), I also performed a strict self-audit and added:

Overflow Protection: Handling cases where the scale factor b^n overflows (returning x to preserve the value).

JS Parity: Enforced strict float32 emulation in the JavaScript implementation to match C behavior exactly.

Amansingh0807 avatar Dec 14 '25 04:12 Amansingh0807