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

handle exponential floats #16

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open

Conversation

zoryamba
Copy link

@zoryamba zoryamba commented Mar 1, 2024

Fix for #15

Hope it won't affect performance significantly

@jgranstrom
Copy link
Owner

jgranstrom commented Mar 2, 2024

Good catch! However, I don't think we can regex evaluate all numbers to cover the scientific notation case. Is there another way to distinguish them?

There should be a cut-off where it is represented with those notions. However, I'm not sure that is platform agnostic. Regardless I think we'll have to find another approach for them as the regex at this very hot path is not really feasible I think.

@zoryamba
Copy link
Author

zoryamba commented Mar 3, 2024

I haven't done any benchmarks, but i think it's valid concern...

According to this reference

All numbers in Javascript are 64bit (8 bytes) floating point numbers which yields an effective range of 5e-324 (negative) to 1.7976931348623157e+308 (positive) at the time this article was written (this may eventually change to 128 bits in the future as 64 bit processors become commonplace and the ECMA standards evolve).

JS numbers seems to be platform agnostic, but i wouldn't rely on internal Number implementation because it may change.

There is Number.prototype.toExponential method that seems to return the same result as toString for exponential numbers

So using something kinda if (Number.isInteger(obj) && obj.toString() !== obj.toExponential()) seems reasonable. I'll update the PR

Comment on lines +99 to +106
it('floatExponentialOne', function() {
testPackUnpackHomogeneousArray(-1.552345411e+123, ONE, 2, true);
});

it('floatExponentialMany', function() {
testPackUnpackHomogeneousArray(-1.552345411e+123, MANY, 2, true);
});

Copy link
Owner

@jgranstrom jgranstrom Mar 3, 2024

Choose a reason for hiding this comment

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

Another couple of test cases that would be worth adding is like 1e1 and 1e-1 for numbers that would generally not be expressed with this notation, but still could be.

Copy link
Author

Choose a reason for hiding this comment

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

Makes sense. Added them in test/full/number.ts

if(obj % 1 === 0) {
if(Number.isInteger(obj) && obj.toString() !== obj.toExponential()) {
Copy link
Owner

Choose a reason for hiding this comment

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

I don't think you have to change the existing condition,
obj % 1 === 0 && obj.toString() !== obj.toExponential()
Should also work I think?

Copy link
Author

Choose a reason for hiding this comment

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

Thought inner implementation should be more performant... But i see that according to docs it may return wrong result with high precision. Maybe you're right. Will revert this

Copy link
Owner

Choose a reason for hiding this comment

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

Thought inner implementation should be more performant... But i see that according to docs it may return wrong result with high precision. Maybe you're right. Will revert this

Yeah could be, hard to tell unless benchmarked. I’m mostly thinking just for compatibility

@zoryamba
Copy link
Author

zoryamba commented Mar 3, 2024

added few optimizations

@zoryamba
Copy link
Author

zoryamba commented Mar 5, 2024

any more concerns here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants