Re: BIG number as string
Re: BIG number as string
- Subject: Re: BIG number as string
- From: Deivy Petrescu <email@hidden>
- Date: Sat, 21 Dec 2002 22:00:19 -0500
At 13:43 -0800 12/21/02, John W Baxter wrote:
OK, here's a simple proof-of-concept of an approach very different from
those proposed. Useful for Mac OS X, recent enough to have shipped with
Python, or with Python installed by you in command line form.
Actually John's solution is valid for any OS since python is
available for "classic" Mac OSes.
I believe John's solution is actually the only solution that will
provide an "accurate" result.
Python handles numbers better, in general.
After John proposed the Python solution , I decided to use OS X and
came up with the following very simple script:
-- requires OS X
set z to "123456798"
set z1 to "876543392"
set k to do shell script "awk 'BEGIN {print " & z & "*" & z1 & "} END{}'"
--108215240484378816
However, it stops there. It is not very precise for larger numbers,
while python is. (see note at the bottom)
On the other hand if the number is not larger than 10^18 or 10^19
then, this script is simpler and faster albeit being X only.
For an AS vanilla solution, I'd use Andy's idea of the error but
instead I'd go for display dialog. This script takes care of
negative number and international "," convention. It does not,
however take care of very large exponents and one should throw a
dialog sayint it is unreasonable for powers much larger than ...
--Vanilla AS
set z to -123456798 * 8.765433921E+9
{z, ndigits(z)}
to ndigits(num)
set sig to ""
if num < 0 then set sig to "-" as string
set num to (num ^ 2) ^ (1 / 2)
set num to text returned of (display dialog "" default answer
num giving up after 1)
set myzeros to "000000000000000000000"
set k to (offset of "e" in num) + 1
if k = 1 then return num
set sep to character 2 of (0.1 as string)
set n to (characters (k + 1) thru -1 of num as string) as number
log num
log n
set zas to ((character 2 of num) & (characters 4 thru (k - 2)
of num)) as string
log zas
if character k of num is "-" then return (sig & "0" & sep &
characters 2 thru (n - 1) of myzeros) & zas as string
return (sig & characters 1 thru (n + 1) of (zas & myzeros)) as string
end ndigits
--{-1.08215240496724E+18, "-1082152404967245000"}
NOTE:
On terminal:
localhost:~] deivy% awk 'BEGIN {print 1082152022374638 * 87654321 } END{}'
--> 94855300740025694552064
[localhost:~] deivy% python teste.py
--> 94855300740025701510798
where teste.py is "print 1082152022374638 * 87654321"
--
Deivy Petrescu
http://www.dicas.com
_______________________________________________
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.