9.3 - (2 * 4.5) => 0.3000000000000007

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

9.3 - (2 * 4.5) => 0.3000000000000007

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

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: 9.3 - (2 * 4.5) => 0.3000000000000007

Patrick Mylund Nielsen
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




On Wed, Jan 16, 2013 at 7:25 AM, ivan dragolov <[hidden email]> wrote:

9.3 - (2 * 4.5) => 0.3000000000000007

I expected 0.3

?

--
Иван Драголов
dragolov.net

GSM: <a href="tel:0888%2063%2019%2046" value="+46888631946" target="_blank">0888 63 19 46
GSM за SMS: <a href="tel:0878%2082%2083%2093" value="+46878828393" target="_blank">0878 82 83 93  
facebook.com/ivan.dragolov

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe



_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: 9.3 - (2 * 4.5) => 0.3000000000000007

Austin Seipp
In reply to this post by ivan dragolov
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 <[hidden email]> 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
> [hidden email]
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>



--
Regards,
Austin

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: 9.3 - (2 * 4.5) => 0.3000000000000007

Patrick Mylund Nielsen
In reply to this post by Patrick Mylund Nielsen
This may be of interest if you want full-precision decimals: http://www.haskell.org/haskellwiki/Libraries_and_tools/Mathematics#Decimal_numbers


On Wed, Jan 16, 2013 at 7:30 AM, Patrick Mylund Nielsen <[hidden email]> 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




On Wed, Jan 16, 2013 at 7:25 AM, ivan dragolov <[hidden email]> wrote:

9.3 - (2 * 4.5) => 0.3000000000000007

I expected 0.3

?

--
Иван Драголов
dragolov.net

GSM: <a href="tel:0888%2063%2019%2046" value="+46888631946" target="_blank">0888 63 19 46
GSM за SMS: <a href="tel:0878%2082%2083%2093" value="+46878828393" target="_blank">0878 82 83 93  
facebook.com/ivan.dragolov

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe




_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: 9.3 - (2 * 4.5) => 0.3000000000000007

Thomas Davie
In reply to this post by ivan dragolov
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 <[hidden email]> 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
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe


_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: 9.3 - (2 * 4.5) => 0.3000000000000007

David Turner
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 <[hidden email]
> <mailto:[hidden email]>> 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.

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: 9.3 - (2 * 4.5) => 0.3000000000000007

Luis Cabellos-2
In reply to this post by ivan dragolov
You should read 

If you want to be efficient with floats. Language independent.

Luis


On Wed, Jan 16, 2013 at 2:25 PM, ivan dragolov <[hidden email]> 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

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe



_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: 9.3 - (2 * 4.5) => 0.3000000000000007

Daniel Fischer
In reply to this post by ivan dragolov
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.

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: 9.3 - (2 * 4.5) => 0.3000000000000007

Albert Y. C. Lai
In reply to this post by Luis Cabellos-2
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/

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: 9.3 - (2 * 4.5) => 0.3000000000000007

Joe Quinn
In reply to this post by ivan dragolov

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" <[hidden email]> 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

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe


_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe