Boolean to integer [Re: Currency Numbers]
Boolean to integer [Re: Currency Numbers]
- Subject: Boolean to integer [Re: Currency Numbers]
- From: "Nigel Garvey" <email@hidden>
- Date: Sat, 24 Jun 2006 14:00:05 +0100
I wrote on Thu, 22 Jun 2006 11:29:53 +0100:
>You could claw back some of the speed with this one-coercion version:
>
> y * (1 - 2 * ((y < 0) as integer))
Faster still would be to use a handler instead of the coercion:
on booleanToInteger(b)
if (b) then
1
else
0
end if
end booleanToInteger
y * (1 - 2 * (booleanToInteger(y < 0)))
This is plainly silly (but fun) in the context of getting the absolute
value of a number. But it may be handy to know that booleanToInteger() is
about three and a half times as fast as a boolean-to-integer coercion
and, of course, works on any system.
Incidentally, it seems that at some time between OS 9.2.2 and OS 10.2.8,
it became possible to coerce the integers 0 and 1 to _boolean_. Not that
anyone would want to, given that (n is 1) is about nine times as fast as
(n as boolean).
Hi, Gary. :)
NG
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden