# 2000-1992.2 - is this a known bug?

**URL:** <https://www.ruby-forum.com/t/2000-1992-2-is-this-a-known-bug/162224>\
**Category:** Ruby\
**Created:** [March 7, 2009, 12:05am UTC](https://www.ruby-forum.com/t/2000-1992-2-is-this-a-known-bug/162224 "2009-03-07T00:05:45Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![S2S2S2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f08c70/32.png) [@S2S2S2](https://www.ruby-forum.com/u/S2S2S2)\
**Post date:** [March 7, 2009, 12:05am UTC](https://www.ruby-forum.com/t/2000-1992-2-is-this-a-known-bug/162224/1 "2009-03-07T00:05:45Z")

</div>

ruby 1.8.7 (2008-08-11 patchlevel 72) [i486-linux]

> > 2000-1992.2  
> > =\> 7.79999999999995

---

<div class="post-metadata">

**Author:** ![S2S2S2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f08c70/32.png) [@S2S2S2](https://www.ruby-forum.com/u/S2S2S2)\
**Post date:** [March 7, 2009, 12:11am UTC](https://www.ruby-forum.com/t/2000-1992-2-is-this-a-known-bug/162224/2 "2009-03-07T00:11:37Z")

</div>

2009/3/6 S2 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> ruby 1.8.7 (2008-08-11 patchlevel 72) [i486-linux]
> 
> > > 2000-1992.2  
> > > =\> 7.79999999999995

[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:** ![S2S2S2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f08c70/32.png) [@S2S2S2](https://www.ruby-forum.com/u/S2S2S2)\
**Post date:** [March 7, 2009, 12:12am UTC](https://www.ruby-forum.com/t/2000-1992-2-is-this-a-known-bug/162224/3 "2009-03-07T00:12:47Z")

</div>

> > > 2000-1992.2  
> > > =\> 7.79999999999995

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

Regards,  
Rimantas

---

<div class="post-metadata">

**Author:** ![S2S2S2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f08c70/32.png) [@S2S2S2](https://www.ruby-forum.com/u/S2S2S2)\
**Post date:** [March 7, 2009, 4:44am UTC](https://www.ruby-forum.com/t/2000-1992-2-is-this-a-known-bug/162224/4 "2009-03-07T04:44:59Z")

</div>

2009/3/6 S2 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> ruby 1.8.7 (2008-08-11 patchlevel 72) [i486-linux]
> 
> > > 2000-1992.2  
> > > =\> 7.79999999999995

The use of inexact floating point numbers by default in Ruby (which  
follows C and most, but not all, other programming languages) is a  
design choice, not a bug. One might dispute whether or not its a good  
design choice (personally, I prefer Scheme’s numeric tower and exact  
numbers by default with conversion to inexact numbers only when an  
inexact operation is applied to an exact number or an explicit  
designation of an inexact number is given), but its a point about  
which people could debate endlessly, and since BigDecimal is at least  
in the standard library, its not a matter of whether Ruby can do exact  
math, but just how verbose the code is to do it.

ex:

require ‘bigdecimal’  
require ‘bigdecimal/math’

class BigDecimal

# BigDecimal.to\_s is ugly

def pretty  
digits, magnitude = to\_s.split(‘.’)[1].split(‘E’)  
magnitude=magnitude.to\_i  
digits.insert(0,‘0’\*[(1-magnitude),0].max)  
digits.insert([magnitude,1].min,‘.’)  
end  
end

include BigMath

(2000-BigDecimal(‘1992.2’)).pretty  
=\> “7.8”

---

<div class="post-metadata">

**Author:** ![S2S2S2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f08c70/32.png) [@S2S2S2](https://www.ruby-forum.com/u/S2S2S2)\
**Post date:** [March 7, 2009, 4:56am UTC](https://www.ruby-forum.com/t/2000-1992-2-is-this-a-known-bug/162224/5 "2009-03-07T04:56:05Z")

</div>

On Fri, Mar 6, 2009 at 7:42 PM, Christopher D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> design choice, not a bug. One might dispute whether or not its a good  
> require ‘bigdecimal’  
> end
> 
> include BigMath
> 
> (2000-BigDecimal(‘1992.2’)).pretty  
> =\> “7.8”

Or, while that works for the case presented, if you want it to work  
right in general:

class BigDecimal  
def pretty  
return to\_i.to\_s if self==to\_i  
digits, magnitude = to\_s.split(‘.’)[1].split(‘E’)  
magnitude = magnitude.to\_i  
digits.insert(0,‘0’\*[(1-magnitude),0].max)  
digits.insert([magnitude, 1].max,‘.’)  
end  
end

---

<div class="post-metadata">

**Author:** ![S2S2S2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f08c70/32.png) [@S2S2S2](https://www.ruby-forum.com/u/S2S2S2)\
**Post date:** [March 7, 2009, 5:17am UTC](https://www.ruby-forum.com/t/2000-1992-2-is-this-a-known-bug/162224/6 "2009-03-07T05:17:42Z")

</div>

Hi,

2009/3/7 Christopher D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> design choice, not a bug. One might dispute whether or not its a good  
> require ‘bigdecimal’  
> end
> 
> include BigMath
> 
> (2000-BigDecimal(‘1992.2’)).pretty  
> =\> “7.8”

Why not use BigDecimal.to\_s(‘F’) ?

Refer to  
[http://www.ruby-doc.org/stdlib/libdoc/bigdecimal/rdoc/classes/BigDecimal.html#M000032](http://www.ruby-doc.org/stdlib/libdoc/bigdecimal/rdoc/classes/BigDecimal.html#M000032)

Regards,

Park H.

---

<div class="post-metadata">

**Author:** ![S2S2S2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f08c70/32.png) [@S2S2S2](https://www.ruby-forum.com/u/S2S2S2)\
**Post date:** [March 7, 2009, 5:28am UTC](https://www.ruby-forum.com/t/2000-1992-2-is-this-a-known-bug/162224/7 "2009-03-07T05:28:14Z")

</div>

On Fri, Mar 6, 2009 at 8:15 PM, Heesob P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Â Â digits.insert(0,‘0’\*[(1-magnitude),0].max)
> 
> Refer to [http://www.ruby-doc.org/stdlib/libdoc/bigdecimal/rdoc/classes/BigDecimal.html#M000032](http://www.ruby-doc.org/stdlib/libdoc/bigdecimal/rdoc/classes/BigDecimal.html#M000032)

Because that would be sane?

No, seriouly, I knew there was a BigDecimal formatter somewhere that  
I’d seen (and even used, though not recently), but I thought it was  
external. Clearly, it’s better to use that than to try to reinvent the  
wheel.
