Limiting floating point variables
3dogpottery
Posts: 78
I am pretty sure that something doesn't work because I tried:
[ A_Variable := -100_0.0 #> A_Variable <# 100_0.0]
Any ideas how to put limits on floating point variables?
Comments
Yep, there's no float limit operators.
Obviously you can do this:
But if you think about how floats actually work, you should be able to do something like this if you want to limit the magnitude in both directions:
Data types aren't a thing in Spin/Spin2. Which means that line of code is doing two integer evaluations, even though all three values are floats. To tell Spin2 to do a floating point operation means placing a dot on the end of each operator, eg:
Having said that, it seems there is no float operator for bounding, so that won't work. So this instead:
Because the exponent is in the higher order bits and the mantissa in the lower integer comparison operators also work on floating point values, at least for positive values. So if you want to limit a floating point variable to the range -1000.0 to +1000.0 then remove the sign, do a
variable <#= 1000.0
and restore the sign.Excellent explanations and examples everyone! Thank you.
That's exactly what my snippet from earlier does (using RMW on a bitfield)
Oh yes, that's true. I haven't understood it immediately and ignored it, sorry. Bitfields are very clever, sometimes.