Advertisement

String buffer size needed to store all float values

Started by June 20, 2021 04:09 PM
2 comments, last by Shaarigan 3 years, 5 months ago

Hi!
It is easy to know the buffer size needed to store any uint8, uint16, uint32, uint64 and the signed version since we know the min and max values and there is no decimal part.
It is more difficult to know the buffer size for float and double.
Is there a safe way to compute or know the buffer size needed to store any float values?
Thanks a lot!

floating type is either 4byte or 8 byte, you can outconvert them safely to certain encoding, it will just not be readable. In formated case to readable form to store, there is this pascal formating which results in max count of characters. I rememeber it is some digits*e^digitse where diggits is 2^24 and digitse is 256.

Advertisement

Alundra said:
uint8, uint16, uint32, uint64

I guess you refer to C/C++ data types right?

When I worte my ToString function in C++, I simply shifted the decimals by 10 for as long as there were decimals in the value

while(decimals > 0 && ((value - Math::Floor(value)) > 1e-12 && (Math::Ceil(value) - value) > 1e-12))
{
    value *= 10;
    decimals--;
}

And finally just ran the remaining non-decimal value through a generic signed to string function to get the final result of digits

This topic is closed to new replies.

Advertisement