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

Draft: Use 128-bit floats instead of BigInts #326

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft

Conversation

ptomato
Copy link
Contributor

@ptomato ptomato commented Mar 28, 2025

I read a paper on double-double precision arithmetic (i.e., using two Numbers to implement 128-bit floats with the same range as Number but twice the precision.) This precision would allow safe integers covering the entire epoch nanoseconds range that we support, as well as being precise enough for all the floating point calculations that we do in methods like Duration.p.total().

The paper is: Hida, Li, Bailey (2008). Library for double-double and quad-double arithmetic. Technical report, Lawrence Berkeley National Laboratory. https://www.davidhbailey.com/dhbpapers/qd.pdf

They published a C++ library at https://github.com/BL-highprecision/QD. It was a quick experiment to implement a subset of the operations in JS in lib/float128.ts. This allows us to get rid of both JSBI as well as the power-of-10-math-by-string-manipulation in lib/math.ts.

I read a paper on double-double precision arithmetic (i.e., using two
Numbers to implement 128-bit floats with the same range as Number but
twice the precision.) This precision would allow safe integers covering
the entire epoch nanoseconds range that we support, as well as being
precise enough for all the floating point calculations that we do in
methods like Duration.p.total().

The paper is: Hida, Li, Bailey (2008). Library for double-double and
quad-double arithmetic. Technical report, Lawrence Berkeley National
Laboratory. https://www.davidhbailey.com/dhbpapers/qd.pdf

They published a C++ library at https://github.com/BL-highprecision/QD.
It was a quick experiment to implement a subset of the operations in JS
in lib/float128.ts. This allows us to get rid of both JSBI as well as
the power-of-10-math-by-string-manipulation in lib/math.ts.
@ptomato ptomato marked this pull request as draft March 28, 2025 01:20
@ptomato
Copy link
Contributor Author

ptomato commented Mar 28, 2025

This didn't have any obvious effect on performance, either good or bad.

Unfortunately it does make the default bundle bigger (by about +3k minified). Turns out JSBI is quite a small library when it's just a thin wrapper around BigInt! And if we ever got a build working where JSBI was compiled out when targeting new enough browsers, the increase in bundle size would get worse, because the default bundle would not even have JSBI in it.

But when transpiling down to ES5 it saves quite a lot of space (-31k minified). I doubt that's worth increasing everyone else's bundle size though.

So in the end I think my opinion is that we should not merge this, but I wanted to put it up for comments.

@justingrant
Copy link
Contributor

If it doesn't make anything faster, and makes the bundle larger, then it doesn't seem worth doing, IMO.

@ptomato
Copy link
Contributor Author

ptomato commented Mar 28, 2025

Yeah, my current thinking is that this is more of a curiosity that could be useful for implementations if they didn't want to use JS BigInt or compiler-dependent __int128 internally.

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