Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add math/base/special/exp2f #3366

Open
wants to merge 16 commits into
base: develop
Choose a base branch
from

Conversation

aayush0325
Copy link
Member

@aayush0325 aayush0325 commented Dec 7, 2024

Progresses #649

Description

What is the purpose of this pull request?

This pull request:

  • adds math/base/special/exp2f

Related Issues

Does this pull request have any related issues?

This pull request:

Questions

Any questions for reviewers of this pull request?

What should i do about benchmark/c/cephes ? that doesn't seem to compile locally and on the CI tests as well.

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.


@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 7, 2024
@aayush0325
Copy link
Member Author

/stdlib update-copyright-years

@stdlib-bot stdlib-bot added the bot: In Progress Pull request is currently awaiting automation. label Dec 7, 2024
@stdlib-bot stdlib-bot removed the bot: In Progress Pull request is currently awaiting automation. label Dec 7, 2024
@stdlib-bot
Copy link
Contributor

stdlib-bot commented Dec 7, 2024

Coverage Report

Package Statements Branches Functions Lines
math/base/special/exp2f 328 / 328
+ 100.00 %
19 / 19
+ 100.00 %
4 / 4
+ 100.00 %
328 / 328
+ 100.00 %

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

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 your work on this PR! Looks great overall, but some changes will be needed for the JavaScript implementation.

In main.js, we need to use the helper function

var float64ToFloat32 = require( '@stdlib/number/float64/base/to-float32' );

in order to ensure that all operations are done in a manner consistent with single-precision arithmetic. Recall that in JavaScript, all numbers are double-precision by default, so to emulate single-precision we must, in addition to using the single-precision variants of math functions, explicitly cast intermediate results and constants to single-precision floats.

@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 Dec 15, 2024
@aayush0325
Copy link
Member Author

good morning @Planeshifter! I've wrapped the intermediate results and final answers in float64ToFloat32(). If there's anything else that needs to be changed here please let me know.

@aayush0325
Copy link
Member Author

aayush0325 commented Dec 15, 2024

Also, as mentioned in the questions section of the PR, what should i do about benchmark/c/cephes since it doesn't seem to compile locally and it fails on CI as well. Is there something that I'm missing from my side? (see this commit)

@stdlib-bot stdlib-bot added the Needs Review A pull request which needs code review. label Dec 16, 2024
@kgryte kgryte requested a review from gunjjoshi March 14, 2025 09:45
@kgryte kgryte removed the Needs Changes Pull request which needs changes before being merged. label Mar 14, 2025
@Planeshifter Planeshifter added the Potential Duplicate There might be another pull request resolving the same issue. label Mar 15, 2025
@gunjjoshi
Copy link
Member

/stdlib update-copyright-years

@stdlib-bot stdlib-bot added the bot: In Progress Pull request is currently awaiting automation. label Mar 19, 2025
@stdlib-bot stdlib-bot removed the bot: In Progress Pull request is currently awaiting automation. label Mar 19, 2025
Signed-off-by: Gunj Joshi <gunjjoshi8372@gmail.com>
Comment on lines +62 to +71
var randu = require( '@stdlib/random/base/randu' );
var exp2f = require( '@stdlib/math/base/special/exp2f' );

var x;
var i;

for ( i = 0; i < 100; i++ ) {
x = ( randu() * 100.0 ) - 50.0;
console.log( '2^%d = %d', x, exp2f( x ) );
}
Copy link
Member

Choose a reason for hiding this comment

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

@aayush0325 We can use @stdlib/random/array/uniform here.

Copy link
Member

Choose a reason for hiding this comment

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

Same can be followed in examples/index.js too.

Signed-off-by: Gunj Joshi <gunjjoshi8372@gmail.com>
…chmark.native.js

Signed-off-by: Gunj Joshi <gunjjoshi8372@gmail.com>
@gunjjoshi gunjjoshi added Needs Changes Pull request which needs changes before being merged. and removed Needs Review A pull request which needs code review. labels Mar 21, 2025
Comment on lines +95 to +96
float y;
double t;
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
float y;
double t;
double t;
float y;

*
* @return random number
*/
static double rand_float( void ) {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
static double rand_float( void ) {
static float rand_float( void ) {

Copy link
Member

Choose a reason for hiding this comment

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

We are returning a float here.

Comment on lines +94 to +95
double y;
double t;
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
double y;
double t;
double t;
float y;

Copy link
Member

Choose a reason for hiding this comment

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

y must be a float, as exp2f returns a float, in L103.

int i;

for ( i = 0; i < 10; i++ ) {
x[ i ] = ( 240.0 * rand_float() ) - 120.0;
Copy link
Member

Choose a reason for hiding this comment

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

Missing f suffixes.

Comment on lines +3 to +4
Evaluates the base 2 exponential function in
single-precision floating-point format.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
Evaluates the base 2 exponential function in
single-precision floating-point format.
Evaluates the base 2 exponential function in single-precision
floating-point format.

Comment on lines +24 to +32
float x;
float v;
int i;

for ( i = 0; i < 100; i++ ) {
x = ( ( (float)rand() / (float)RAND_MAX ) * 100.0f ) - 50.0f;
v = stdlib_base_exp2f( x );
printf( "2^%f = %f\n", x, v );
}
Copy link
Member

Choose a reason for hiding this comment

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

@aayush0325 We should be using tabs here, not spaces.

*
* | arithmetic | domain | # trials | peak | rms |
* |:----------:|:-----------:|:--------:|:-------:|:-------:|
* | IEEE | -1022,+1024 | 30000 | 1.8e-16 | 5.4e-17 |
Copy link
Member

Choose a reason for hiding this comment

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

@aayush0325 These values for domain, number of trials, peak and rms are for the double-precision implementation, i.e., exp2. You'll need to check the single-precision implementation and update these.

Copy link
Member

Choose a reason for hiding this comment

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

For instance, the domain for exp2f will be restricted to -127,+127, and not -1022,+1024.

Comment on lines +110 to +111
}
// Separate into integer and fractional parts...
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
}
// Separate into integer and fractional parts...
}
// Separate into integer and fractional parts...

Copy link
Member

Choose a reason for hiding this comment

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

Empty line before a comment.

return PINF;
}
if ( x < FLOAT32_MIN_BASE2_EXPONENT ) {
return float64ToFloat32( 0.0 );
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
return float64ToFloat32( 0.0 );
return 0.0;

Copy link
Member

Choose a reason for hiding this comment

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

0.0 can be returned directly here.

Copy link
Member

Choose a reason for hiding this comment

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

@aayush0325 After L110, you'll need to add a conditional statement to return 1.0 if x is 0.0. You might want to refer to the single-precision implementation (exp2f) from cephes, as it is a bit different from porting exp2 directly to single-precision.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Math Issue or pull request specific to math functionality. Needs Changes Pull request which needs changes before being merged. Potential Duplicate There might be another pull request resolving the same issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants