MikeLankamp / fpm

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

Is there a way to initialize a fpm value from the string expression of a float number? #54

Closed rockingdice closed 1 year ago

rockingdice commented 1 year ago

What I'm doing is to read float numbers from a JSON file, and I don't know how to read it into a fpm value. I'm making a deterministic app, so I tried to avoid any float number when possible.

Like this one:

    std::string a_number = "3.1415926";

How to convert it to a fpm value with deterministic?

MikeLankamp commented 1 year ago

Hi @rockingdice, you can use the streaming operators to convert deterministically between fpm values and strings, e.g.:

fpm::fixed_16_16 f;
std::stringstream("3.1415926") >> f;

There's also an open request to support the equivalent of std::from_chars: #2.

Does this solve your issue?

rockingdice commented 1 year ago

Yes, it does! Thanks for the quick reply!

rockingdice commented 1 year ago

oh, i just realized if

Hi @rockingdice, you can use the streaming operators to convert deterministically between fpm values and strings, e.g.:

fpm::fixed_16_16 f;
std::stringstream("3.1415926") >> f;

There's also an open request to support the equivalent of std::from_chars: #2.

Does this solve your issue?

Will the std::from_chars be implemented?