ruby 1.9+ , floats, and decimal

B

botp

0.2-0.1
=> 0.1
=> 1.1
=> 0.0999999999999999

gotcha!

ok, i know what you're thinking. this dead horse is double dead..

in the spirit of advancing to a better computing env, shouldn't it be
time for ruby to default to "real" decimal instead of float?

arguments:

1. ruby using the dot decimal notation, for sure that is a decimal
notation (and ruby is not c)
2. we remove 99% of deci gotchas
3. there is always #to_f. we can put it to better use (if you want
speed and not precision).
4. i wont ask again and again ;-)

kind regards -botp
ps: this is not th*u*nk. LOL :))
 
C

Christopher Dicely

=> 0.1

=> 1.1

=> 0.0999999999999999

gotcha!

ok, i know what you're thinking. this dead horse is double dead..

in the spirit of advancing to a better computing env, shouldn't it be
time for ruby to default to "real" decimal instead of float?

I think decimal floating point (as supported in the 2008 version of
IEEE 754) would
be a good idea. OTOH, to reduce the potential impact on existing code
-- including
on performance, where binary floating point, since it has more
prevalent hardware
support, is likely to outperform in most cases -- it might be better
to introduce a
simple syntax for decimal floating point literals, e.g. an integer or
floating point
expression with a trailing (no intervening whitespace) "d" like 1.0d
would be treated
as a decimal floating point value.
 
G

George Neuner

I think decimal floating point (as supported in the 2008 version of
IEEE 754) would be a good idea.

The decimal formats in 754-2008 are *storage* formats only ... the
math specification is still binary.

OTOH, to reduce the potential impact on existing code -- including
on performance, where binary floating point, since it has more
prevalent hardware support, is likely to outperform in most cases

More prevalent than what? There is no commercial architecture that
has a decimal FPU. Even integer BCD support is rare now.

-- it might be better to introduce a simple syntax for decimal
floating point literals, e.g. an integer or floating point expression
with a trailing (no intervening whitespace) "d" like 1.0d would be
treated as a decimal floating point value.

Just adding syntax won't help - you'd need to automagically activate
the BigDecimal class as well.

George
 
G

George Neuner

in the spirit of advancing to a better computing env, shouldn't it be
time for ruby to default to "real" decimal instead of float?

arguments:

1. ruby using the dot decimal notation, for sure that is a decimal
notation (and ruby is not c)
2. we remove 99% of deci gotchas
3. there is always #to_f. we can put it to better use (if you want
speed and not precision).

Default behavior? I don't know whether it's a good idea or not. On
one hand, it supports the principle of least surprise for newbies ...
but on the other hand it keeps legions of programmers clueless about
reality.

Lisp defaults to precise arithmetic but falls back on imprecise binary
floating point under a whole bunch of corner conditions ... and
imprecision is contagious ... any operation that involves an imprecise
operand produces an imprecise result. The Lisp standard committee
spent a very great deal of time defining the conditions under which
arithmetic should produce precise or imprecise results. But the rules
are many and too confusing ... I've seen experienced Lisp programmers
panic when they trigger an imprecise result, or when they try to
integrate Lisp with libs in other languages (C, Fortran, etc.) that
don't have precise arithmetic.

I can see supporting parallel numeric towers - one precise and an
imprecise, performance oriented one - and letting the programmer
decide which to use. But in that case, I think there should only be
explicit conversions between them.

But I also think programmers need to understand how floating point
hardware operates because, if they become professional, sooner or
later they will have to deal with it.

George
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top