Log In  


This cartridge fails the assertion, which normally I would write off as a floating point bug, but pico-8 uses fixed point representation so this seems like an actual bug in pico-8 to me?

function _init()
 local x=0.6
 local y=0.3
 local sum=x+y
 assert_equal(sum,0.9)
end

function assert_equal(a,b)
 local s=a.." ("..tostr(a,true)..") does not equal "..b.." ("..tostr(b,true)..")"
 printh(s,"@clip")
 assert(a==b,s)
end

function _draw()
 cls(3)
end

The assertion fails, saying:
0.9 (0x0000.e665) does not equal 0.9 (0x0000.e666)

This is just one example, but this happens to me more often than not when trying to compare decimal numbers :(



1

That issue doesn't come from numbers being floating point. That issue comes from approximating between different number representations. 0.6 and 0.3 are both repeating number in binary, due to 0.1 being a repeating number. Unless you use a string representation instead, that will always be the case.


Oh interesting... yeah that makes perfect sense now, thank you. I guess I assumed that fixed-point decimals would be stored as a multiple of the smallest possible value (e.g. 0.3 in pico-8 would be stored as the 2's-complement representation of the decimal number 30000)



[Please log in to post a comment]