# Math problem

**URL:** <https://www.ruby-forum.com/t/math-problem/115895>\
**Category:** JRuby\
**Created:** [September 30, 2007, 12:03am UTC](https://www.ruby-forum.com/t/math-problem/115895 "2007-09-30T00:03:15Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dimitri\_Krassovski](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8e8cbc/32.png) [@Dimitri\_Krassovski](https://www.ruby-forum.com/u/Dimitri_Krassovski)\
**Post date:** [September 30, 2007, 12:03am UTC](https://www.ruby-forum.com/t/math-problem/115895/1 "2007-09-30T00:03:15Z")

</div>

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
```

---

<div class="post-metadata">

**Author:** ![Dimitri\_Krassovski](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8e8cbc/32.png) [@Dimitri\_Krassovski](https://www.ruby-forum.com/u/Dimitri_Krassovski)\
**Post date:** [September 30, 2007, 3:53am UTC](https://www.ruby-forum.com/t/math-problem/115895/2 "2007-09-30T03:53:32Z")

</div>

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
```

---

<div class="post-metadata">

**Author:** ![Dimitri\_Krassovski](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8e8cbc/32.png) [@Dimitri\_Krassovski](https://www.ruby-forum.com/u/Dimitri_Krassovski)\
**Post date:** [September 30, 2007, 8:13am UTC](https://www.ruby-forum.com/t/math-problem/115895/3 "2007-09-30T08:13:02Z")

</div>

On 9/30/07, Charles Oliver N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

```

---

<div class="post-metadata">

**Author:** ![Dimitri\_Krassovski](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8e8cbc/32.png) [@Dimitri\_Krassovski](https://www.ruby-forum.com/u/Dimitri_Krassovski)\
**Post date:** [September 30, 2007, 2:45pm UTC](https://www.ruby-forum.com/t/math-problem/115895/4 "2007-09-30T14:45:45Z")

</div>

— 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.

> **[Floating-point arithmetic | Accuracy problems](https://en.wikipedia.org/wiki/Floating_point#Accuracy_problems)**
>
> The fact that floating-point numbers cannot accurately represent all real numbers, and that floating-point operations cannot accurately represent true arithmetic operations, leads to many surprising situations. This is related to the finite precision with which computers generally represent numbers.
> For example, the decimal numbers 0.1 and 0.01 cannot be represented exactly as binary floating-point numbers. In the IEEE 754 binary32 format with its 24-bit significand, the result of attempting to ...

d.

* * *

To unsubscribe from this list please visit:

```
http://xircles.codehaus.org/manage_email

```

---

<div class="post-metadata">

**Author:** ![Dimitri\_Krassovski](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8e8cbc/32.png) [@Dimitri\_Krassovski](https://www.ruby-forum.com/u/Dimitri_Krassovski)\
**Post date:** [September 30, 2007, 6:38pm UTC](https://www.ruby-forum.com/t/math-problem/115895/5 "2007-09-30T18:38:38Z")

</div>

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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

```

---

<div class="post-metadata">

**Author:** ![Dimitri\_Krassovski](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8e8cbc/32.png) [@Dimitri\_Krassovski](https://www.ruby-forum.com/u/Dimitri_Krassovski)\
**Post date:** [October 1, 2007, 1:09am UTC](https://www.ruby-forum.com/t/math-problem/115895/6 "2007-10-01T01:09:34Z")

</div>

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](http://en.wikipedia.org/wiki/Floating_point#Accuracy_problems)  
> > 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 removed_email_address@domain.invalid
  ::

```

* * *

To unsubscribe from this list please visit:

```
http://xircles.codehaus.org/manage_email

```

---

<div class="post-metadata">

**Author:** ![Dimitri\_Krassovski](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8e8cbc/32.png) [@Dimitri\_Krassovski](https://www.ruby-forum.com/u/Dimitri_Krassovski)\
**Post date:** [October 1, 2007, 1:24am UTC](https://www.ruby-forum.com/t/math-problem/115895/7 "2007-10-01T01:24:11Z")

</div>

On 10/1/07, Luke M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

```
