Printing math gives inf

I use this code, en when printing get ovf.
Why is this?

typdouble xd=24.66e10,yd=32.54,zd;
float xf=12.4e+10,yf=98.67,zf;

void setup() {
  Serial.begin(115200);
  Serial.println(" test ");
  zd = xd * yd;
  Serial.println(zd);
  zf = xf * yf;
  Serial.println(zf);
}

void loop() {
  // put your main code here, to run repeatedly:

}e or paste code here

From the reference documentation:

However,

xd is 2.466E100.

I suspect that double is 4 bytes on your system.

[edit]

Oops, that should be 2.466E11 as @jremington said. Please disregard the explanation above.

Did you mean 2.466E11?

@PowerSoft You forgot to say which Arduino you are using, but on the Uno and others, Serial.print() does not support scientific notation, so it converts the integer part of the float/double to int32, which overflows at about 2.4e9.

Example.

void setup() {
  // put your setup code here, to run once:
Serial.begin(115200);
while (!Serial);
float x = 2.1e9;
float y = 21.0e9;
Serial.println(x);
Serial.println(y);
}


void loop() {
  // put your main code here, to run repeatedly:
}

Output

2100000000.00
ovf

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.