Re: Scientific notation
Re: Scientific notation
- Subject: Re: Scientific notation
- From: Tim Bumgarner <email@hidden>
- Date: Thu, 1 May 2003 23:58:45 -0400
You may find this interesting, given the following script (using a
simple Studio app with a single button):
on clicked theObject
set theNumber to 3.0E-4
log "start perl"
repeat 1000 times
do shell script "perl -e 'print " & theNumber & "'"
end repeat
log "end perl"
log "start applescript"
repeat 1000 times
my NumberToString(theNumber)
end repeat
log "end applescript"
end clicked
on NumberToString(bigNumber)
set bigNumber to bigNumber as string
if bigNumber does not contain "E" then return bigNumber
set {tids, AppleScript's text item delimiters} to {AppleScript's text
item delimiters, {"E"}}
try
set {basicReal, powersOfTen} to {bigNumber's text item 1,
(bigNumber's text item 2) as integer}
on error -- e.g. Python strings have lower-case "e", tids
case-sensitive
set AppleScript's text item delimiters to {"e"}
set {basicReal, powersOfTen} to {bigNumber's text item 1,
(bigNumber's text item 2) as integer}
end try
set AppleScript's text item delimiters to {"."}
try
set {integerPart, decimalPart} to basicReal's {text item 1, text item
2}
set decimalSign to "."
on error -- European
set AppleScript's text item delimiters to {","}
set {integerPart, decimalPart} to basicReal's {text item 1, text item
2}
set decimalSign to ","
end try
set AppleScript's text item delimiters to tids
if powersOfTen > 0 then
set n to count decimalPart
if powersOfTen is greater than or equal to n then
repeat (powersOfTen - n) times
set decimalPart to decimalPart & "0"
end repeat
set bigNumber to integerPart & decimalPart
else
set bigNumber to integerPart & decimalPart
set bigNumber to text 1 thru (powersOfTen + 1) of bigNumber &
decimalSign & text (powersOfTen + 2) thru -1 of bigNumber
end if
else -- negative index
set bigNumber to integerPart & decimalPart
repeat (-powersOfTen - 1) times
set bigNumber to "0" & bigNumber
end repeat
set bigNumber to "0" & decimalSign & bigNumber
repeat while bigNumber ends with "0"
set bigNumber to text 1 thru -2 of bigNumber
end repeat
if bigNumber ends with "." then set bigNumber to bigNumber & "0"
end if
return bigNumber
end NumberToString
Produces the following output:
2003-05-01 23:54:18.405 PopUpTest[2387] "start perl"
2003-05-01 23:55:12.416 PopUpTest[2387] "end perl"
2003-05-01 23:55:12.433 PopUpTest[2387] "start applescript"
2003-05-01 23:55:13.867 PopUpTest[2387] "end applescript"
Notice that it takes nearly one minute to execute the do shell script
1000 times, but only a little over a second and a half for executing
the NumberToString handler.
Food for thought...
Tim Bumgarner
AppleScript Engineering
On Thursday, May 1, 2003, at 11:39 PM, Jeffrey Mattox wrote:
>
Paul:
>
>
You do good work, but all that AS for such a simple task makes me
>
cringe. How about this instead:
>
>
set theNumber to 3.0E-4
>
do shell script "perl -e 'print " & theNumber & "'"
>
--> "0.0003"
>
>
Jeff
>
>
At 7:09 PM -0700 5/1/03, Paul Berkowitz wrote:
>
> On 5/1/03 5:24 PM, "Steve Roy" <email@hidden> wrote:
>
>
>
>> Hi,
>
>>
>
>> In a script, I'm getting small real numbers such as 3.0E-4. When I
>
>> turn them
>
>> to
>
>> string, I would like them to appear in normal notation "0.0003"
>
>> instead of
>
>> "3.0E-4". Is that possible?
>
>>
>
>
>
> my NumberToString(3.0E-4)
>
> --> "0.0003"
>
>
>
> on NumberToString(bigNumber)
>
> set bigNumber to bigNumber as string
>
> if bigNumber does not contain "E" then return bigNumber
>
> set {tids, AppleScript's text item delimiters} to
>
> {AppleScript's text
>
> item delimiters, {"E"}}
>
> try
>
> set {basicReal, powersOfTen} to {bigNumber's text item 1,
>
> (bigNumber's text item 2) as integer}
>
> on error -- e.g. Python strings have lower-case "e", tids
>
> case-sensitive
>
> set AppleScript's text item delimiters to {"e"}
>
> set {basicReal, powersOfTen} to {bigNumber's text item 1,
>
> (bigNumber's text item 2) as integer}
>
> end try
>
> set AppleScript's text item delimiters to {"."}
>
> try
>
> set {integerPart, decimalPart} to basicReal's {text item 1,
>
> text
>
> item 2}
>
> set decimalSign to "."
>
> on error -- European
>
> set AppleScript's text item delimiters to {","}
>
> set {integerPart, decimalPart} to basicReal's {text item 1,
>
> text
>
> item 2}
>
> set decimalSign to ","
>
> end try
>
> set AppleScript's text item delimiters to tids
>
> if powersOfTen > 0 then
>
> set n to count decimalPart
>
> if powersOfTen is greater than or equal to n then
>
> repeat (powersOfTen - n) times
>
> set decimalPart to decimalPart & "0"
>
> end repeat
>
> set bigNumber to integerPart & decimalPart
>
> else
>
> set bigNumber to integerPart & decimalPart
>
> set bigNumber to text 1 thru (powersOfTen + 1) of
>
> bigNumber &
>
> decimalSign & text (powersOfTen + 2) thru -1 of bigNumber
>
> end if
>
> else -- negative index
>
> set bigNumber to integerPart & decimalPart
>
> repeat (-powersOfTen - 1) times
>
> set bigNumber to "0" & bigNumber
>
> end repeat
>
> set bigNumber to "0" & decimalSign & bigNumber
>
> repeat while bigNumber ends with "0"
>
> set bigNumber to text 1 thru -2 of bigNumber
>
> end repeat
>
> if bigNumber ends with "." then set bigNumber to bigNumber &
>
> "0"
>
> end if
>
> return bigNumber
>
> end NumberToString
>
>
>
>
>
>
>
> --
>
> 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.
>
_______________________________________________
>
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.
_______________________________________________
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.