Forum: JRuby math problem

F5049506664636c6cc725099367bd167?d=identicon&s=25 Dimitri Krassovski (Guest)
on 2007-09-30 00:03
(Received via mailing list)
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
Ede2aa10c6462f1d825143879be59e38?d=identicon&s=25 Charles Oliver Nutter (Guest)
on 2007-09-30 03:53
(Received via mailing list)
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
F5049506664636c6cc725099367bd167?d=identicon&s=25 Dimitri Krassovski (Guest)
on 2007-09-30 08:13
(Received via mailing list)
On 9/30/07, Charles Oliver Nutter <charles.nutter@sun.com> 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.1*10*3/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
23b4a6da6fa17ddfb5665cafe4c1c881?d=identicon&s=25 Dave Newton (Guest)
on 2007-09-30 14:45
(Received via mailing list)
--- 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.

http://en.wikipedia.org/wiki/Floating_point#Accura...

d.


---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email
F5049506664636c6cc725099367bd167?d=identicon&s=25 Dimitri Krassovski (Guest)
on 2007-09-30 18:38
(Received via mailing list)
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 Newton <newton.dave@yahoo.com> 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
C15f8583ff7dfb5dd0d580e7d9713c6a?d=identicon&s=25 Luke Monahan (Guest)
on 2007-10-01 01:09
(Received via mailing list)
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.

> > http://en.wikipedia.org/wiki/Floating_point#Accura...
> 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 Monahan
      Senior Software Engineer
      T 03 8080 5777
      F 03 8080 5778
      M 0403 436 915
      E luke.monahan@remora.com.au
      ::

---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email
F5049506664636c6cc725099367bd167?d=identicon&s=25 Dimitri Krassovski (Guest)
on 2007-10-01 01:24
(Received via mailing list)
On 10/1/07, Luke Monahan <luke.monahan@remora.com.au> 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
This topic is locked and can not be replied to.