Basic Math Error I can't resolve

When running this in the Arduino, the calculation is incorrect. It should be 343597383, but it outputs 343597376. What am I doing wrong?

I removed this portion from my code to show only my math error.

Thanks,

Jim

double freq = (10.0/125.0) * 4294967295.0; void setup() { // put your setup code here, to run once:

}

void loop() { // put your main code here, to run repeatedly: Serial.print(freq); Serial.print("\n"); delay(2000) ; }

That's a problem of using floating-point arithmetic, I'm afraid. You could use integer (maybe "long long") instead.

(On AVR Arduinos, "double" is the same as "float" - only 32 bit)

32 bit floats only have 23 bits of mantissa, meaning the accuracy is limited to between 1 part in 8 million to 1 part in 16 million, ie about 7 digits.

You should not be expecting floating point arithmetic to be exact in the first place, for instance using == to compare floats (other than to zero) is normally a programming error.

Please use code tags.

Read this before posting a programming question

http://en.wikipedia.org/wiki/Floating_point

Plus what the above two said.

You will only get around 7 digits of precision, which you did.

MarkT: 32 bit floats only have 23 bits of mantissa, meaning the accuracy is limited to between 1 part in 8 million to 1 part in 16 million, ie about 7 digits.

You should not be expecting floating point arithmetic to be exact in the first place, for instance using == to compare floats (other than to zero) is normally a programming error.

So what are you saying? I don't have floating point errors of that magnitude with other compilers. I don't have this problem with FORTRAN (obviously) or even Visual Basic (32bit). So is this a C++ limitation or a processor limitation of the arduino?

Thanks,

Jim

jelanier:

MarkT: 32 bit floats only have 23 bits of mantissa, meaning the accuracy is limited to between 1 part in 8 million to 1 part in 16 million, ie about 7 digits.

You should not be expecting floating point arithmetic to be exact in the first place, for instance using == to compare floats (other than to zero) is normally a programming error.

So what are you saying? I don't have floating point errors of that magnitude with other compilers. I don't have this problem with FORTRAN (obviously) or even Visual Basic (32bit). So is this a C++ limitation or a processor limitation of the arduino?

Thanks,

Jim

Disregard...I see it is only an 8bit processor. I would have to "bit and" it out to break it up in order to get the resolution I want.

Thanks,

Jim

What I’m saying is “don’t use floating point when you don’t need or want it”.

void setup() 
{
 unsigned long freq = (10 * ((1LL << 32) - 1)) / 125; 
 Serial.begin (115200);
 Serial.println(freq);
}

void loop() 
{}

jelanier: So what are you saying? I don't have floating point errors of that magnitude with other compilers. I don't have this problem with FORTRAN (obviously) or even Visual Basic (32bit).

Well, you do, as the page on Wikipedia states. a 4-byte float cannot hold a number of more than (just over) 7 digits of precision. Whatever the processor. You may be accustomed to 8-byte floats (commonly known as "double").

double freq = (10.0/125.0) * 4294967295.0;

Yes, I see you are. Well the thing is that the Arduino IDE silently converts them to floats (4 bytes). A double has around 15 digits of precision.

This is documented here:

http://arduino.cc/en/Reference/Double