ckormanyos/soft_double

math::softfloat::float64_t( <float_value> ) or math::softfloat::float64_t(<double_value>) possible?

RobertMassart opened this issue · 3 comments

In a cpp header I want to have the following work:

inline constexpr auto gravitational_constant = math::softfloat::float64_t(6.674e-11);

// This will work when it's an integer value
inline constexpr auto gravitational_constant = math::softfloat::float64_t(6);

Just can't use constexpr when it's a double or float.

Is it possible to make this work as an constexpr?

Is it possible to make this work as an constexpr?

Yes @RobertMassart there are several options for this to work. One of them is to make the constructor from the internal 64-bit representation type to be public. I'll see qhich option is best, and we can discuss further.

I did find in VS2022 it was an "invalid access to an inactive member" so to fix this I change the following:

at line 394:

From:
explicit constexpr uz_type(float_type f) noexcept : my_f(f) { }

To:
explicit constexpr uz_type(float_type f) noexcept : my_u{}, my_f(f) { }

Hi Robert (@RobertMassart), I used a standard C++20 function to do a preliminary (yet portable) fix for this. The only real condition is that you need C++ 20 or higher, or use a compiler that has the feature __cpp_lib_bit_cast, as shown in feature-testing.

If you are using a modern GCC or VS2022, this will simply be transparent for you and constexpr-initialization from built-in double will simply just work.

This is all cycling in #112