# Floating point calculation problem

**URL:** <https://www.ruby-forum.com/t/floating-point-calculation-problem/142465>\
**Category:** Ruby\
**Created:** [July 8, 2008, 10:43am UTC](https://www.ruby-forum.com/t/floating-point-calculation-problem/142465 "2008-07-08T10:43:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hyun\_chul\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/8e8cbc/32.png) [@Hyun\_chul\_P](https://www.ruby-forum.com/u/Hyun_chul_P)\
**Post date:** [July 8, 2008, 10:43am UTC](https://www.ruby-forum.com/t/floating-point-calculation-problem/142465/1 "2008-07-08T10:43:16Z")

</div>

hi.  
i’m confused by float point calculation.  
like this!

* * *

irb(main):001:0\> 123.6 - 123  
=\> 0.599999999999994

* * *

how to i get result 0.6 ?

---

<div class="post-metadata">

**Author:** ![Hyun\_chul\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/8e8cbc/32.png) [@Hyun\_chul\_P](https://www.ruby-forum.com/u/Hyun_chul_P)\
**Post date:** [July 8, 2008, 10:55am UTC](https://www.ruby-forum.com/t/floating-point-calculation-problem/142465/2 "2008-07-08T10:55:41Z")

</div>

Hi,

On Tue, Jul 8, 2008 at 10:39 AM, Hyun chul Park [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> how to i get result 0.6 ?  
> try using BigDecimal, like:

irb(main):001:0\> require ‘bigdecimal’  
=\> true  
irb(main):002:0\> x = BigDecimal(“123.6”) - BigDecimal(“123”)  
=\> #BigDecimal:8b13c,‘0.6E0’,4(16)  
irb(main):003:0\> x.to\_f  
=\> 0.6

zoran

---

<div class="post-metadata">

**Author:** ![Hyun\_chul\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/8e8cbc/32.png) [@Hyun\_chul\_P](https://www.ruby-forum.com/u/Hyun_chul_P)\
**Post date:** [July 8, 2008, 11:20am UTC](https://www.ruby-forum.com/t/floating-point-calculation-problem/142465/3 "2008-07-08T11:20:20Z")

</div>

-------- Original-Nachricht --------

> Datum: Tue, 8 Jul 2008 17:39:50 +0900  
> Von: Hyun chul Park [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> An: removed\_email\_address@domain.invalid  
> Betreff: Floating point calculation problem

Hi –

> how to i get result 0.6 ?  
> you may want to take a look at this:

[http://docs.sun.com/source/806-3568/ncg\_goldberg.html](http://docs.sun.com/source/806-3568/ncg_goldberg.html)

If you want to do exact calculations, you can use fractions

[http://www.ruby-doc.org/stdlib/libdoc/rational/rdoc/classes/Rational.html](http://www.ruby-doc.org/stdlib/libdoc/rational/rdoc/classes/Rational.html)

To convert between the two, for each real number (Float), you can use  
its continued fraction  
approximation.

> **[MathLab | Mathematical Sciences | Michigan Tech](https://www.mtu.edu/math/undergraduate/mathlab/)**
>
> The Michigan Tech MathLab shall enhance the understanding of core calculus concepts through the use of the Wolfram Mathematica program, as well as promoting an understanding of applied mathematics and general programming knowledge.

Here’s a link to a Ruby program that calculates digits of Pi from its  
continued fraction representation  
(and also a Java one, which is much longer):

[http://www.cs.utsa.edu/~wagner/pi/pi\_cont.html](http://www.cs.utsa.edu/~wagner/pi/pi_cont.html)

Best regards,

Axel

---

<div class="post-metadata">

**Author:** ![Hyun\_chul\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/8e8cbc/32.png) [@Hyun\_chul\_P](https://www.ruby-forum.com/u/Hyun_chul_P)\
**Post date:** [July 8, 2008, 11:11am UTC](https://www.ruby-forum.com/t/floating-point-calculation-problem/142465/4 "2008-07-08T11:11:27Z")

</div>

Zoran Regvart wrote:

> Hi,
> 
> On Tue, Jul 8, 2008 at 10:39 AM, Hyun chul Park [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > how to i get result 0.6 ?  
> > try using BigDecimal, like:
> 
> irb(main):001:0\> require ‘bigdecimal’  
> =\> true  
> irb(main):002:0\> x = BigDecimal(“123.6”) - BigDecimal(“123”)  
> =\> #BigDecimal:8b13c,‘0.6E0’,4(16)  
> irb(main):003:0\> x.to\_f  
> =\> 0.6
> 
> zoran

thank you, you’re reply.
