Serial.print() gives wrong value (0.00) when rounding 0.995 through 0.999
my arduino101 giving incorrect values in serial.print() using serial monitor when value between parentheses float value 0.995 0.999; result should 1.00, displayed value 0.00. following code , output show what's going on:
sketch:
gives output (errant output values highlighted in bold:
i = 990 0.99 99.00
i = 991 0.99 99.10
i = 992 0.99 99.20
i = 993 0.99 99.30
i = 994 0.99 99.40
i = 995 0.00 99.50
i = 996 0.00 99.60
i = 997 0.00 99.70
i = 998 0.00 99.80
i = 999 0.00 99.90
i = 1000 1.00 100.00
0.99
0.99
0.00
0.00
0.00
0.00
0.00
i've looked documentation of this, couldn't find any. has else run this? way, not happen mega 2560, arduino uno, or uno clone.
sketch:
code: [select]
float fraction, scaled, x100value;
int count=0;
void setup() {
// initialize serial communication @ 9600 bits per second:
serial.begin(9600);
}
void loop() {
while(count<1){
delay(5000);
for(int i=990; i<=1000; i++){
serial.print("i = ");
serial.print(i);
serial.print("\t ");
fraction = float(i)/1000;
serial.print(fraction);
serial.print("\t ");
x100value = 100*fraction;
serial.println(x100value);
delay(1);
}
count++;
serial.println(0.993);
serial.println(0.994);
serial.println(0.995);
serial.println(0.996);
serial.println(0.997);
serial.println(0.998);
serial.println(0.999);
}
}
gives output (errant output values highlighted in bold:
i = 990 0.99 99.00
i = 991 0.99 99.10
i = 992 0.99 99.20
i = 993 0.99 99.30
i = 994 0.99 99.40
i = 995 0.00 99.50
i = 996 0.00 99.60
i = 997 0.00 99.70
i = 998 0.00 99.80
i = 999 0.00 99.90
i = 1000 1.00 100.00
0.99
0.99
0.00
0.00
0.00
0.00
0.00
i've looked documentation of this, couldn't find any. has else run this? way, not happen mega 2560, arduino uno, or uno clone.
i've found issue has been addressed fix has not yet been released:
https://github.com/01org/corelibs-arduino101/issues/339
https://github.com/01org/corelibs-arduino101/issues/339
Arduino Forum > Products > Arduino 101 (Moderators: cmaglie, facchinm) > Serial.print() gives wrong value (0.00) when rounding 0.995 through 0.999
arduino
Comments
Post a Comment