Fixed Point to Floating Point
RogerInHawaii
Posts: 87
I have a fixed point variable in my Spin code. It has 16 bits of integer value and 16 bits of fractional value. It's coming from an external device in Fixed Point format so I have no direct control over its format. I want to be able to use that in some floating point operations, such as FloatMath.FMul(MyFixedPointVal, MyFLoatingPointVal).
But that clearly doesn't work since the FMul expects both arguments to already be floating point values.
How can I convert my Fixed Point variable into the Floating Point format?
But that clearly doesn't work since the FMul expects both arguments to already be floating point values.
How can I convert my Fixed Point variable into the Floating Point format?
Comments
MyFloatInt := F.FFloat(MFP >> 16) MyFloat is the integer portion of the value. Or
MyFloatInt := F.FFloat(MFP ~> 16) if the first bit is a sign
temp := MFP & $FFFF get the lower 16 bits
MyFloatFrac := F.FDiv(F.FFloat(temp), F.FFloat(65536)) could be 65535
MyFloat := F.FAdd(MyFloatInt, MyFloatFrac)
John Abshier
Those approaches, using FFloat to convert and then combine, looks good!
I'll give it a try.
Thanks
Jay