Is there any simple way to round a run-time variable floating point number to the nearest integer on the Propeller? I'm looking for something that will work like ROUND does for constants.
For positive numbers, add 0.5 to it before truncating to an integer; for negative numbers, whether you add or subtract 0.5 depends on whether conversion to an integer truncates up or down.
Ah, that explains it. I misunderstood and assumed the Propeller had floating point support built-in. So what does the Propeller on its own do with fractional results for math operations? Just truncate/discard them, or round them or what?
The Propeller Tool can do floating point calculations with constants, but the resulting values look like large 32-bit numbers at run-time because the Propeller Tool doesn't automatically convert them back to ordinary integers. It leaves them as floating point numbers which use the IEEE format (see here).
coryco2, the built in math operations are integer and the results are truncated to the next lower integer multiple. Even if you convert the result to float the truncation error can produce bad results. This happened to me with a ACos call which I pointed out in this thread.
Another thing to be careful about is that the Propeller will happily use integer operations on floating point numbers. Since Spin is loosely typed it won't complain, but the results will be wrong. If you look in that code in my thread you'll see I carefully convert back and forth when I need to. As Mike points out, you can pre-compute floating point constants using the Spin complier. I did that for various radian angle constants as they are all fractions of Pi.
Comments
-Phil
http://forums.parallax.com/showthread.php?127302-F32-Concise-floating-point-code-for-the-Propeller
and I like it. For that one you would use code like "rounded = f32.FRound( inputVal )"
-Phil
When you divide it truncates. Use the modulus operator (//) to get the fractional part.
http://forums.parallax.com/showthread.php?130486-Spin-Pop-Quiz-what-s-wrong-with-the-SetPosition-method.
Another thing to be careful about is that the Propeller will happily use integer operations on floating point numbers. Since Spin is loosely typed it won't complain, but the results will be wrong. If you look in that code in my thread you'll see I carefully convert back and forth when I need to. As Mike points out, you can pre-compute floating point constants using the Spin complier. I did that for various radian angle constants as they are all fractions of Pi.