0

Let's say I want to use floating point for calculation, but I want to represent my input data with constant resolution. One possible answer is to start with integers and convert them to floating point but this can be wasteful if you have to do it repeatedly. It would seem like subnormal floating point is a possible solution, since in this region, the magnitude of the number is controlled only by the mantissa and not the exponent.

The question is, are there any gotchas with how floating point numbers are handled in CPUs (for CPUs that support subnormal numbers, that is) that would prevent this from working as I expect?

reveazure
  • 653
  • 2
  • 7
  • 14
  • 3
    Of course there are gotchas, but since you have not listed your expectations, this issue is broad. Some are very nuanced [example](https://stackoverflow.com/q/46611633/2410359) or the existence of +/- zero vs. integer math. I think you do have a good germ of an idea, yet need more info on your goals. You might be able to simulate up to an `int54_t`. Be mindful that sub-normal support is often a source of inconsistency amongst C implementations. Good luck. – chux - Reinstate Monica Aug 12 '22 at 20:22
  • 1
    It seems that what you want is a [fixed-point](https://en.wikipedia.org/wiki/Fixed-point_arithmetic) numeric type. – dan04 Aug 12 '22 at 21:57
  • Subnormals can be slower than normal floating point numbers on many CPUs. Also, if someone accidentally activated flush-to-zero, any (float) operation on your numbers will set them to 0. Also, `int->float` usually is implemented in hardware, so you don't really gain a lot by just reinterpreting your int as a subnormal float. – chtz Aug 12 '22 at 22:56
  • “Subnormals can be slower” understates the issue: On some CPUs, subnormals are hugely slower. – Eric Postpischil Aug 12 '22 at 23:00
  • Constant resolution means fixed spacing, which means the scale does not float. Why do you want to use floating-point if your point does not float? For what purpose do you want fixed resolution? Why can’t you get it with integer arithmetic? – Eric Postpischil Aug 12 '22 at 23:00
  • @Eric are subnormals slower on normal desktop CPUs? – reveazure Aug 12 '22 at 23:59
  • @reveazure: Yes, on various processor models. – Eric Postpischil Aug 13 '22 at 00:01
  • @Eric I want to use floating point for calculations, because it makes a lot of calculations easier, but treat it as an intermediate form where the data is ultimately stored in a non-floating point way. Just an idea I'm exploring. – reveazure Aug 13 '22 at 00:04

0 Answers0