Skip to content

Comments

feat: Add C implementation math/base/special/log10f#10277

Open
InFiNiTy0639 wants to merge 2 commits intostdlib-js:developfrom
InFiNiTy0639:feat/add-log10f
Open

feat: Add C implementation math/base/special/log10f#10277
InFiNiTy0639 wants to merge 2 commits intostdlib-js:developfrom
InFiNiTy0639:feat/add-log10f

Conversation

@InFiNiTy0639
Copy link

@InFiNiTy0639 InFiNiTy0639 commented Feb 14, 2026

Progresses #649.

Description

What is the purpose of this pull request?

This pull request:

Related Issues

Does this pull request have any related issues?

This Pull request:

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.

Open to reviews!!

Passed Tests Screenshots

Screenshot 2026-02-14 164717

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

Research and understanding


@stdlib-js/reviewers

@stdlib-bot stdlib-bot added Math Issue or pull request specific to math functionality. First-time Contributor A pull request from a contributor who has never previously committed to the project repository. Needs Review A pull request which needs code review. labels Feb 14, 2026
@InFiNiTy0639
Copy link
Author

InFiNiTy0639 commented Feb 14, 2026

@kgryte @Planeshifter Please Review this PR thanks

@stdlib-bot
Copy link
Contributor

⚠️ Tracking Issue Closure Warning ⚠️

I noticed your PR description contains closing keywords ("Resolves", "Closes", or "Fixes") referencing a "Tracking Issue".

Why this matters:
Tracking issues should typically remain open until all related sub-issues are completed. GitHub automatically closes issues with such closing keywords when the PR is merged. For more information, see GitHub's documentation on using keywords in issues and pull requests.

Required action:
Use "Progresses" instead to reference the tracking issue without automatically closing it.

Thank you for your contribution to the project!

@stdlib-bot stdlib-bot added the Potential Duplicate There might be another pull request resolving the same issue. label Feb 15, 2026
@InFiNiTy0639 InFiNiTy0639 changed the title feat: Add math/base/special/log10f feat: Add math/base/special/log10f Feb 15, 2026
@InFiNiTy0639
Copy link
Author

⚠️ Tracking Issue Closure Warning ⚠️

I noticed your PR description contains closing keywords ("Resolves", "Closes", or "Fixes") referencing a "Tracking Issue".

Why this matters: Tracking issues should typically remain open until all related sub-issues are completed. GitHub automatically closes issues with such closing keywords when the PR is merged. For more information, see GitHub's documentation on using keywords in issues and pull requests.

Required action: Use "Progresses" instead to reference the tracking issue without automatically closing it.

Thank you for your contribution to the project!

@stdlib-bot Why did you add a potential duplicate module?, IN Issue #649 log10f packages implementation is still open

Removed unused variable declaration for options in benchmark.

Signed-off-by: MA RIZWAN <148482027+InFiNiTy0639@users.noreply.github.com>
@nakul-krishnakumar
Copy link
Member

@stdlib-bot Why did you add a potential duplicate module?, IN Issue #649 log10f packages implementation is still open

That is probably because there is already an open PR for log10f: #8873

@InFiNiTy0639
Copy link
Author

@stdlib-bot Why did you add a potential duplicate module?, IN Issue #649 log10f packages implementation is still open

That is probably because there is already an open PR for log10f: #8873

Yeah ,I also just checked this, but the PR is still not merged. I think I should work on other packages. 😢

@InFiNiTy0639 InFiNiTy0639 changed the title feat: Add math/base/special/log10f feat: Add C implementation math/base/special/log10f Feb 15, 2026
@stdlib-bot stdlib-bot removed the Potential Duplicate There might be another pull request resolving the same issue. label Feb 16, 2026
@InFiNiTy0639 InFiNiTy0639 mentioned this pull request Feb 16, 2026
7 tasks
Copy link
Member

@Planeshifter Planeshifter left a comment

Choose a reason for hiding this comment

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

Thanks for opening a PR!

* @param x input value
* @return output value
*/
float stdlib_base_log10f( const float x ) {
Copy link
Member

Choose a reason for hiding this comment

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

The parameter x is declared as const float, but it gets modified on line 70 (x *= TWO25) and line 83 (stdlib_base_float32_from_word(..., &x)). This will cause a compilation error.

I propose introducing a local float xc;, assign xc = x; near the top, and replace all subsequent uses of x with xc (see how lnf/src/main.c does it).

Comment on lines +59 to +108
function log10f(x) {
var hfsq;
var hi;
var lo;
var r;
var f;
var y;
var hx;
var i;
var k;

if (x === 0.0) {
return NINF;
}
if (isnanf(x) || x < 0.0) {
return NaN;
}
hx = toWord(x);
k = 0;
if (hx < FLT_MIN_NORMAL_EXP) {
// x < 2**-126
if ((hx & 0x7fffffff) === 0) {
return NINF; // log(+-0) = -inf
}
k -= 25;
x *= TWO25; // subnormal number, scale up x
hx = toWord(x);
}
if (hx >= FLT_MAX_EXP_BIT_MASK) {
return x + x;
}
if (hx === FLT_BIAS_EXP) {
return 0.0; // log(1) = +0
}
k += (hx >>> 23) - 127;
hx &= 0x007fffff;
i = (hx + 0x95f64) & 0x800000;
// Normalize x or x/2...
x = fromWord(hx | (i ^ FLT_BIAS_EXP));
k += (i >>> 23);
y = k;
f = x - 1.0;
hfsq = 0.5 * f * f;
r = kernelLog1pf(f);

hi = f - hfsq;
hx = toWord(hi);
hi = fromWord(hx & 0xfffff000);
lo = (f - hi) - hfsq + r;
return (y * LOG10_2LO) + ((lo + hi) * IVLN10) + (y * LOG10_2HI);
Copy link
Member

Choose a reason for hiding this comment

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

This is the most critical issue in this PR: the implementation is missing float64ToFloat32 wrapping around all arithmetic operations. In JavaScript, all math is performed in float64 by default. For a single-precision function, every intermediate computation must be explicitly rounded to float32 via float64ToFloat32(), or the results will be computed in double precision.

Take a look at how lnf/lib/main.js wraps every arithmetic expression in float64ToFloat32(...). For example, your line:

f = x - 1.0;

should be:

f = float64ToFloat32( x - 1.0 );

And:

hfsq = 0.5 * f * f;

should be:

hfsq = float64ToFloat32( 0.5 * float64ToFloat32( f * f ) );

This needs to be applied to every arithmetic expression in this function. You'll also need to add the float64ToFloat32 import at the top.

Comment on lines +23 to +27
var toWord = require('@stdlib/number/float32/base/to-word');
var fromWord = require('@stdlib/number/float32/base/from-word');
var isnanf = require('@stdlib/math/base/assert/is-nanf');
var kernelLog1pf = require('@stdlib/math/base/special/kernel-log1pf');
var NINF = require('@stdlib/constants/float32/ninf');
Copy link
Member

Choose a reason for hiding this comment

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

A few style issues here:

  1. stdlib uses spaces inside parentheses for require calls: require( '...' ) not require('...'). This applies everywhere in the file.
  2. The file uses 4-space indentation, but stdlib requires TAB indentation for .js files (per .editorconfig).
  3. You're missing a float64ToFloat32 import, which you'll need for the correctness fix mentioned below.

Please also check the lnf package for the expected formatting -- it's the closest reference implementation.


@license Apache-2.0

Copyright (c) 2018 The Stdlib Authors.
Copy link
Member

Choose a reason for hiding this comment

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

The copyright year should be 2025 for a new package, not 2018.

Suggested change
Copyright (c) 2018 The Stdlib Authors.
Copyright (c) 2025 The Stdlib Authors.


var tape = require('tape');
var isnanf = require('@stdlib/math/base/assert/is-nanf');
var isInfinitef = require('@stdlib/math/base/assert/is-infinitef');
Copy link
Member

Choose a reason for hiding this comment

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

isInfinitef is imported but never used. Please remove the unused import.

Also, all require calls in this file need spaces inside parentheses (e.g., require( 'tape' ) not require('tape')), and indentation should use tabs, not spaces.

Comment on lines +97 to +117
tape('the function matches Math.log10 results within tolerance', function test(t) {
var expected;
var delta;
var tol;
var x;
var y;
var i;

for (i = 0; i < 100; i++) {
x = (Math.random() * 100.0) + 0.1;
x = float64ToFloat32(x);
y = log10f(x);
expected = float64ToFloat32(Math.log10(x));
delta = absf(y - expected);
tol = 2.0 * EPS * absf(expected);
if (tol < EPS) {
tol = EPS;
}
t.ok(delta <= tol, 'within tolerance. x: ' + x + '. y: ' + y + '. expected: ' + expected + '. delta: ' + delta + '. tol: ' + tol);
}
t.end();
Copy link
Member

Choose a reason for hiding this comment

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

Using Math.random() makes these tests non-deterministic -- they could pass on one run and fail on another. The stdlib convention is to use pre-generated test fixtures (typically from Julia or similar) stored as JSON files in test/fixtures/. Take a look at lnf/test/test.js for the expected pattern with fixture-based tests covering different value ranges (large positive, small positive, subnormal, etc.).

#include <stdlib.h>
#include <stdio.h>

int main() {
Copy link
Member

Choose a reason for hiding this comment

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

stdlib convention uses int main( void ) instead of int main().

Suggested change
int main() {
int main( void ) {

]
}
]
} No newline at end of file
Copy link
Member

Choose a reason for hiding this comment

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

This file is missing a trailing newline at the end, which is required by the project conventions. Same issue in package.json.

var bench = require('@stdlib/bench');
var uniform = require('@stdlib/random/array/uniform');
var isnanf = require('@stdlib/math/base/assert/is-nanf');
var float64ToFloat32 = require('@stdlib/number/float64/base/to-float32');
Copy link
Member

Choose a reason for hiding this comment

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

float64ToFloat32 is imported but never used in this file -- it can be removed. Also, the same spacing/indentation style issues apply here (tabs, spaces inside parens).

@Planeshifter Planeshifter added Needs Changes Pull request which needs changes before being merged. and removed Needs Review A pull request which needs code review. labels Feb 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

First-time Contributor A pull request from a contributor who has never previously committed to the project repository. Math Issue or pull request specific to math functionality. Needs Changes Pull request which needs changes before being merged.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants