feat: add `math/base/special/truncbf`
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.
- [x] Read, understood, and followed the contributing guidelines.
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
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.
@kgryte I have addressed all your feedback:
- Macro Usage: Switched
addon.cto use the@stdlib/math/base/napi/ternarymacro (and updated dependencies inpackage.json). - Single-Precision: Updated
main.cto use single-precision functions (powf,truncf) and headers. - Manifest: Cleaned up the
srcarray inmanifest.jsonto only includemain.c.
Additionally, I fixed the benchmarks to use @stdlib/math/base/special/trunc and resolved all linting/build errors. All checks are passing now.
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.