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 = 204616/32768.0;
float c = 205616/32768.0;
float d = 206616/32768.0;
Serial.println(a);
Serial.println(b);
Serial.println(c);
Serial.println(d);
while (true){}
}
Hi
i found the problem!
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
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:
}
Tell the compiler that you want to do floating point math by using decimal point constants.
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.
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.
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).
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.