nickgammon / BigNumber

BigNumber library for the Arduino
MIT License
88 stars 22 forks source link

casting float #7

Closed akita11 closed 6 years ago

akita11 commented 6 years ago

Casting float-type variable seems to be ignoring fractional part?

BigNumber a = "12.34";
Serial.println(a);
Serial.println((float)a);

This code results in:

12.34
12.00

Is this a specification of BigNumber?

nickgammon commented 6 years ago

BigNumber doesn't have an operator float and it wouldn't make a huge amount of sense for it to do so. The compiler probably selects the nearest operator, being operator long, which would truncate the decimal points. Since big numbers can have hundreds of decimal points, casting to float would discard most of them.

If you must, use atof to convert the string returned by a big number to a float.

akita11 commented 6 years ago

Thanks, I've understood the internal operation in the library. Let me close this issue.