Is it CPU bug?

Hi All,
I'm running the following code:
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
while(!Serial);
}

void loop() {
// put your main code here, to run repeatedly:
float a = 203616/32768.0;
float b = 2046
16/32768.0;
float c = 205616/32768.0;
float d = 2066
16/32768.0;
Serial.println(a);
Serial.println(b);
Serial.println(c);
Serial.println(d);
while (true){}
}

And i get:
0.99
0.00
1.00
1.01

Can somebody explain why b == 0?
Thanks

Hi
i found the problem! :slight_smile:
If i change the print(b) to be print(b,10) i get:
0.9941406250
0.9990234375
1.0039062500
1.0087890625
So the problem is in print operations - please fix!
Thanks

So the problem is in print operations

It's not a bug. Serial.print() with no length parameter defaults to printing 2 decimal places. That's what you told it to do so that is what you got.

0.0 is pretty far from 0.99!

You need to put decimal points in the numerator so the multiplication is carried out as a float.

A Nano with IDE version 1.6.12 prints:

0.99
1.00
-1.00
-0.99

It also prints warnings about integer overflow on the initialization of c and d.

Which Arduino and IDE are you using?

Pete

  1. If you want the code to execute only once, put it in setup()
void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  while(!Serial);

  float a = 2036.0*16.0/32768.0;
  float b = 2046.0*16.0/32768.0;
  float c = 2056.0*16.0/32768.0;
  float d = 2066.0*16.0/32768.0;
  Serial.println(a);
  Serial.println(b);
  Serial.println(c);
  Serial.println(d);  
}

void loop() {
  // put your main code here, to run repeatedly:
}
  1. Tell the compiler that you want to do floating point math by using decimal point constants.
  2. Read the "How To Post" post by Nick Gammon at the top of this Forum on the proper way to use code tags when posting source code.

el_supremo:
A Nano with IDE version 1.6.12 prints:

0.99

1.00
-1.00
-0.99



It also prints warnings about integer overflow on the initialization of c and d.

Which Arduino and IDE are you using?

Pete

I'm using a Mega2560 and IDE 1.6.12, with the code I listed above, I get:

0.99
1.00
1.00
0.99

But using the decimal point constants prevents the overflow.

The OP asked why b == 0. I pointed out that a NANO with 1.16.12 does not print 0.00, in fact it prints the correct answer. Ergo, I ask which Arduino and IDE version (s)he's using.

Pete
P.S. the fact that OP claims that c and d were positive suggests that it's a 32-bit processor.

I'm using genuino101

Known bug (fixed, waiting to be released) in the 101 core:

oqibidipo:
Known bug (fixed, waiting to be released) in the 101 core:
Serial.print() rounding error · Issue #339 · arduino/ArduinoCore-arc32 · GitHub

So to answer the original question: No, it's not a CPU bug. It's a Arduino/Genuino 101 core bug. :slight_smile:

2066 * 16 is 0x0812 * 0x10, which is 0x8120, and should be being evaluated as the two byte integer -32750. Then that value should be promoted to floating point to do the division, the result of which is -.99 (to two places).

I would bet that rather than the CPU having a bug, the code the OP posted is not exactly the code that generated his output.

The OP has already said he's using a genuino 101 which is a 32-bit processor. That's why the last two results (c and d) were positive (the multiplications didn't overflow).

Pete

el_supremo:
The OP has already said he's using a genuino 101 which is a 32-bit processor. That's why the last two results (c and d) were positive (the multiplications didn't overflow).

Which just goes to show that "int is whatever size is convenient to the processor" was K&R's biggest blunder. If I had a time machine, it's one of the mistakes I would fix.

Funny, they didn't think so. They were more concerned about the precedence of the logical vs bitwise operators.