MikeLankamp / fpm

C++ header-only fixed-point math library
https://mikelankamp.github.io/fpm
MIT License
649 stars 79 forks source link

Question about having to resort to floats for initialization. #34

Closed unvestigate closed 2 years ago

unvestigate commented 2 years ago

Hi Mike and thanks for the library!

I am working with a deterministic codebase and I have question about initializing fixed-point numbers without having to resort to floats for initialization. Say I need to init a number to 0.25. I can use a float to init an fpl number like so:

fpm::fixed_16_16 b { 0.25 };

However, here we are using a float to init the value. Now, I don't know the risks of using a float for this when it comes to determinism but it would be nice to avoid floats altogether if possible. Is there a way to init an fpm number using a fraction or something? Eg, the above could be initialized like this:

fpm::fixed_16_16 b; b.initFraction(1, 4);

Thanks!

unvestigate commented 2 years ago

Oh, wait a minute. I can just do this:

fpm::fixed_16_16 b = fpm::fixed_16_16{ 1 } / fpm::fixed_16_16{ 4 };

Not sure if this is the most ergonomic/efficient way to init a value but it will do. Closing the issue for now...