float number accuracy

44 views
Skip to first unread message

Jerry Jin

unread,
Jun 25, 2019, 5:24:56 AM6/25/19
to v8-dev
Hello, 
I'm new to v8-dev, sorry if this was already asked before.

my problem is, i re-implemented a c++ numerical lib using typescript, and some test cases failed due to float number not having enough accuracy
(i debugged js/c++ code side by side with chrome devtool&msvc)
right now, my only option is to user third party lib like `decimal.js`

i wonder is there any way to increase float number accuracy like c++ double type? 

example, is there anyway to store more than 15 decimal places in number
```
> let pi = 3.141592653589793238462643383280;
< undefined
> pi
< 3.141592653589793
```

not sure if this is possible in v8, if yes, where should i start?

thanks!

Dan Elphick

unread,
Jun 25, 2019, 8:19:18 AM6/25/19
to v8-dev
On Tuesday, June 25, 2019 at 10:24:56 AM UTC+1, Jerry Jin wrote:
Hello, 
I'm new to v8-dev, sorry if this was already asked before.

my problem is, i re-implemented a c++ numerical lib using typescript, and some test cases failed due to float number not having enough accuracy
(i debugged js/c++ code side by side with chrome devtool&msvc)
right now, my only option is to user third party lib like `decimal.js`

i wonder is there any way to increase float number accuracy like c++ double type?

example, is there anyway to store more than 15 decimal places in number

Javascript and V8 already use doubles to represent floating point numbers. However double precision floating point numbers can have about 16 significant figures, so it's not possible to get any more accuracy using C++ double. You may be able to print more than that (e.g. pi.ToFixed(30) but they won't be accurate beyond 15 decimal places.
  

Jakob Kummerow

unread,
Jun 25, 2019, 8:51:58 AM6/25/19
to v8-...@googlegroups.com
If your original C++ code uses doubles, then porting it to TypeScript/JavaScript should be possible without loss of accuracy (and using plain built-in Numbers, no external libraries). If you have a reproducible counter-example, it would be interesting to hear about the details.

The only exception I can think of is if your C++ compiler decided to emit x87 FPU code (on x86 hardware) which would use 80 bits (instead of 64 bits) for intermediate results. But I don't think modern compilers still do that; they should emit 64-bit SSE instructions.

Side note: modern JavaScript has BigInts, which have arbitrary precision, but only integer values. So they're probably not useful for your case, but depending on what problem you're solving, you might be able to reformulate it to use integers.


--
--
v8-dev mailing list
v8-...@googlegroups.com
http://groups.google.com/group/v8-dev
---
You received this message because you are subscribed to the Google Groups "v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to v8-dev+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/v8-dev/cfed0633-c6e7-45c4-a5c0-ca9f6cfaec9a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jerry Jin

unread,
Jun 25, 2019, 11:52:08 PM6/25/19
to v8-dev
thanks! is it because c++ double has 1 more significant figure than js, that cause the accuracy issue?

Jerry Jin

unread,
Jun 26, 2019, 12:00:16 AM6/26/19
to v8-dev
thank you, i googled around and found msvc double is using 64-bit sse, not x87 fpu

not sure if it's because c++ double has 16 significant figures and js has 15
To unsubscribe from this group and stop receiving emails from it, send an email to v8-...@googlegroups.com.

Leszek Swirski

unread,
Jun 26, 2019, 1:56:34 AM6/26/19
to v8-dev
To be clear: both C++ and JS use the exact same "double" representation, neither is more precise than the other. In fact we use the exact same machine instructions to feel with them. The significant digits/decimal digits mentioned are when printing, not when calculating.

So, if it's not x87 extra precision, then it must either be a non exact translation from C++ to JavaScript, or a bug. If you have a small repro that demonstrates the difference, then please file a bug and we'll take a look.

- Leszek


To unsubscribe from this group and stop receiving emails from it, send an email to v8-dev+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/v8-dev/bc1e8872-a93a-405b-94b7-0a8ad01e24f2%40googlegroups.com.

Jerry Jin

unread,
Jun 26, 2019, 3:50:56 AM6/26/19
to v8-dev
thank you, i'll come back later with sample code


On Wednesday, June 26, 2019 at 1:56:34 PM UTC+8, Leszek Swirski wrote:
To be clear: both C++ and JS use the exact same "double" representation, neither is more precise than the other. In fact we use the exact same machine instructions to feel with them. The significant digits/decimal digits mentioned are when printing, not when calculating.

So, if it's not x87 extra precision, then it must either be a non exact translation from C++ to JavaScript, or a bug. If you have a small repro that demonstrates the difference, then please file a bug and we'll take a look.

- Leszek


Jerry Jin

unread,
Sep 24, 2019, 11:41:29 PM9/24/19
to v8-dev
thank you Leszek, you are right, i found the bug in my code, it was not exact translation
now my javascript code can do the same numerical calculation, with same accuracy!

Leszek Swirski

unread,
Sep 25, 2019, 1:59:12 AM9/25/19
to v8-dev
Thanks for closing the loop :)

To unsubscribe from this group and stop receiving emails from it, send an email to v8-dev+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/v8-dev/79e10225-6522-4524-810b-fa2db0f2e0e3%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages