Some strange behavior

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

Some strange behavior

Mateusz Grotek
6 * 0.2 CTRL-P 1.2
1.2 - 1 CTRL-P 0.2
6 * 0.2 - 1 CTRL-P 0.2000000000000002

Squeak 4.1 #9957

bug or not?

Reply | Threaded
Open this post in threaded view
|

Re: Some strange behavior

Mateusz Grotek
Mateusz Grotek pisze:
> 6 * 0.2 CTRL-P 1.2
> 1.2 - 1 CTRL-P 0.2
> 6 * 0.2 - 1 CTRL-P 0.2000000000000002
>
> Squeak 4.1 #9957
>
> bug or not?
>
>
oops, ALT-P of course...

Reply | Threaded
Open this post in threaded view
|

Re: Some strange behavior

Bert Freudenberg
In reply to this post by Mateusz Grotek

On 28.10.2010, at 21:59, Mateusz Grotek wrote:

> 6 * 0.2 CTRL-P 1.2
> 1.2 - 1 CTRL-P 0.2
> 6 * 0.2 - 1 CTRL-P 0.2000000000000002
>
> Squeak 4.1 #9957
>
> bug or not?

Not.

- Bert -


Reply | Threaded
Open this post in threaded view
|

Re: Some strange behavior

Levente Uzonyi-2
On Thu, 28 Oct 2010, Bert Freudenberg wrote:

>
> On 28.10.2010, at 21:59, Mateusz Grotek wrote:
>
>> 6 * 0.2 CTRL-P 1.2
>> 1.2 - 1 CTRL-P 0.2
>> 6 * 0.2 - 1 CTRL-P 0.2000000000000002
>>
>> Squeak 4.1 #9957
>>
>> bug or not?
>
> Not.

You can find the explanation here: http://floating-point-gui.de/ . If you
really need exact results, then you can use Fractions:

6 * (1/5) - 1 "===> (1/5)"


Levente

>
> - Bert -
>
>
>

Reply | Threaded
Open this post in threaded view
|

Re: Some strange behavior

Hans-Martin Mosner
In reply to this post by Mateusz Grotek
Am 28.10.2010 21:59, schrieb Mateusz Grotek:

> 6 * 0.2 CTRL-P 1.2
> 1.2 - 1 CTRL-P 0.2
> 6 * 0.2 - 1 CTRL-P 0.2000000000000002
>
> Squeak 4.1 #9957
>
> bug or not?
>
>
>  
Not a bug. Welcome to the wonders of binary floating point arithmetic.
Binary float representation of decimals are inexact. The bit pattern you
get for 0.2 is different from the one that you get for 6 * 0.2 - 1.
You can look at the bit patterns by using the message #hex
(6 * 0.2) hex
(1.2 - 1) hex
(6 * 0.2 - 1) hex

This has nothing to do with Squeak, see the following C code for a
similar  effect:

#include <stdio.h>

main() {
    printf("%1.16f\n", 6 * 0.2);
    printf("%1.16f\n", 1.2 - 1);
    printf("%1.16f\n", 6 * 0.2 - 1);
}

The fact that Squeak prints 6 * 0.2 as 1.2 and not as 1.2000000000000002
is a result of the particular algorithm that is being used for printing
floats as decimal numbers.

Cheers,
Hans-Martin

Reply | Threaded
Open this post in threaded view
|

Re: Some strange behavior

Bert Freudenberg
On 28.10.2010, at 22:30, Hans-Martin Mosner wrote:

> The fact that Squeak prints 6 * 0.2 as 1.2 and not as 1.2000000000000002
> is a result of the particular algorithm that is being used for printing
> floats as decimal numbers.

(6 * 0.2) storeString

==> '1.2000000000000002'

- Bert -


Reply | Threaded
Open this post in threaded view
|

Re: Some strange behavior

Mateusz Grotek
In reply to this post by Hans-Martin Mosner

> The fact that Squeak prints 6 * 0.2 as 1.2 and not as 1.2000000000000002
> is a result of the particular algorithm that is being used for printing
> floats as decimal numbers.
>
> Cheers,
> Hans-Martin
>
>
Yes, that was something I wondered. Thanks.

Reply | Threaded
Open this post in threaded view
|

Re: Some strange behavior

Mateusz Grotek
In reply to this post by Bert Freudenberg

> (6 * 0.2) storeString
>
> ==> '1.2000000000000002'
>
> - Bert -
>

Yup, I should have check it first. Thanks.


Reply | Threaded
Open this post in threaded view
|

Re: Some strange behavior

LawsonEnglish
In reply to this post by Mateusz Grotek
On 10/28/10 2:46 PM, Mateusz Grotek wrote:

>> The fact that Squeak prints 6 * 0.2 as 1.2 and not as 1.2000000000000002
>> is a result of the particular algorithm that is being used for printing
>> floats as decimal numbers.
>>
>> Cheers,
>> Hans-Martin
>>
>>
> Yes, that was something I wondered. Thanks.
>
>
The moral is: don't use floating point math to do accounting...


Lawson

Reply | Threaded
Open this post in threaded view
|

Re: Some strange behavior

Darius Clarke

The moral is: don't use floating point math to do accounting...


Lawson


... unless you're a politician... ;-)

- Darius


Reply | Threaded
Open this post in threaded view
|

Re: Some strange behavior

Michael Haupt-3
In reply to this post by LawsonEnglish
Hi,

On 29 October 2010 02:57, Lawson English <[hidden email]> wrote:
> The moral is: don't use floating point math to do accounting...

believe it or not, I once received the most hilarious invoice. The
relevant part is attached. Note that this is a snippet taken out of a
real invoice I got in a box with some article I had ordered. *This is
real.* (Need to emphasise.)

See attachment. The text at the bottom says "amount remaining to be
paid", and the negative number suggests they owe me. ;-)

Well, I sent them a friendly e-mail, thanking them for their kindness
- after all they actually admitted they would take care of me and my
family for some time. Very generous. They didn't respond to my
question whether they would rather pay all at once or by installments.
Huh.

Best,

Michael



invoice-snippet.jpg (37K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Some strange behavior

Nicolas Cellier
In reply to this post by Bert Freudenberg
2010/10/28 Bert Freudenberg <[hidden email]>:

> On 28.10.2010, at 22:30, Hans-Martin Mosner wrote:
>
>> The fact that Squeak prints 6 * 0.2 as 1.2 and not as 1.2000000000000002
>> is a result of the particular algorithm that is being used for printing
>> floats as decimal numbers.
>
> (6 * 0.2) storeString
>
> ==> '1.2000000000000002'
>
> - Bert -
>
>

I would add that Squeak4.1 now gives your some insights about what is going on.
You can try to evaluate these expressions:
   (1/5) = 0.2.
   (1/5) asFloat = 0.2.
   0.2 asTrueFraction.

Nicolas