Showing posts with label Bug. Show all posts
Showing posts with label Bug. Show all posts

Sunday, May 17, 2015

Uh oh...

I ran across an interesting little anomaly today.  Consider the following Arduino code which prints the result of dividing an unsigned long by a float.

void setup() 
{
  Serial.begin(115200);
  
  uint32_t f = 10000005UL;

  for (int i = 0; i < 10; i++)
  {
    Serial.print(f);
    Serial.print(" - ");
    Serial.println(f/1e6, 6);
    f--;
  }
}

void loop()
{

}

Unfortunately, the following is the output:

10000005 - 10.000005
10000004 - 10.000004
10000003 - 10.000003
10000002 - 10.000002
10000001 - 10.000001
10000000 - 10.000000
9999999  - 10.000000
9999998  - 9.999999
9999997  - 9.999998
9999996  - 9.999997

I guess I am going to have to look into the Print class a bit and see if the error is in the divide operation or the print operation.  Heads up...