Live data from Hacker News

Show HN: Floating point arithmetic types in C++ for any size and any base

github.com

1–10 of 42 posts

Show HN: Floating point arithmetic types in C++ for any size and any base

#1
Since there is so much interest on HN in floats lately and their software implementations, I wanted to show mine. It has no use and is just for teaching me floats and C++. Give me your thoughts.

Show HN: Floating point arithmetic types in C++ for any size and any base
github.com

Re: Show HN: Floating point arithmetic types in C++ for any size and any base

#4
Nice!

> TODO: (configurable) rounding support

What's the default rounding mode? Round to nearest even?

You might be interested in https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2024/p33... too, which is a recent paper for introducing reproducible floating point to C++.

Very small floating point types can be handy for exhaustive testing of floating point function templates, especially ones that take multiple arguments. Walking over all floating point values for a small type often finds most if not all corner cases that can manifest with a floating point type of any size.

Re: Show HN: Floating point arithmetic types in C++ for any size and any base

#5
post #3

For "any size" I was kind of expecting arbitrary sized mantissa/exponent, can be useful for emulating weird DACs, for example, 12-bit mantissa and 3-bit exponent[1]. [1] https://ajxs.me/blog/Yamaha_DX7_Technical_Analysis.html

Possibly could be combined with C23's _BitInt(N) for the template arguments? I think it's available in clang as a C++ extension.

edit: or I guess you could have your own Tmantissa and Texponent types as custom classes that correctly model _BitInt(N), they don't seem to be required to be builtin integral types.

Re: Show HN: Floating point arithmetic types in C++ for any size and any base

#7
Consider dropping the specializations for the type traits, given that it's undefined behavior:

https://en.cppreference.com/w/cpp/types/is_fundamental

https://en.cppreference.com/w/cpp/types/is_floating_point

https://en.cppreference.com/w/cpp/types/is_arithmetic

https://en.cppreference.com/w/cpp/types/is_scalar

https://en.cppreference.com/w/cpp/types/is_object

Re: Show HN: Floating point arithmetic types in C++ for any size and any base

#8

Nice! > TODO: (configurable) rounding support What's the default rounding mode? Round to nearest even? You might be interested in https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2024/p33... too, which is a recent paper for introducing reproducible floating point to C++. Very small floating point types can be handy for exhaustive testing of floating point function templates, especially ones that take multiple argu…

Rounding: actually it just cuts off. I have not spent much time to think about how to specify and implement the different rounding modes. Maybe some day...

Thanks for the hint to the paper. I also faced these issues. Thus, I provided a constructor which accepts mantissa and exponent as values. Very handy for the unittests.

Re: Show HN: Floating point arithmetic types in C++ for any size and any base

#9
post #3

For "any size" I was kind of expecting arbitrary sized mantissa/exponent, can be useful for emulating weird DACs, for example, 12-bit mantissa and 3-bit exponent[1]. [1] https://ajxs.me/blog/Yamaha_DX7_Technical_Analysis.html

Actually you can specify the numeric limits of the mantissa and the exponent. They can be specified as template arguments[0]. So you could do:

      Float       // highest possible value of the exponent
The Float then simulates an unsigned 12bit mantissa and a 3bit exponent. Sure it still takes 16 bytes. But you could create a union with bitfields where you shrink that even further.

[0] https://github.com/clemensmanert/fas/blob/58f9effbe6c13ab334...

Post reply on HN