[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

2000-1992.2 - is this a known bug?

S2

3/6/2009 11:01:00 PM

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

>> 2000-1992.2
=> 7.79999999999995
6 Answers

Gregory Brown

3/6/2009 11:10:00 PM

0

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

Rimantas Liubertas

3/6/2009 11:11:00 PM

0

>>> 2000-1992.2
> => 7.79999999999995

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

Regards,
Rimantas
--
http://rim...

Christopher Dicely

3/7/2009 3:43:00 AM

0

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"

Christopher Dicely

3/7/2009 3:54:00 AM

0

On Fri, Mar 6, 2009 at 7:42 PM, Christopher Dicely <cmdicely@gmail.com> wro=
te:
> 2009/3/6 S2 <some.r@ndom.mail.invalid.net>:
>> ruby 1.8.7 (2008-08-11 patchlevel 72) [i486-linux]
>>
>>>> 2000-1992.2
>> =3D> 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
> =C2=A0# BigDecimal.to_s is ugly
> =C2=A0def pretty
> =C2=A0 =C2=A0digits, magnitude =3D to_s.split('.')[1].split('E')
> =C2=A0 =C2=A0magnitude=3Dmagnitude.to_i
> =C2=A0 =C2=A0digits.insert(0,'0'*[(1-magnitude),0].max)
> =C2=A0 =C2=A0digits.insert([magnitude,1].min,'.')
> =C2=A0end
> end
>
> include BigMath
>
> (2000-BigDecimal('1992.2')).pretty
> =3D> "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=3D=3Dto_i
digits, magnitude =3D to_s.split('.')[1].split('E')
magnitude =3D magnitude.to_i
digits.insert(0,'0'*[(1-magnitude),0].max)
digits.insert([magnitude, 1].max,'.')
end
end

Heesob Park

3/7/2009 4:16:00 AM

0

Hi,

2009/3/7 Christopher Dicely <cmdicely@gmail.com>:
> 2009/3/6 S2 <some.r@ndom.mail.invalid.net>:
>> ruby 1.8.7 (2008-08-11 patchlevel 72) [i486-linux]
>>
>>>> 2000-1992.2
>> =3D> 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
> =C2=A0# BigDecimal.to_s is ugly
> =C2=A0def pretty
> =C2=A0 =C2=A0digits, magnitude =3D to_s.split('.')[1].split('E')
> =C2=A0 =C2=A0magnitude=3Dmagnitude.to_i
> =C2=A0 =C2=A0digits.insert(0,'0'*[(1-magnitude),0].max)
> =C2=A0 =C2=A0digits.insert([magnitude,1].min,'.')
> =C2=A0end
> end
>
> include BigMath
>
> (2000-BigDecimal('1992.2')).pretty
> =3D> "7.8"
>
Why not use BigDecimal.to_s('F') ?

Refer to http://www.ruby-doc.org/stdlib/libdoc/bigdecimal/rdoc/cla...
ecimal.html#M000032


Regards,

Park Heesob

Christopher Dicely

3/7/2009 4:26:00 AM

0

On Fri, Mar 6, 2009 at 8:15 PM, Heesob Park <phasis@gmail.com> wrote:
> Hi,
>
> 2009/3/7 Christopher Dicely <cmdicely@gmail.com>:
>> 2009/3/6 S2 <some.r@ndom.mail.invalid.net>:
>>> ruby 1.8.7 (2008-08-11 patchlevel 72) [i486-linux]
>>>
>>>>> 2000-1992.2
>>> =3D> 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
>> =C2=A0# BigDecimal.to_s is ugly
>> =C2=A0def pretty
>> =C2=A0 =C2=A0digits, magnitude =3D to_s.split('.')[1].split('E')
>> =C2=A0 =C2=A0magnitude=3Dmagnitude.to_i
>> =C2=A0 =C2=A0digits.insert(0,'0'*[(1-magnitude),0].max)
>> =C2=A0 =C2=A0digits.insert([magnitude,1].min,'.')
>> =C2=A0end
>> end
>>
>> include BigMath
>>
>> (2000-BigDecimal('1992.2')).pretty
>> =3D> "7.8"
>>
> Why not use BigDecimal.to_s('F') ?
>
> Refer to http://www.ruby-doc.org/stdlib/libdoc/bigdecimal/rdoc/c...
gDecimal.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.