Re: Convert long number to string
Re: Convert long number to string
- Subject: Re: Convert long number to string
- From: Martin Orpen <email@hidden>
- Date: Mon, 19 Apr 2004 01:23:11 +0100
on 18/4/04 7:41 am, Gnarlodious at email@hidden wrote:
>
Not for a Forthhead like me. Graff, are you saying the 'echo' command piles
>
a bunch of stuff on a stack and 'bc' evaluates it all endfirst?
You need to use "echo" and then pipe the commands into bc.
If you tried:
do shell script "bc 10/3"
The bc would try to run the statements it found in a file called "10/3".
I don't think it does it "endfirst" - it just does the maths in the order
that you'd expect.
The example I posted earlier was a bit longwinded, but you can easily reach
the limits of AS's number handling capabilities.
For example, the simplest of *prime* tests:
set n to (text returned of (display dialog "Enter an integer to test for
primeness:" default answer "")) as number
set x to (2 ^ n) - 2
if (x mod n) = 0 then
display dialog "Probably Prime"
else
display dialog "Not Prime"
end if
AS will only work properly if n is less than 58, whereas a bc version will
just keep on going:
set n to (text returned of (display dialog "Enter an integer to test for
primeness:" default answer "")) as number
set x to do shell script "echo '(2 ^" & n & ")-2' | bc"
set y to do shell script " echo '" & x & "%" & n & "' | bc"
if y = "0" then
display dialog "Probably Prime"
else
display dialog "Not Prime"
end if
Although you'll have to wait a while when you start testing 6 or 7 digit
numbers - and I've had to "sudo kill" bc a few times so that I could stop
and save a script.
The other benefit (or gotcha) when using bc with AS is that you need to
specify the number of decimal places that you'd like returned using "scale":
do shell script "echo '10/3' | bc"
--> "3"
do shell script "echo 'scale=20; 10/3' | bc"
--> "3.33333333333333333333"
Regards
--
Martin Orpen
Idea Digital Imaging Ltd -- The Image Specialists
http://www.idea-digital.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.