logicalTrunc
FlexibleLearning at aol.com
FlexibleLearning at aol.com
Tue Feb 12 02:20:39 EST 2008
Eric wrote:
> function logicalTrunc n
> if n is not a number then return "NAN"
> return n - (n mod 1)
> end logicalTrunc
Sorry, Eric... MOD suffers from the same same floating point issue as TRUNC:
get (36 - 34.1) * 100 --> 190, good
put it - (it MOD 1) --> 189, bad
Avoiding them obtains the expected answer:
get logicalTrunc ((36 - 34.1) * 100) --> 190, good
function logicalTrunc n
if n is not a number then return "NAN"
set the itemDel to "."
return item 1 of n
end logicalTrunc # by Kay C Lan
or
function logicalTrunc n
if n is not a number then return "NAN"
get offset(".", n)
if it > 0 then
delete char it to -1 of n
end if
return n
end logicalTrunc # by Richard Gaskin
Bottom line:
Use TRUNC and MOD with single numbers, not with calculations
/H
More information about the use-livecode
mailing list