testing float if zero?
Rob v.d. berg
Posts: 89
Hi,
I have (may be) a simple question: say there are 2 buttons, up and down. By pressing the up button, a tens (0.1) must be add to a float. By pressing down a tens must be submit from the float, till the float is 0.0 (zero).
What is the 'normal' way to test a float if zero?
This code works but .......
OBJ
F32 : "F32"
if ina(buttonDown)
if floatVar > 900_000_000 'this works, but the must be a better one
floatVar := F32.Fsub(FloatVar, 0.1)
if ina(buttonUp)
floatVar := F32.Fadd(FloatVar, 0.1)
any help?
Rob
I have (may be) a simple question: say there are 2 buttons, up and down. By pressing the up button, a tens (0.1) must be add to a float. By pressing down a tens must be submit from the float, till the float is 0.0 (zero).
What is the 'normal' way to test a float if zero?
This code works but .......
OBJ
F32 : "F32"
if ina(buttonDown)
if floatVar > 900_000_000 'this works, but the must be a better one
floatVar := F32.Fsub(FloatVar, 0.1)
if ina(buttonUp)
floatVar := F32.Fadd(FloatVar, 0.1)
any help?
Rob
Comments
Jonathan
edit: if you just want to make sure the value never goes below 0, you could use:
floatVar := F32.FMax(FloatVar, 0.0)
thanks, with a integer and than multiply by 0.1, thats i good one!
Rob
if( (FloatVal & $8000_0000) <> 0 )
Or:
if( (FloatVal >> 31) == 1 )
You don't have to use the float library to check it this way, so it's faster.
Massimo
For example, when I use the DS1620 thermometer the units I work with are tenths of degrees. If I want to see if two temperature readings are more than a degree apart, I compare the difference to 10 (tenths of a degree) instead of 1 (degree).