# Simple division error

double n=12345678;
n=n*0.000001;
Serial.print(n,12); //12 is to view the fractional nos

Here the answer is not 12.345678000000, but have some more decimal digits like 12.345678154896.
How to solve this? I want the exact value as mentioned (12.345678).

Floating point will not give you EXACT values for some numbers. Just like some fractions don't have exact decimal representations.

The arduino float format is also only good for about 6 digits of precision, and you're trying to use 8.

How to solve this?

Use something other than an Arduino.

A float is 32 bits. That gives a decimal precision of 6 to 7 digits. 12.345678 is 8 digits. That is more than a float can manage.

Printing a float to 12 decimal places is a waste of effort.

devanpvasudevan:
double n=12345678;
n=n*0.000001;
Serial.print(n,12); //12 is to view the fractional nos

Here the answer is not 12.345678000000, but have some more decimal digits like 12.345678154896.
How to solve this? I want the exact value as mentioned (12.345678).

If you only need 8 digits you can use unsigned long integers and do the math fixed point style.

Here's an example. I want to divide 696 by 12.345678.

12.345678 is 12,345,678 / 1,000,000.

``````long x = 696;
long result = x * 1000000L / 12345678L; // must do the multiplication first!
``````

Note, however, that the result is an integer, 56. Not 56.376004622832379072255083924917... Fixed point math is a lot more hands-on than with floating point and each calculation has to be tailored for the task at hand. But in many cases, it's a much better choice because it's faster and leaner.

In this example, the limiting factor was the 31 bit maximum size of a long, and the fact that the product of two numbers uses about double the number of bits. The divisor wouldn't fit in 15 bits. But I could do:

``````long x = 696;
long result = x * 1000000L / 12345L; // must do the multiplication first!
``````

and the integer result is 56379. The assumed decimal place is three digits to the left, so 56.379 is the actual result.

It is a double not a float.
I'm working with a GPS module and want to process Latitude and Longitude data which have 6 decimal digits.

I wish to process them in their own structure (6 decimal points and total 8 digits) or else I can use them as simply 8 digit integer. So what should I do?

devanpvasudevan:
It is a double not a float.

The AVR implementation of GCC uses float representation internally when you declare a double. So they are both identical in this environment.

A long type can safely hold 9 decimal digits - 999,999,999. What you choose to do with them, is up to you.

devanpvasudevan:
It is a double not a float.
I'm working with a GPS module and want to process Latitude and Longitude data which have 6 decimal digits.

I want to process them in their own structure (6 decimal points and total 8 digits) or else I can use them as simply 8 digit integer. So what should I do?

I would keep it in 8 digit integers. More precision with unsigned long than float.