FP scripts
Dar Scott
dsc at swcp.com
Wed Apr 3 23:06:01 EST 2002
Lately, we have had some questions and explorations concerning
binary floating point numbers in Revolution.
Here are some functions that can be used to examine the floating
point representation. You can use them to log intermediate values
in confusing calculations. Or you can use them to learn about
floating point or to prove bugs.
function rawFP x
-- returns a string showing the binary values of the eight bytes
set numberFormat to "00000000.#"
put binaryEncode("d", x) into y
put empty into z
repeat for each char c in y
put (baseConvert(charToNum(c),10,2)+0) & " " after z
end repeat
return z
end rawFP
function FP x
-- returns a string in this form:
-- -1.000000000010000000000...0000 (binary) * 2^24 (decimal)
local bits
put binaryEncode("d", x) into y
put binaryDecode("B64",y,bits) into count
if char 1 of bits = "1" then put "-" into bin
put "1." & char 13 to -1 of bits & " (binary) " after bin
put "* 2^" & baseConvert(char 2 to 12 of bits,2,10)-1023 \
& " (decimal)" after bin
return bin
end FP
This last one does not indicate +inf, NaN and so on. I did it
second, so it is a little cleaner. It will have to be modified
to get the fixed point form used by Michael Mays in recent mail.
Dar Scott
More information about the use-livecode
mailing list