Incorrect result for basic one and two digit arithmetic

I have been trying to do some simple projects on my Arduino mega and for some reason, when using variables, basic arithmetic such as 4*3.14 will return an answer like 12.03 instead of 12.56.

float radius;
float pi = 3.14;
float area;

void setup() {

  Serial.begin(9600);
}

void loop() {
  Serial.println("Enter radius: ");
  while(Serial.available() == 0){

  }
  radius = Serial.parseFloat();
  area = pi*radius*radius;
  Serial.println("The area is: ");
  Serial.print(area);
  Serial.println();
}

Above is a simple piece of code I was using. I have checked that the input from the user is correct and I have attempted to cast the variables as floats and change the order of operations but the problem persists.
This only occurs when using variables so I'm assuming I'm not familiar with some Arduino syntax that isn't present in typical c++.

Any pointers would be appreciated, thanks!

Welcome to the forum

Please post an example of this rather than an example of something else

That's the code I used. When I input 2 into the serial port, leading to 3.14 * 2 * 2 (or 4*3.14), it gives 12.03 instead of 12.56.

Your code works OK for me

I enter 2 and get 12.56 as the output

It will not affect the output of the calculation, but as a matter of interest, what have you got the Line ending set to in the Serial monitor ?

Interesting, I have it set to new line.

And me on an Uno R3

The area is: 
12.56
Enter radius: 
The area is: 
12.56
Enter radius: 
The area is: 
12.56
Enter radius: 
The area is: 
12.56
Enter radius: 

Then presumably when you enter 2 you see two results, the second of which is zero

Yes it does this is my result:

The area is: 
12.03
Enter radius: 
The area is: 
0.00

Did you wonder why you got two putputs ?

Set the Line ending to "No Line ending" as a starting point

To try and track down what is going on with the calculation break the calculation into 2 steps, ie print the result of radius * radius then the result of area and for completeness print the value of pi

I tried both with wokwi and a real Arduino MEGA and got 12.56

So either you did not share the full story, or code or there is something wrong with your board (make sure nothing is connected to it)...

can you share a screen capture of the IDE and Serial monitor?

That's the full code but I will have to come back to it later tomorrow and ensure nothing was connected because now I'm not so sure now.

Out of curiosity, what impact would it have if there was a circuit connected while running the code?

I will try the "no line ending" and break down the code further tomorrow and update you with the results, thank you.

The biggest impact would occur if you have something connected to pins 0 or 1 as they are used by the Serial interface

Okay makes sense, definitely had nothing connected in those pins.

Learn to debug your code by printing ALL the intermediate values and results.

I've done this and it does not get me closer to an answer. It just shows that there is a basic error in the calculation. Like other replies have said, the code works fine on other boards.


Here is a screenshot of the IDE and Serial monitor.

What do you see if you print the intermediate result ?
What do you see if you increase the number of decimal places printed ?
What do you see if you print 2 * 2 ?

As suggested, start debugging by using more print statements. E.g

float radius;
float pi = 3.14;
float area;

void setup()
{

  Serial.begin(115200);
}

void loop()
{
  Serial.print("Enter radius: ");
  while (Serial.available() == 0)
  {
  }
  radius = Serial.parseFloat();
  // debug
  Serial.println(radius);

  // more debug
  Serial.print("Radius square: ");
  Serial.println(radius * radius);

  area = pi * radius * radius;
  Serial.print("The area is: ");
  Serial.println(area);
  Serial.println();
}

Output:

09:35:01.331 -> Enter radius: 2.00
09:35:04.341 -> Radius square: 4.00
09:35:04.341 -> The area is: 12.56
09:35:04.341 -> 
09:35:04.341 -> Enter radius: 

Notes

  1. I've used a baudrate of 115200; your original sketch with 9600 also gave the correct results (as others have confirmed).
  2. Are you using an original Mega or a clone. If the latter, what is the ttl-to-usb converter on the board? It's the IC closest to the USB port.

My board is a clone (Elegoo). The chip I believe you are referring to has:
"ATMEL"
"MEGA16U2"
"2401 TH"
"240 HCR"
Written on it.
Below is my result using your code as well. You can see that my inputs are being read in correctly but the multiplication by 3.14 is what is causing issues.

Enter radius: 2.00
Radius square: 4.00
The area is: 12.03

Enter radius: 

Also, I increased the number of decimals printed and the result was:

Enter radius: 2.00
Radius square: 4.00
The area is: 12.03256060

Enter radius: