1

i want to ask, how to get correct float value from uint32_t or long variable is devided by 10??

long var3 = 1139399025;
int a = 3;
int b = 10;
void setup() {
  Serial.begin(9600);
  Serial.println((float)a/b,2);
  Serial.println((float)var3/b,2);
}
void loop() {
}

result

0.3
113939904.00

it give 113939904.00 instead of 113939902.50, plase how to fix that?

mukhlas
  • 11
  • 2

1 Answers1

2

1139399025 is not a floating point number: the largest odd integer that can be exactly represented as a float is 224 − 1 = 16777215. Then, when var3 is cast to float, it gets rounded to the closest float, which is 1139399040. This is quite a small error, especially considering that, in this range, only multiples of 128 can be exactly represented as floats.

Dividing 1139399040 by 10 gives 113939904, which happens to be a float. The floating point division gives then the exact result, which is what gets printed. Indeed, floating point operations do not always give inexact results: sometimes they are exact. However, unless you know what you are doing, exact results only happen by luck. In general, expect each floating point operation to make a relative rounding error in the range of 10−7.

A highly recommended read: What Every Programmer Should Know About Floating-Point Arithmetic.

Edit: If you want to print the value of an integer divided by 10, you can divide the number using Euclidean division. Print the quotient, then the decimal point, then the remainder:

Serial.print(var3 / 10);  // quotient
Serial.print('.');
Serial.println(var3 % 10);  // remainder

As these are all integer operations, there is no rounding error involved. Obviously this trick doesn't work for arbitrary divisors.

Edgar Bonet
  • 45,094
  • 4
  • 42
  • 81