PDA

View Full Version : Floating point division always giving similar fractional?

scurrier
11-17-2010, 03:22 AM
Hello all,

I am using fdiv to compute how far a fan has moved since the last fan blade was sensed. Here is some code:

lastedge and period are addresses marking longs that are updated with integer values by another cog. Lastedge is the number of clocks since the last blade was detected. Period represents the number of clocks between blades from the last cycle.

depth := f.fdiv( f.ffloat( cnt - long[lastedge] ),f.ffloat( long[period] ) )
debug.dec( f.frac( depth ) )

That debug line keeps outputting something like 1059194816. I can put my hand in front of the sensor and it hovers around that value. I am expecting that it should show a full range of values.

Does this sound familiar to anyone?

Maybe there's a better question... is there a better way to count how long it has been (in terms of percent to the next blade) since the last blade passed using integers? I feel like there should be some way, but I can't think of it.

Thank you

scurrier
11-17-2010, 03:28 AM
Some more details on this. The truncated portion of the float value seems to be what I would expect. Usually 0 if I don't block the sensor. Some other reasonable values if I do block the sensor.

John Abshier
11-17-2010, 04:28 AM
F.Frac returns a floating point number. debug.dec requires an integer. I think you need debug.dec(f.ftrunc(f.fmul(fr.frac(depth), 1000.0)))

John Abshier

scurrier
11-17-2010, 05:21 AM
Didn't think to check that! I was thinking of frac as being in the same class of functions as ftrunc. Thanks for your help.

scurrier
11-18-2010, 12:19 AM
For anyone who is wondering, I asked a question on stack overflow about calculating percent using integers. Being only a mortal non-programmer I am sure many here know this, but I thought I'd post it anyway for completeness.

Using fixed point math to calculate percent:
http://stackoverflow.com/questions/4201747/looking-for-good-substitute-for-calculating-percentage-employing-integer-math