Live data from Hacker News

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

github.com

11–20 of 42 posts

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

#11
post #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

I don't get what you mean. I thought they specify how the type can be used?

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

#12
post #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

I don't get what you mean. I thought they specify how the type can be used?

Technically, you're not supposed to add your own specialisations to the `std` namespace

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

#13
post #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

I don't get what you mean. I thought they specify how the type can be used?

The cppreference page says:

> If the program adds specializations for std::is_fundamental or std::is_fundamental_v, the behavior is undefined.

This is an oversimplification. The actual rule is https://eel.is/c++draft/library#namespace.std-2 .

> the specialization meets the standard library requirements for the original template.

For is_fundamental it means that is_fundamental::value must be false, as YourClassType is not a fundamental type, as defined in https://eel.is/c++draft/basic.fundamental#17 .

Some traits are just not designed to be customization points.

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

#14
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/…

Can you go in the other direction? Higher exponent and mantissa than regular float/double?

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

#15

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.

By cutting off do you mean that it correctly rounds towards zero? Maybe you can implement rounding to closest by just doing the calculation in a one digit wider mantissa with rounding to zero and observing the last digit, at least for an even base. It won't be rounding to even though, but for that a 2 digit wider mantissa is probably enough.

Rounding to nearest with an odd base doesn't seem to be as straightforwardly implementable from rounding to zero calculations at a higher precision.

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

#16

Earlier quoted context omitted.

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/…

Can you go in the other direction? Higher exponent and mantissa than regular float/double?

Sure.

    Float
Gives you a signed Mantissa with 64 bit and a signed Exponent with 64bit. Since there are numeric limits for int64_t available, Float knows the max and the min value.

You could get even bigger ranges for Float by implementing your own big integer type.

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

#17

Earlier quoted context omitted.

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.

By cutting off do you mean that it correctly rounds towards zero? Maybe you can implement rounding to closest by just doing the calculation in a one digit wider mantissa with rounding to zero and observing the last digit, at least for an even base. It won't be rounding to even though, but for that a 2 digit wider mantissa is probably enough. Rounding to nearest with an odd base doesn't seem to be as straightforwardly…

I remember that I tried that some time ago. Especially the multiplication was tough, but I can not recall where I gave up. When I find some time, I will pick it up again :)

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

#18

Earlier quoted context omitted.

I don't get what you mean. I thought they specify how the type can be used?

Technically, you're not supposed to add your own specialisations to the `std` namespace

In general this isn’t true (i guess it is in this specific context). For example I believe it’s totally expected to specialize std hash
Post reply on HN