John McFarlane
John McFarlane
Yes, that's correct. You're using the library as intended but the implementation has inefficiencies which I need to address. The correct solution may take some time to figure out though....
Note to self: test written in Jun 2019 (!) to capture this: ```c++ #if !defined(CNL_UNREACHABLE_UB_ENABLED) TEST(safe_integer, construction) { cnl::safe_integer ex{987653210LL}; cnl::safe_integer a{987653210LL}; cnl::safe_integer ac{cnl::_impl::scale(a)}; ASSERT_TRUE(identical(ex, ac)); } #endif ```
This is another case where the type avoids pitfalls for inexperienced users. From [P0828](https://wg21.link/p0828): > For two's complement signed types, the most negative number is not in the range allowed...
> there has to be datatype with correct behaviour. I'm happy to consider such a type. Feedback from LEWGI in Cologne in 2019 may point to a solution which is...
> I let the datatype expand naturally without loss of precision I think we're on the same page but to make it clear: with this type, [you must cast to...
Sorry, saturation won't work as desired because `overflow_integer` doesn't know how many bits the alias has. You are looking for a type which doesn't currently exist in CNL. It's similar...
There's no such API currently. I took a look at this yesterday. There is a [`set_rounding_t`](https://github.com/search?q=user%3Ajohnmcfarlane+set_rounding_t&type=code) alias which does a near-identical thing for `rounding_integer`. But a quick fix to copy/paste...
Quick thought: have you tried `%`? If you just want lossless division, then the modulo operator will provide that -- even though it's not modulo 1 like regular integer `%`....
That looks like the correct result. Re-opening because I should really provide this at some point.
Notes for anyone wishing to pursue this task (including myself!)... You might wish to see how `[sqrt(scaled_integer)](https://github.com/johnmcfarlane/cnl/blob/main/include/cnl/_impl/scaled_integer/sqrt.h)` is implemented (using `[sqrt(integer)](https://github.com/johnmcfarlane/cnl/blob/main/include/cnl/_impl/cmath/sqrt.h)` under the hood) for examples of how such a...