Re: Int Function
Re: Int Function
- Subject: Re: Int Function
- From: Christopher Nebel <email@hidden>
- Date: Sun, 18 Aug 2002 19:23:24 -0700
On Sunday, August 18, 2002, at 05:24 PM, Chuck Ross wrote:
Can anyone give me an easy way to get the Int of a number, converting,
for instance, 6.1 into 6?
"round" from Standard Additions will do what you want. If you want to
truncate the number (e.g., 6.9 --> 6), use "toward zero" as the
rounding direction -- "down" will produce surprising results on
negative numbers. If you want the nearest integer (e.g., 6.1 --> 6,
6.9 --> 7), use "to nearest" (the default) or "as taught in school".
Read the dictionary entry for the difference.
Now, on a related but much more fiddly topic...
On Sunday, August 18, 2002, at 06:26 PM, Doug McNutt wrote:
set x to 3.45
set y to round (x)
set classofY to class of y
display dialog classofY
The dialog returns "long" but the EventLog is silent.
Do you have "show events" turned on? I get proper log lines for both
"round" and "display dialog". The "long" is not a mystery class --
it's the internal code for the regular "integer" class. Because of how
"display dialog" is implemented, passing it a class or enumeration
constant will show you the internal code, not the AppleScript name. If
you say "display dialog (classofY as string)", you'll get "integer".
set x to 2 ^ 29
set y to round (x)
set classofY to class of y
display dialog classofY
Returns "doub"
That isn't even defined in ASLG. Is it the same as IEEE double?
Similarly, "doub" is the internal code for "real". It's not defined in
the ASLG because it's not a language element.
set x to 2 ^ 24 + 0.5
set y to (round (x) as integer)
set classofY to class of y
display dialog classofY
generates error "cant make 1.6777. . .E7 into integer." ... 1.6777. .
.E7 is clearly less than 2^29 and ought to be coerceable to integer.
Well, there are two problems there. First, the "set y" line isn't
binding the way you think it is, since "as" has higher precedence than
the function call. It's actually doing "round (x as integer)", not
"(round x) as integer".
This brings us to the second problem, which is that AppleScript is
curiously fussy about coercing reals into integers -- it will only
allow it if the real has no fractional part at all. (Interestingly,
this restriction seems to exist in order to keep things like "item 1.5
of {1, 2}" from working. We're probably going to relax it.) Because x
has a 0.5 at the end, "x as integer" fails.
To make this work, throw in some extra parentheses to make it do the
round first. The "as integer" is completely unnecessary, though --
"round" always returns an integer if it can.
--Chris Nebel
AppleScript Engineering
_______________________________________________
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.