Re: div bug
Re: div bug
- Subject: Re: div bug
- From: Doug McNutt <email@hidden>
- Date: Tue, 4 Aug 2009 05:09:35 -0600
At 09:12 +0200 8/4/09, julifos wrote:
>Just as someone found out it was OK mixing soda with chocolate, Mr. Bansemer
>found this funny bug. Please, check it out and report!
>
>set n to 1.015 * 100 --> 101.5
>set n to n + 0.5 --> 102.0
>n div 1 --> 101 --> (!)
>
>The workaround for the "affected routines" would be coercing n to "small
>real" (if you wish to preserve decimals) before using div.
I think you'll find the answer in the basics of IEEE float as a format for floating decimal numbers. The common fraction 1/10 is simply not exactly expressible as a float. It is
0.00011001100110011. . . .continuing forever just as 1/3 is
0.33333... continuing forever in decimal arithmetic.
When you truncate to the 52 bits of a double float something is always lost.
That binary representation of 1.015 is not exact and is likely to be a little (of order 10^-15) smaller than the decimal value.. Add multiply by 100 (integers are exact until they exceed 10^15 or so) and add 1/2 (which is exact) to it and the result will be a tiny bit less than 102.000. Now do an integer division of a float by an integer and it will start with discarding the fractional part of the float. That is a little bit less than one but the whole non-integer part gets tossed. It's truncation, not rounding.
Applescript's formatting for decimal display leaves a bunch to be desired. It you could tell AppleScript to display the float to 16 digits the effect would be apparent.
A better work around would to redefine your units so that the original number would be 1015 (multiplying by 1000) then 1015 * 100 -> 101500; add 500 to get 102000; then div by 1000. It's those decimal fractions that create the problem. 1/2, 1/4, 1/8, 1/16 as in the Imperial measurement systems work better in computers. Think 8 bits in a buck.
A homework question: Are there more un-expressible fractions in decimal or in binary?
--
Applescript syntax is like English spelling:
Roughly, though not thoroughly, thought through.
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden
References: | |
| >div bug (From: julifos <email@hidden>) |