# Arduino Float values not Accurate

I used this note to test my keypad and I need at least 6 points to float correctly. After working at least 7 digits it goes wrong. Please tell me how to fix it. I must enter 9 digits at a time and the return floating value must be correct to at least 6 digits on return. (Ex: Input => 123456789 and Exit => 123.456789)

const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1', '2', '3', '+'},
{'4', '5', '6', '-'},
{'7', '8', '9', '*'},
{'C', '0', '=', '/'}
};

byte rowPins[ROWS] = {11, 10, 9, 8};
byte colPins[COLS] = {5, 4, 3, 2};

void setup() {
Serial.begin(9600);
}

void loop() {
long va = function();
Serial.print("Value is   ");
Serial.print(va);
Serial.print("  Value 2 is   ");
double lati = double(va) / 1000000;
Serial.println(lati,6);
delay(2000);
}

long function()
{
Serial.println("enter Value  ");
long valu = 0;
long key = 0;
int index;
do {
index = (key >= '0' && key <= '9');
if (index)
{
Serial.print(key - '0');
valu = (valu * 10 + key - '0');
}
}
while (index || !key);
Serial.println();
return valu;
}

My outputs...

enter Value
123456
Value is   123456  Value 2 is   0.123456
enter Value
1234567
Value is   1234567  Value 2 is   1.234567
enter Value
12345678
Value is   **12345678  Value 2 is   12.345679**
enter Value
14785236
Value is   **14785236  Value 2 is   14.785237**
enter Value

You don't understand how floating point values work in a microprocessor.

If you want that precision, store your value as a long (original value * 100000) and then use it on demand if you want that precision.

Uno floats are only good for about 7 digits
What do you need to do with the result?
You could just keep the numbers in a String.
OR split the pre decimal numbers from post decimal and handle them separately and add the results.
Still only 6/7 digits total accuracy
see float - Arduino Reference

Look at your results: they ARE correct to 6 digits (actually even to 7 digits) eg

14785236 Value 2 is 14.785237

There is a library called "Float64" which will allow you to do calculations in 64-bit floats instead of the Arduino's 32-bit floats.

Got it. Thankyou every one.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.