Math problem

I’m totally new to jRuby, but i found a really weird thing while
testing some of my apps.
In jirb:
irb(main):001:0> (2…10).map { |x| [ 10.1*x, x]}
=> [[20.2, 2], [30.299999999999997, 3], [40.4, 4], [50.5, 5],
[60.599999999999994, 6], [70.7, 7], [80.8, 8], [90.89999999999999, 9],
[101.0, 10]]
In normal irb it works normally, returning
[[20.2, 2], [30.3, 3], [40.4, 4], [50.5, 5], [60.6, 6], [70.7, 7],
[80.8, 8], [90.9, 9], [101.0, 10]]

Is this a known issue, is there a way to fix it, and am I bound to
find such stuff or was this one exceptional? =)

PS: I’m on OS X/10.4.10, jruby-1.0.1.


To unsubscribe from this list please visit:

http://xircles.codehaus.org/manage_email

Dimitri Krassovski wrote:

Is this a known issue, is there a way to fix it, and am I bound to
find such stuff or was this one exceptional? =)

PS: I’m on OS X/10.4.10, jruby-1.0.1.

It’s a precision issue, and one we’re not likely to correct in JRuby
proper. JRuby defers to Java’s handling of floating point math, so you
get anomalies like 30.29999999997. In truth, the JRuby version is
probably more “true” to the actual value you’re getting back. I wonder,
is MRI rounding things after a certain precision or what?

  • Charlie

To unsubscribe from this list please visit:

http://xircles.codehaus.org/manage_email

On 9/30/07, Charles Oliver N. [email protected] wrote:

[80.8, 8], [90.9, 9], [101.0, 10]]
is MRI rounding things after a certain precision or what?

  • Charlie

Yes, i just tried the same in java. Funny thng that 10.1103/10 does
return 30.3 like it should, while 10.1*3 doesn’t. Anyway, I don’t
think ill be digging deeper into this java precision thing, but it
really seems a drawback for me.
By the way, thanks for the terrific work you’ve done on jruby!


To unsubscribe from this list please visit:

http://xircles.codehaus.org/manage_email

— Dimitri Krassovski wrote:

I don’t think ill be digging deeper into this java
precision thing, but it really seems a drawback for
me.

That’s how floating point works: there are some
numbers that cannot be represented precisely.

d.


To unsubscribe from this list please visit:

http://xircles.codehaus.org/manage_email

Yeah, i understand that floating numbers ain’t really easy. The thing
that bothers me is that java fails on such a simple calculation. I
made some tests and I found out that every language I checked (Obj-C,
C, ruby, php) have no problems giving me 30.3 when multiplying 10.1 by
3. Only java has this 30.299999999999997 problem.

On 9/30/07, Dave N. [email protected] wrote:

d.


To unsubscribe from this list please visit:

http://xircles.codehaus.org/manage_email

To unsubscribe from this list please visit:

http://xircles.codehaus.org/manage_email

On Monday 01 October 2007 02:38:03 Dimitri Krassovski wrote:

Yeah, i understand that floating numbers ain’t really easy. The thing
that bothers me is that java fails on such a simple calculation. I
made some tests and I found out that every language I checked (Obj-C,
C, ruby, php) have no problems giving me 30.3 when multiplying 10.1 by
3. Only java has this 30.299999999999997 problem.

This is only an issue in displaying the number. In IRB try this:

#>> x = 10.1*3
#=> 30.3
#>> x == 30.3
#=> false

MRI (and many other languages) “hide” the fact that it’s calculation is
imprecise. The Java version lets you know earlier that you’re dealing
with an
imprecise number.

Consider using BigDecimal when possible, as it has fixed precision.

L.

Floating-point arithmetic - Wikipedia
To unsubscribe from this list please visit:

http://xircles.codehaus.org/manage_email


::
Remora Technologies Pty Ltd
www.remora.com.au
Level 6, 685 Burke Road, Camberwell VIC 3124

  Luke M.
  Senior Software Engineer
  T 03 8080 5777
  F 03 8080 5778
  M 0403 436 915
  E [email protected]
  ::

To unsubscribe from this list please visit:

http://xircles.codehaus.org/manage_email

On 10/1/07, Luke M. [email protected] wrote:

#>> x = 10.1*3
L.
Thanks, I didn’t check that. I did some checks on the actual precision
used and it seems its the same after all.
But then, I think it would be reasonable to see gow MRI does the
“hiding” and implement the same in jruby, for as I understand it’s
supposed to mimic ruby’s behaviour. And in my case, i got this .29999
thing right out into the user interface. The closer jruby behaves to
MRI the easier and “safer” it will be to use the apps writen for MRI
with it.
I may be wrong, of course, if so, correct me.


To unsubscribe from this list please visit:

http://xircles.codehaus.org/manage_email