Re: Runded Beyond Recognition
Re: Runded Beyond Recognition
- Subject: Re: Runded Beyond Recognition
- From: Paul Berkowitz <email@hidden>
- Date: Sat, 21 Feb 2004 18:00:03 -0800
On 2/21/04 4:27 PM, "ehsan saffari" <email@hidden> wrote:
>
>
I have a number result like this: 2.45305722033565E+6
>
I need to display it in non scientific form: 2453057.22033565, either as
>
a string or real.
>
>
set x to 2.45305722033565E+6
>
set y to text returned of (display dialog "Hack!" default answer x)
>
--> 2453057.22033565
>
>
The above works, but how can i get the coercion to work without display
>
dailog?
>
>
I tried Nigel Garvey's routines in aRounderRound 1.3, but the best I get
>
is 2453057.220336
>
>
Max number of decimal places in my number results will be 8.
You're not going to be able to do it. There are always going to be problems
with floating point numbers this precise (enormous) - they can't be
represented accurately in binary, which is how computers do their
calculations.
You can see the error already in the first step of any routine which tries
to get a string representation. I have my one, similar to Nigel's. The very
first line is always something like:
set x to x as string
Look what happens with a number this huge:
set X to 2.45305722033565E+6
set X to X as string
"2.453057220336E+6"
Right there, it rounded up that last 3 digits (565) to (600), undoubtedly as
part of whatever AppleScript has to do with the number at a low machine
level when converting it to string. All the rest of the routine, which works
just fine, is going to be wrong.
So it occurred to me to try to get just the whole number part of the number,
before the decimal point, subtract that from the complete number, and work
on the "tail". But subtraction has the same problem. Look what happens
before I can even get started:
set x to 2.45305722033565E+6
set y to X div 1
--> 2453057
set strx to (y as string) & "."
--> "2453057."
set z to x - y
-->0.220335649792
!! Not 0.2033565, but 0.220335649792.
I guess by specifying how many decimal places you want, you might get it
right to within +/- 1 if it "fills up" your decimal places.
I think this might work for your purposes:
set X to 2.45305722033565E+6
set y to X div 1
set strx to (y as string) & "."
set z to X - y
repeat 8 times
set z to (10 * z)
set w to z div 1
set strx to strx & w -- accumulating string
set z to z - w
end repeat
if z is greater than or equal to 0.5 then
set n to 0
set c to 1 -- initialize
repeat while c = (10 ^ n)
set n to n + 1
set c to (text -n thru -1 of strx) as integer
set c to c + 1 -- (rounding up)
end repeat
set strx to (text 1 thru -(n + 1) of strx) & c
end if
return strx
--"2453057.22033565"
--
Paul Berkowitz
_______________________________________________
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.