b3w4r3 Posted May 10, 2012 Share Posted May 10, 2012 I ran across this while damaging the players charisma, then restoring it. Charisma is damaged by (.20), and the variable cha is modified to track the ammount, so cha = (-.20), or any multiple of .2 really as that's just one example. So when I restore this value I restore it (.20) every second until (cha == 0). The cha variable prints in console as (-0.00), then (.2) This the code that should stop it at 0... if cha < 0 set cha to cha + .2 elseif cha >= 0 do other stuff But since cha is equal to -0.00 the first check for < 0 still runs to the point where cha is eventually .2 before the elseif runs. I even tried to put if cha < -0 That didn't change anything. The solution for the time being is this... if cha < -.1 set cha to cha + .2 elseif cha >= -.1 do other stuff So the engine spits out this -0.00, which it then can't identify as 0.00 Maybe I'm just missing something obvious, or someone out there knows why this happens? Link to comment Share on other sites More sharing options...
rickerhk Posted May 12, 2012 Share Posted May 12, 2012 An artifact of floating point math, apparently. http://en.wikipedia.org/wiki/Signed_zeroThat was news to me too ;) Can you use this instead? if cha >=0 do other stuff else set cha to cha + .2 endif Link to comment Share on other sites More sharing options...
b3w4r3 Posted May 12, 2012 Author Share Posted May 12, 2012 Well the code you posted would still result in cha being set to .2 because -0.00 would go to else, and it needs to stop at 0. My solution works, the problem is that cha gets stored as -0.00 and displays that way in my menu (which just looks odd). I figured a way to detect the -0, and set to to 0 without any sign. It' a workaround but it's the only option I tried that has worked. Basically I run this on the close of the script to correct the signed 0... if cha < 0 && cha > -.2 set cha to 0 endif I saw that signed zero page too, and I guess it makes sense. It's just strange that the game considers it less than zero but won't recognize it in a direct comparison like if cha == -0 or if cha < -0 Probably could have set cha to cha + 1 then -1 before the comparison to zero to remove the sign. I may still try that, it seems more efficient that using the if comparison on closing... Well always learning new things writing scripts. There might be instances where this info could be useful. It could tell you if a floating point variable has been modified up from negative numbers when it is passing zero. This can really make you crazy. Link to comment Share on other sites More sharing options...
Cyberlazy Posted May 12, 2012 Share Posted May 12, 2012 Never trust floating point to be accurate! never use == either. Or 0"if cha < 0 && cha > -.2" Wrong"if cha < 0.00001 && cha > -.19999" Right Floating point is where 1-1 = 0.000001. Or maybe -0.00001. or 0. or -0.1+1 = anywhere from 1.99999 to 2.00001 (Well ok floating point is a little more accurate, but the errors can accumulate) Link to comment Share on other sites More sharing options...
b3w4r3 Posted May 12, 2012 Author Share Posted May 12, 2012 I understand there are some issues with floating point numbers now, and how they are stored in a computer. The first code you posted as wrong is working though, it is only for catching -0, but maybe changing the second bit to -.1 would avoid any confusion with .2 which is what my modifier number is. I don't think the 0 needs to be adjusted, but I will keep an eye on it. On a side note adding 1 then subtracting 1 from the -0 removes the sign as I expected, but for now I'm going to stick with using the workaround I posted above at the close of the script. Thanks for the information. Link to comment Share on other sites More sharing options...
Quetzlsacatanango Posted May 16, 2012 Share Posted May 16, 2012 Add zero to it twice so it will be positive 0 :whistling: :whistling: :whistling: Link to comment Share on other sites More sharing options...
Recommended Posts