Skip to content

Comments

feat: add math/base/special/truncbf#8915

Open
Amansingh0807 wants to merge 2 commits intostdlib-js:developfrom
Amansingh0807:feat-math-base-special-truncbf
Open

feat: add math/base/special/truncbf#8915
Amansingh0807 wants to merge 2 commits intostdlib-js:developfrom
Amansingh0807:feat-math-base-special-truncbf

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

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

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

@Amansingh0807 Amansingh0807 force-pushed the feat-math-base-special-truncbf branch from 4ed17b6 to ab5e9a8 Compare December 14, 2025 04:30
@Amansingh0807
Copy link
Contributor Author

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.

Implement single-precision floating-point truncation function that rounds
toward zero to n digits in base b.
Features:
- C implementation with native addon support
- Single-precision operations using powf and truncf
- Overflow handling when scale factor becomes infinite
- Comprehensive tests (23 test cases)
- Benchmarks for C, native, Julia, and JavaScript
- TypeScript declarations and documentation
- Support for arbitrary base b
- Proper handling of edge cases (NaN, ±0, ±Infinity, base=0)
Addresses maintainer feedback:
- Uses single-precision packages (powf, truncf)
- Uses NAPI macro STDLIB_MATH_BASE_NAPI_MODULE_FII_F
- Proper manifest.json structure (only main.c in src)
Closes stdlib-js#649
@Amansingh0807 Amansingh0807 force-pushed the feat-math-base-special-truncbf branch from ab5e9a8 to 539f58a Compare December 14, 2025 05:08
@Amansingh0807 Amansingh0807 requested a review from kgryte January 28, 2026 14:21
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