Shop OBEX P1 Docs P2 Docs Learn Events
Custom float type ? — Parallax Forums

Custom float type ?

I'm thinking about creating a custom floating point data type in spin2 for the propeller2 processor.
The propeller2 has built in hardware 32 bit integer multiplication and division routines.
Spin2 already has ieee single precision floating point, but it reduces the mantissa precision so that the sign, mantissa, and exponent can all be stored in a single 32 bit long. I would like to make a double long type that stores the mantissa in the first 32 bit long, and the sign, exponent and nan stuff in the second 32 bit long. This would give the mantissa the full 32 bit precision that is afforded by the math hardware functionality...
Storing the sign in the second long would give 33 effective bits?
The new structure feature may make this look cleaner...

Comments

  • ElectrodudeElectrodude Posts: 1,687
    edited 2026-04-10 20:49

    That would get you a dynamic range of 2^(2^31)10^646456993, which is unquestionably overkill for any sane application, considering that the volume of the universe is only 10^185 Planck volumes. Even the dynamic range of a native 32-bit fixed-point number without the runtime-adjustable exponent used in floating point is overkill for most applications - are you sure you can't just drop your second exponent long and use 32-bit fixed-point math?

  • evanhevanh Posts: 17,178

    Yeah, even extended (80-bit) floats are still only 16-bit exponent.

  • @Electrodude said:
    Even the dynamic range of a native 32-bit fixed-point number without the runtime-adjustable exponent used in floating point is overkill for most applications - are you sure you can't just drop your second exponent long and use 32-bit fixed-point math?

    32 bit resolution is enough for applications that have a single range per channel like audio data or scopes/sensor data loggers. But if you need derivative functions like position, velocity, acceleration and jerk with small time steps like ms or µs you soon get overflows or aliasing effects with only 32 bits. I use 64 bit fixed point math for my motion controller for that reason. It's still pretty fast with the CORDIC unit of the P2.

  • @ManAtWork said:

    @Electrodude said:
    Even the dynamic range of a native 32-bit fixed-point number without the runtime-adjustable exponent used in floating point is overkill for most applications - are you sure you can't just drop your second exponent long and use 32-bit fixed-point math?

    32 bit resolution is enough for applications that have a single range per channel like audio data or scopes/sensor data loggers. But if you need derivative functions like position, velocity, acceleration and jerk with small time steps like ms or µs you soon get overflows or aliasing effects with only 32 bits. I use 64 bit fixed point math for my motion controller for that reason. It's still pretty fast with the CORDIC unit of the P2.

    I used (by neccessity) 32 bit fixed point in my texture mapping code and it's just barely enough to stave off catastrophic overflow or rounding errors with a bunch of tricks to keep 1/Z and related values in certain normalized ranges. (though 8 bits are wasted in 1/Z itself so the other values come out as rounded integers after division without needing extra shifts and moves)


    For OP, I'd recommend looking at the Spin2 BinFloat(? or was it called something else?) object that was used before native float operations were added. having the mantissa in it's own long I don't think is a huge change to make.

Sign In or Register to comment.