Re: BIG number as string
Re: BIG number as string
- Subject: Re: BIG number as string
- From: John W Baxter <email@hidden>
- Date: Sat, 21 Dec 2002 13:43:17 -0800
At 22:51 +0000 12/17/2002, julifos wrote:
>
'd like retrieve a string from a big-numbers-calculation, but I can't :-(
>
>
Eg, I'd like this:
>
>
55555 * 55555
>
--> 3086358025
>
--> rather than 3.086358025E+9
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.
-- Pick a path name, and get the forms we need--better duplicate prevention
desired
set fn to (path to temporary items folder as string) & "xxxx.py"
set fnp to quoted form of POSIX path of fn
-- Unix end of line
set newline to ASCII character of 10
-- Create a temporary file and write a Python program into it
set f to open for access file fn with write permission
write "print 55555 * 55555" & newline to f
close access f
-- Run the Python program and delete it
set ans to do shell script "/usr/bin/python " & fnp
do shell script "rm " & fnp
-- -ans is a string with the answer
ans
Clearly, the thing has to be parameterized one way or another (either write
a permanent Python script which takes command line input, or write a
handler which writes the needed Python script, or something which does the
parameterization another way), although for a few quickies just changing
the AppleScript script is enough.
This will work with larger numbers...change the write in the above script to
write "print 5555500 * 5555500" & newline to f
and the result is "30863580250000"
(I picked that problem because given the prior answer we know this one by
inspection.)
Modern Python versions automatically use long integers where
necessary...it's necessary here to hold the answer (the Python integer is
32 bits...the Python long integer can be as large as will fit in [virtual]
memory).
Enjoy.
--John
--
John Baxter email@hidden Port Ludlow, WA, USA
There is more than one way to do it.
_______________________________________________
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.