-->
Page 1 of 2

FP issues

PostPosted: Tue Feb 16, 2016 2:50 pm
by xtal
The following code does not print correct Floating point values....
Do I have to include a special lib ??
The only inclued I'm using is 'ESP8266WiFi.h'
I could not display float values on webpage .002222 was showing .00
Serial.print should show ,x decimals
Code: Select allint RFH = 8;
float Hr = 0;
float yy = 0;
float zz = 0; 

void setup() {
 Serial.begin(38400);                            //  start serial for debug
  delay(10);
}

void loop() {
 delay(500);
  Serial.print("RFH= ");
  Serial.println(RFH); 
 delay(500);
  zz = RFH/3600 ;                  // Arduino 5 decimals  8/3600=.00222
  Serial.print("zz=RFH/3600= ");   
  Serial.println(zz,5); 
  Serial.println(zz*1000000);
 delay(500);
  Serial.print("Serial.print 8/3600,6= ");
  Serial.println(8/3600,5); 
  Serial.println(8000000/3600);
 delay(500);
  yy = Hr + zz;                 // Hr = 0 @ start 0.00222
  Serial.print("yy= ");
  Serial.println(yy,5);
   Serial.println(yy*1000000);
 delay(500);   
  Serial.print("Hr=yy= ");
  Hr = yy;   
  Serial.println(Hr,5);
  Serial.println(Hr*1000000); 
}

This is the output----
Code: Select allPORT OPEN 38400
Serial.print 8/3600,6= 0
2222
yy= 0.00000
0.00
Hr=yy= 0.00000
0.00
RFH= 8
zz=RFH/3600= 0.00000
0.00
Serial.print 8/3600,6= 0
2222
yy= 0.00000
0.00
Hr=yy= 0.00000
0.00


Re: FP issues

PostPosted: Tue Feb 16, 2016 3:28 pm
by WilliamMcC
Check to make sure the firmware on your chip was compiled with floating point. Some of the firmware does not include floating point to save space.

Try :
float num = 1.23456
print(num)

Re: FP issues

PostPosted: Tue Feb 16, 2016 4:06 pm
by xtal
this is Arduino IDE as far as I know there is only 1 FW generic ESP8266

Re: FP issues

PostPosted: Tue Feb 16, 2016 6:21 pm
by lethe
If both operands are integer, the result of a division will also be an integer in C/C++ (rounded down to the next int).
You need to make at least one operand a float value (either by adding .0f if it's a constant or casting to float).