# Newb math question

**URL:** <https://rubytalk.org/t/newb-math-question/18865>\
**Category:** ruby-talk\
**Created:** [9 June 2005 00:40 UTC](https://rubytalk.org/t/newb-math-question/18865 "2005-06-09T00:40:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brian\_Le\_Roy](https://avatars.discourse-cdn.com/v4/letter/b/a88e57/32.png) [@Brian\_Le\_Roy](https://rubytalk.org/u/Brian_Le_Roy)\
**Post date:** [9 June 2005 00:40 UTC](https://rubytalk.org/t/newb-math-question/18865/1 "2005-06-09T00:40:57Z")

</div>

Can someone tell me why this code:  
&nbsp;&nbsp;&nbsp;&nbsp;puts (9.0-8.9).to\_s  
results in:  
&nbsp;&nbsp;&nbsp;&nbsp;0.0999999999999996

I'm expecting 0.01 as the result. Do I have to specify the precision? Even with the full precision available on the platform (Windows XP), the correct answer is still 0.01, not what's printed. What's the deal? I like this language so far, but this is confusing...

Brian

---

<div class="post-metadata">

**Author:** ![Anonymous\_Coward](https://avatars.discourse-cdn.com/v4/letter/a/94ad74/32.png) [@Anonymous\_Coward](https://rubytalk.org/u/Anonymous_Coward)\
**Post date:** [9 June 2005 00:57 UTC](https://rubytalk.org/t/newb-math-question/18865/2 "2005-06-09T00:57:05Z")

</div>

Le 9/6/2005, "brian" \<brian@le-roy.org\> a écrit:

> Can someone tell me why this code:  
> &nbsp;&nbsp;&nbsp;puts (9.0-8.9).to\_s  
> results in:  
> &nbsp;&nbsp;&nbsp;0.0999999999999996
> 
> I'm expecting 0.01 as the result. Do I have to specify the precision?  
> Even with the full precision available on the platform (Windows XP), the  
> correct answer is still 0.01, not what's printed. What's the deal? I  
> like this language so far, but this is confusing...

Floating point math 😕 You can use BigDecimal to avoid most problems.

> Brian

E

> **···**
>
> --  
> template\<typename duck\>  
> void quack(duck& d) { d.quack(); }

---

<div class="post-metadata">

**Author:** ![Brian\_Le\_Roy](https://avatars.discourse-cdn.com/v4/letter/b/a88e57/32.png) [@Brian\_Le\_Roy](https://rubytalk.org/u/Brian_Le_Roy)\
**Post date:** [9 June 2005 02:21 UTC](https://rubytalk.org/t/newb-math-question/18865/3 "2005-06-09T02:21:09Z")

</div>

Thanks, I'll give it a read. Thanks to Bill too. I have to say this is one of the friendlier communities I've come across. The BigDecimal is working out. Newb's not getting burned with RTFM's, tends to keep them coming back for more. Thanks again!

Gary Wright wrote:

> **···**
>
> > On Jun 8, 2005, at 8:40 PM, brian wrote:
> > 
> > > Can someone tell me why this code:  
> > > &nbsp;&nbsp;&nbsp;puts (9.0-8.9).to\_s  
> > > results in:  
> > > &nbsp;&nbsp;&nbsp;0.0999999999999996
> > > 
> > > I'm expecting 0.01 as the result. Do I have to specify the precision? Even with the full precision available on the platform (Windows XP), the correct answer is still 0.01, not what's printed. What's the deal? I like this language so far, but this is confusing...
> > 
> > This is a floating point math issue not a ruby language issue.  
> > Some base 10 floating point numbers can not be represented exactly  
> > when converted to binary (and vice versa). This often leads to small  
> > differences between the arithmetic results expected by you (in base 10  
> > format) and calculated by the computer (in binary).
> > 
> > The solution is to look for "close answers" or switch to a different  
> > encoding (i.e. not binary floating point) for your math. In many  
> > cases just using integers with an appropriate scaling factor solves  
> > the problem.
> > 
> > If anyone is interested in all the gory details of this topic here is  
> > a good read: "What Every Computer Scientist Should Know About Floating-Point  
> > Arithmetic".
> > 
> > [http://docs.sun.com/source/806-3568/ncg\_goldberg.html](http://docs.sun.com/source/806-3568/ncg_goldberg.html)

---

<div class="post-metadata">

**Author:** ![Jacob\_Fugal](https://avatars.discourse-cdn.com/v4/letter/j/439d5e/32.png) [@Jacob\_Fugal](https://rubytalk.org/u/Jacob_Fugal)\
**Post date:** [9 June 2005 16:27 UTC](https://rubytalk.org/t/newb-math-question/18865/4 "2005-06-09T16:27:32Z")

</div>

Along with all the other good responses, I thought I'd just take the  
moment to (once again) state that this behaviour isn't unique to ruby:

&nbsp;&nbsp;&nbsp;&nbsp;ruby: "%.16f" % (9.0-8.9) ==\> 0.0999999999999996  
&nbsp;&nbsp;&nbsp;&nbsp;perl: sprintf("%.16f", 9.0-8.9) ==\> 0.0999999999999996  
&nbsp;&nbsp;&nbsp;&nbsp;python: "%.16f" % (9.0-8.9) ==\> 0.0999999999999996  
&nbsp;&nbsp;&nbsp;&nbsp;php: sprintf("%.16f", 9.0-8.9) ==\> 0.0999999999999996  
&nbsp;&nbsp;&nbsp;&nbsp;C: sprintf(dest, "%.16f", 9.0-8.9) ==\> 0.0999999999999996

In all of the cases, specifying a lower precision allows the  
float-\>string conversion to round to the "expected" result:

&nbsp;&nbsp;&nbsp;&nbsp;ruby: "%.10f" % (9.0-8.9) ==\> 0.1000000000  
&nbsp;&nbsp;&nbsp;&nbsp;perl: sprintf("%.10f", 9.0-8.9) ==\> 0.1000000000  
&nbsp;&nbsp;&nbsp;&nbsp;python: "%.10f" % (9.0-8.9) ==\> 0.1000000000  
&nbsp;&nbsp;&nbsp;&nbsp;php: sprintf("%.10f", 9.0-8.9) ==\> 0.1000000000  
&nbsp;&nbsp;&nbsp;&nbsp;C: sprintf(dest, "%.10f", 9.0-8.9) ==\> 0.1000000000

(The source for round\_error\_c\_2 is identical to round\_error\_c but with  
only 10 instead of 16 digits of precision in the printf.)

The major difference is the default precision used by languages in the  
float-\>string conversion. From experimentation (not actually looking  
at sources), I came to the following conclusions on the default  
precision:

&nbsp;&nbsp;&nbsp;&nbsp;\* ruby and perl both use up to 15 significant digits  
&nbsp;&nbsp;&nbsp;&nbsp;\* python uses up to 12 significant digits  
&nbsp;&nbsp;&nbsp;&nbsp;\* php uses up to 14 significant digits  
&nbsp;&nbsp;&nbsp;&nbsp;\* C uses 6 places after the decimal, always  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(with %f instead of %.10f in the printf)

For all of ruby, perl, python and php I say "up to X significant  
digits" because significant 0's at the end are truncated (e.g. (1.0 -  
0.5).to\_s returns '0.5', not '0.500000000000000') and significant  
digit rules are obeyed (significant digits before the decimal point  
reduce the number of digits after the decimal point, leading zeros  
after the decimal point are not counted).

Jacob Fugal

> **···**
>
> On 6/8/05, brian \<brian@le-roy.org\> wrote:
> 
> > Can someone tell me why this code:  
> > &nbsp;&nbsp;&nbsp;&nbsp;puts (9.0-8.9).to\_s  
> > results in:  
> > &nbsp;&nbsp;&nbsp;&nbsp;0.0999999999999996
> > 
> > I'm expecting 0.01 as the result. Do I have to specify the precision?  
> > Even with the full precision available on the platform (Windows XP), the  
> > correct answer is still 0.01, not what's printed. What's the deal? I  
> > like this language so far, but this is confusing...
> > 
> > Brian

---

<div class="post-metadata">

**Author:** ![Bill\_Kelly](https://avatars.discourse-cdn.com/v4/letter/b/cc9497/32.png) [@Bill\_Kelly](https://rubytalk.org/u/Bill_Kelly)\
**Post date:** [9 June 2005 01:57 UTC](https://rubytalk.org/t/newb-math-question/18865/5 "2005-06-09T01:57:58Z")

</div>

Hi Brian,

> Le 9/6/2005, "brian" \<brian@le-roy.org\> a écrit:  
> \>Can someone tell me why this code:  
> \> puts (9.0-8.9).to\_s  
> \>results in:  
> \> 0.0999999999999996  
> \>  
> \>I'm expecting 0.01 as the result. Do I have to specify the precision?  
> \>Even with the full precision available on the platform (Windows XP), the  
> \>correct answer is still 0.01, not what's printed. What's the deal? I  
> \>like this language so far, but this is confusing...
> 
> Floating point math 😕 You can use BigDecimal to avoid most problems.

If you'd like more info about the problem, the first  
paragraphs of the section entitled Rounding Error in  
this document give the gist of it:  
[http://docs.sun.com/source/806-3568/ncg\_goldberg.html](http://docs.sun.com/source/806-3568/ncg_goldberg.html)

Regards,

Bill

> **···**
>
> From: "ES" \<ruby-ml@magical-cat.org\>
