floating point accuracy in JRuby

S

Steven D. Arnold

[ 03:23 PM (5) omega:thoth ~ ] > jirb
irb(main):001:0> 0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1
=> 0.9999999999999999
[ 03:33 PM (8) omega:thoth ~ ] > irb=> 1.0

Disappointing that JRuby does not handle floating point numbers (a) in
the the same way as CRuby and (b) not as accurately. Is the cost of
implementing CRuby's behavior prohibitively high on the JVM, is there
a fix planned in the future? How would JRuby users recommend handling
currency on that platform?

steven
 
J

Joel VanderWerf

Steven said:
[ 03:23 PM (5) omega:thoth ~ ] > jirb
irb(main):001:0> 0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1
=> 0.9999999999999999
[ 03:33 PM (8) omega:thoth ~ ] > irb=> 1.0

Disappointing that JRuby does not handle floating point numbers (a) in
the the same way as CRuby and (b) not as accurately. Is the cost of
implementing CRuby's behavior prohibitively high on the JVM, is there a
fix planned in the future? How would JRuby users recommend handling
currency on that platform?

steven

irb(main):001:0> printf "%20.18f", 0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1
0.999999999999999889=> nil


irb(main):004:0* RUBY_VERSION
=> "1.8.6"
 
M

Mike Sassak

[Note: parts of this message were removed to make it a legal post.]

When handling currency I'd suggest storing everything in cents with integers
and using a Money library like http://money.rubyforge.org/. You won't have
any rounding errors then.

On Thu, Jun 18, 2009 at 3:44 PM, Steven D. Arnold <
[ 03:23 PM (5) omega:thoth ~ ] > jirb
irb(main):001:0> 0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1
=> 0.9999999999999999
[ 03:33 PM (8) omega:thoth ~ ] > irb=> 1.0

Disappointing that JRuby does not handle floating point numbers (a) in the
the same way as CRuby and (b) not as accurately. Is the cost of
implementing CRuby's behavior prohibitively high on the JVM, is there a fix
planned in the future? How would JRuby users recommend handling currency on
that platform?

steven
 
T

Tim Hunter

Steven said:
[ 03:23 PM (5) omega:thoth ~ ] > jirb
irb(main):001:0> 0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1
=> 0.9999999999999999
[ 03:33 PM (8) omega:thoth ~ ] > irb=> 1.0

Disappointing that JRuby does not handle floating point numbers (a) in
the the same way as CRuby and (b) not as accurately. Is the cost of
implementing CRuby's behavior prohibitively high on the JVM, is there a
fix planned in the future? How would JRuby users recommend handling
currency on that platform?

steven

I don't think it's a good idea to use binary floating-point numbers for
currency in any language or any platform. See the numerous previous
discussions of binary vs. decimal arithmetic on this list.

Use integer cents instead, or BigDecimal.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top