fract() of negative - unexpected
-
https://fuzearena.com/help/view/fract
It seems thatfract(-1.1) = 0.9
I expected:
fract(-1.1) = 0.1
Is that a bug?
-
@spikey I would have thought so too but apparently the fractional part of a negative number is not straight forward.
According to wikipedia the fractional part of -1.3 can be 0.7, 0.3 or -0.3 !
-
Ah. I See, I just found out that my program was failing because of fract(-0.0) = 1
test program output:
z=0.400000 fract(z)= 0.4000000 z=0.300000 fract(z)= 0.3000000 z=0.200000 fract(z)= 0.2000000 z=0.100000 fract(z)= 0.1000000 z=-0.000000 fract(z)=1.000000 z=-0.100000 fract(z)=0.900000 z=-0.200000 fract(z)=0.800000 z=-0.300000 fract(z)=0.700000 z=-0.400000 fract(z)=0.600000 z=-0.500000 fract(z)=0.500000 fract(-0.000000)=0.000000
example code:
z = 0.5 for i = 0 to 10 loop z = z - 0.1 fz = fract(z) print("z=", z, " fract(z)= ", fz, "\n") update() repeat test = fract(-0.000000) print("fract(-0.000000)=",test) update() loop repeat
But something is wrong here, because if I run
print(fract(-0.000000))
I get0.000000
and during the for loop I get1.000000
.
Is it to early in the morning? I don't see the why. -
@spikey that is strange I will have a look
-
@spikey There are a couple of strange things here. Why does it think that 0 is negative (I am pretty sure that it shouldn't be) and why does it behave differently inside and outside of the loop?
I will raise an issue. In the meantime fract(abs(x)) seems to behave as you want
-
Oh, thats perfect. Thanx.
-
I'm guessing the -0.0 and +0.0 are an artefact of the floating point format (it has both a + and - 0.0 due to how it works) and Fuze is taking your "-0.00" literal and converting it to the +0.0 instead whilst the 0.1 - 0.1 is giving -0.0 because of precision limitations in the floating point format.
fract() is then handling -0.0 and +0.0 differently, which may be a bug?