Forum: Ruby 2000-1992.2 - is this a known bug?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
B0ecb1471cd22155fd15fb3b873fd8fa?d=identicon&s=25 S2 (Guest)
on 2009-03-07 00:05
(Received via mailing list)
ruby 1.8.7 (2008-08-11 patchlevel 72) [i486-linux]

>> 2000-1992.2
=> 7.79999999999995
31e038e4e9330f6c75ccfd1fca8010ee?d=identicon&s=25 Gregory Brown (Guest)
on 2009-03-07 00:11
(Received via mailing list)
2009/3/6 S2 <some.r@ndom.mail.invalid.net>:
> 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
Ec5a599777854c540fd102ef4691fe10?d=identicon&s=25 Rimantas Liubertas (Guest)
on 2009-03-07 00:12
(Received via mailing list)
>>> 2000-1992.2
> => 7.79999999999995

It is known non-bug:
http://docs.sun.com/source/806-3568/ncg_goldberg.html

Regards,
Rimantas
D7463bd611f227cfb2ef4da4a978a203?d=identicon&s=25 Christopher Dicely (Guest)
on 2009-03-07 04:44
(Received via mailing list)
2009/3/6 S2 <some.r@ndom.mail.invalid.net>:
> 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"
D7463bd611f227cfb2ef4da4a978a203?d=identicon&s=25 Christopher Dicely (Guest)
on 2009-03-07 04:56
(Received via mailing list)
On Fri, Mar 6, 2009 at 7:42 PM, Christopher Dicely <cmdicely@gmail.com>
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
666b4e17b4bb0e2d999037a25f65a7cb?d=identicon&s=25 Heesob Park (phasis)
on 2009-03-07 05:17
(Received via mailing list)
Hi,

2009/3/7 Christopher Dicely <cmdicely@gmail.com>:
> 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/r...


Regards,

Park Heesob
D7463bd611f227cfb2ef4da4a978a203?d=identicon&s=25 Christopher Dicely (Guest)
on 2009-03-07 05:28
(Received via mailing list)
On Fri, Mar 6, 2009 at 8:15 PM, Heesob Park <phasis@gmail.com> wrote:
>>
>>
>>    digits.insert(0,'0'*[(1-magnitude),0].max)
>
> Refer to 
http://www.ruby-doc.org/stdlib/libdoc/bigdecimal/r...

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.
This topic is locked and can not be replied to.