
9.3 - (2 * 4.5) => 0.3000000000000007 I expected 0.3 ? -- Иван Драголов dragolov.net GSM: 0888 63 19 46 GSM за SMS: 0878 82 83 93 facebook.com/ivan.dragolov twitter.com/dragolov

Fun with floating point!
# ghci
GHCi, version 7.4.2: http://www.haskell.org/ghc/ :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Prelude> 9223372036854775807.0 == 9223372036854775808
True
http://en.wikipedia.org/wiki/Floating_point#Accuracy_problems
On Wed, Jan 16, 2013 at 7:25 AM, ivan dragolov
9.3 - (2 * 4.5) => 0.3000000000000007
I expected 0.3
?
-- Иван Драголов dragolov.net
GSM: 0888 63 19 46 GSM за SMS: 0878 82 83 93 facebook.com/ivan.dragolov twitter.com/dragolov
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

This may be of interest if you want full-precision decimals: http://www.haskell.org/haskellwiki/Libraries_and_tools/Mathematics#Decimal_n... On Wed, Jan 16, 2013 at 7:30 AM, Patrick Mylund Nielsen < haskell@patrickmylund.com> wrote:
Fun with floating point!
# ghci GHCi, version 7.4.2: http://www.haskell.org/ghc/ :? for help Loading package ghc-prim ... linking ... done. Loading package integer-gmp ... linking ... done. Loading package base ... linking ... done. Prelude> 9223372036854775807.0 == 9223372036854775808 True
http://en.wikipedia.org/wiki/Floating_point#Accuracy_problems
On Wed, Jan 16, 2013 at 7:25 AM, ivan dragolov
wrote: 9.3 - (2 * 4.5) => 0.3000000000000007
I expected 0.3
?
-- Иван Драголов dragolov.net
GSM: 0888 63 19 46 GSM за SMS: 0878 82 83 93 facebook.com/ivan.dragolov twitter.com/dragolov
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

This is a rounding error. It will happen in any language due to the
imprecision of floats; for example, using Ruby:
$ irb
1.9.3-p286 :001 > 9.3 - (2 * 4.5)
=> 0.3000000000000007
1.9.3-p286 :001 > ^D
$
Read this:
http://floating-point-gui.de/
On Wed, Jan 16, 2013 at 7:25 AM, ivan dragolov
9.3 - (2 * 4.5) => 0.3000000000000007
I expected 0.3
?
-- Иван Драголов dragolov.net
GSM: 0888 63 19 46 GSM за SMS: 0878 82 83 93 facebook.com/ivan.dragolov twitter.com/dragolov
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- Regards, Austin

Prelude> import Data.Ratio
Prelude Data.Ratio> 93 % 10 - (2 * 9 % 2)
3 % 10
Floating point sucks, avoid it if you can.
Thanks
Tom Davie
On 16 Jan 2013, at 13:25, ivan dragolov
9.3 - (2 * 4.5) => 0.3000000000000007
I expected 0.3
?
-- Иван Драголов dragolov.net
GSM: 0888 63 19 46 GSM за SMS: 0878 82 83 93 facebook.com/ivan.dragolov twitter.com/dragolov _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

On 16/01/2013 13:55, Tom Davie wrote:
Prelude> import Data.Ratio Prelude Data.Ratio> 93 % 10 - (2 * 9 % 2) 3 % 10
Or even just a type annotation: Prelude> (9.3 - (2 * 4.5)) :: Rational 3 % 10
On 16 Jan 2013, at 13:25, ivan dragolov
mailto:ivan@dragolov.net> wrote: 9.3 - (2 * 4.5) => 0.3000000000000007
I expected 0.3
-- David Turner Senior Consultant Tracsis PLC Tracsis PLC is a company registered in England and Wales with number 5019106.

You should read
http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.22.6768
If you want to be efficient with floats. Language independent.
Luis
On Wed, Jan 16, 2013 at 2:25 PM, ivan dragolov
9.3 - (2 * 4.5) => 0.3000000000000007
I expected 0.3
?
-- Иван Драголов dragolov.net
GSM: 0888 63 19 46 GSM за SMS: 0878 82 83 93 facebook.com/ivan.dragolov twitter.com/dragolov
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

On 13-01-16 10:04 AM, Luis Cabellos wrote:
You should read http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.22.6768
If you want to be efficient with floats. Language independent.
And also http://floating-point-gui.de/

On Wednesday 16 January 2013, 15:25:15, ivan dragolov wrote:
9.3 - (2 * 4.5) => 0.3000000000000007
I expected 0.3
?
Prelude Text.FShow.RealFloat> FD 9.3 9.300000000000000710542735760100185871124267578125 The closest Double to 9.3 is somewhat larger than 9.3. Since the first two significant digits are enough to distinguish it from the next smaller number, only those are delivered by the Show instance. If you subtract 9 from that, you get, unsurprisingly, Prelude Text.FShow.RealFloat> it - 9 0.300000000000000710542735760100185871124267578125 which is not the closest Double value to 0.3 (in fact, there are 12 Double values between that and the closest Double to 0.3). To distinguish that number from its neighbours, 16 significant digits are necessary, hence so many are displayed.

Welcome to IEEE floating point math!
Wikipedia has a good article on the specification. If you want exact
fractional numbers for your own purposes, there are a number of ways around
the limits of floats.
Rational will represent numbers internally as fractions, and still allow
you to use the same syntax as above. The result will be "3 % 10", which you
read as the fraction 3 / 10.
If you also want irrational numbers, things get tricky. CReal will do it,
but has some limitations.
On Jan 16, 2013 8:26 AM, "ivan dragolov"
9.3 - (2 * 4.5) => 0.3000000000000007
I expected 0.3
?
-- Иван Драголов dragolov.net
GSM: 0888 63 19 46 GSM за SMS: 0878 82 83 93 facebook.com/ivan.dragolov twitter.com/dragolov
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
participants (9)
-
Albert Y. C. Lai
-
Austin Seipp
-
Daniel Fischer
-
David Turner
-
ivan dragolov
-
Joe Q
-
Luis Cabellos
-
Patrick Mylund Nielsen
-
Tom Davie