Reputation: 1655
I've created my own fixed-type integer types and the library works fine with many compilers and platforms, the only problem left to solve is to convert from built-in float-point types to my types.
The float-point types may have small mantissa but along with exponent it may have big value so if i choose to cast float
, double
, or long double
say to long long
or unsigned long long
a truncation may occur.
If the compiler use IEEE-754 specification it'll be easy to extract mantissa and exponent, but what if the compiler use some other format.
So, my question: is there any generic algorithm that allows me to extract the full value from a float-point using only the language features?
thanks
Upvotes: 4
Views: 635
Reputation: 62048
You may find useful my answer to a somewhat similar question here. The idea is that if the underlying format is binary, you are pretty much guaranteed to be able to extract the mantissa and exponent without loss if there are no more bits in the mantissa than there are in the biggest integer type. You can use double frexp(double value, int *exp);
and double ldexp(double x, int exp);
for the purpose.
Another option would be to sprintf()
the number using %a
to get all the digits and then manually convert the textual representation into what you need.
Upvotes: 5