johnmcfarlane / cnl

A Compositional Numeric Library for C++
Boost Software License 1.0
634 stars 63 forks source link

Strange decimal literal behaviour #911

Open dzarda opened 3 years ago

dzarda commented 3 years ago

Describe the bug:

I'm seeing a weird representation of a _c literal decimal value such as 0.22_c. It seems turn into a massive value in the rep.

To Reproduce:

#include <cnl/all.h>
using namespace cnl::literals;
using fix24_8 = cnl::scaled_integer<int32_t, cnl::power<-8>>;

fix24_8 a = 0.22_c; // rep 4608
fix24_8 b = 0.22; // rep 56

Expected behavior:

Both reps should be 56.

Screenshots: Double literal: obrazek

Custom literal: obrazek

Desktop (please complete the following information):

Link to Compiler Explorer:

https://godbolt.org/z/vTjdz5sah

johnmcfarlane commented 3 years ago

Unfortunately, the UDL, ""_c does not work with fractions. C++17 is only allowed to use integral NTTPs (non-type template parameters) and as 0.22 isn't even representable in binary floating-point, it's out of the running.

Sorry that it compiles at all! (It doesn't under v2 branch. Action: deprecate ""_c.)