Log In  


Hi, I have a trouble with floating point arithmetic.

function _draw()
	cls()
	print( 256 / 65536 * 256 )
	print( 256 / (256*256) )
end

This snipet output "-2" and "32768".
Why this snipet dosn't output "1" and "0.00390625" ?

I try same thing on lua demopage.
https://www.lua.org/cgi-bin/demo
It outputs "0.00390625".

Thanks.



65536 overflows. The largest number PICO-8 can handle is 32767.99999


PICO-8 doesn't use the same number representation as regular Lua, so the Lua demo page exhibits different behavior.

Lua's numbers are 64-bit floating-point numbers. However, PICO-8's numbers are fixed-point, with 16 bits before the decimal point (-32767 to 32768), and 16 bits after the decimal points.


@Xii, @tobiasvl Thanks!!

I understood that limitation.


1

This thread helped me with a problem I was having: I was using rnd(65536) in a program, and it ran fine in the development environment; but when I exported the app to HTML, I kept getting zero as my random value.
Thanks!


1

Ah ! This actually answers the question on why my earlier code of counting high numbers failed only in the BBS but not in immediate cart.

https://www.lexaloffle.com/bbs/?pid=68949

And later when I recoded it where no actual high numbers were used, it didn't:

https://www.lexaloffle.com/bbs/?pid=69113

Thanks, WaterSplash ! That was very helpful.

It's probably not a good idea to skirt 32767 or -32767, much like sailing ships reaching the ends of the earth.

There be monsters ...



[Please log in to post a comment]