Re: real comparison problems [Re: ASTest redux]
Re: real comparison problems [Re: ASTest redux]
- Subject: Re: real comparison problems [Re: ASTest redux]
- From: John W Baxter <email@hidden>
- Date: Sun, 29 Sep 2002 23:14:47 -0700
[Mark...see the stuff near the end. I don't think 0.7 is much like 1.91875.]
At 11:41 +0100 9/28/2002, has wrote:
>
John W Baxter wrote:
>
>
>>Oh, and if anyone has any ideas what I might do to compensate for AS's
>
>>stupid treatment of reals, eg:
>
>>
>
>> 0.7 * 0.7 = 0.49
>
>> --> false
>
>
>
>The way to avoid that particular aspect of the nature of binary floating
>
>point is not to compare floating point values for equality. Decide how
>
>much of a difference is appropriate to the task at hand to indicate "not
>
>equal", and compare that for greater than the absolute value of the
>
>difference of the two values.
>
>
Hi, yeah. However, I think that's supposed to be AS's job. The problem with
>
the above example is that _it's_ the one saying "0.7*0.7-->0.49", but then
>
declares it _not_equal_ when comparing that value to a literal 0.49. It'd
>
be different if "0.7*0.7" visibly returned "0.489999999999" - your solution
>
would work there - but it doesn't.
I don't think it's AppleScript's job to guess how big a difference is
acceptable for "equality" in a given problem. It's a human's job (and
sometimes quite hard).
0.7 happens to be a rather extreme example, since the nearest representable
value is quite far from the actual value.
Modern Python in the command interpreter does not round off when reporting
a result (it prints the so called repr of the value, whose job is to be
capable of reproducing the value when read in). In a print statement,
however, Python does round.
$python
Python 2.2 (#1, 07/14/02, 23:25:09)
[GCC Apple cpp-precomp 6.14] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>
>> point7 = 0.7
>
>> point49 = point7 * point7
>
>> point7
0.69999999999999996
>
>> .49
0.48999999999999999
>
>> point49
0.48999999999999994
>
>> print point7
0.7
>
>> print point49
0.49
>
>> print .49
0.49
>
>>
The change to this representation form--from this
# python
Python 1.5.2 (#1, Apr 3 2002, 18:16:26) [GCC 2.96 20000731 (Red Hat Linux
7.2 2 on linux-i386
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>
>> .7
0.7
>
>> .7*.7 == .49
0
>
>> .49
0.49
--caused consternation early, and still causes questions. But at least the
difference between 0.48999999999999999 and 0.48999999999999994
is visible now.
If anything, AppleScript (or a script debugger [1]) might adopt a similar
form...where the "bare" number is not rounded but
number as string
is. But then you have the silent coercions, as in
display dialog .7
which would show .7, probably, and cause yet more confusion.
Aside...as it happens,
>
>> .50 - .01
0.48999999999999999
(or the same as .49). That is certainly not guaranteed (or is it??...it is
true for the examples I've tried)...it does work for .49.
--John
[1] Actually, it looks as if Script Debugger (3.0.4) tries to and botches
the job: it shows the result of
set x to 0.7
as 0.7 in the result window in "Source" and "Best" form, and as 1.91875
(huh????) in AEPrint form. THAT does look like a bug (not
AppleScript...probably a problem with the wrong floating point
representation being used by the AEPrint library).
--
John Baxter email@hidden Port Ludlow, WA, USA
0.7 as taught in school
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.