Arithmetic Burst C++

Asked

Viewed 88 times

1

Hello, I am a beginner in the C++ language, doing some exercises I came across a message that did not interfere in anything my account, but I would like to know exactly what it means and what I would have to do to make this calculation 100% right for the c++.

#include <iostream>
#include <locale.h>
using namespace std;

int main() {
    setlocale(LC_ALL, "");

    float r, a;

    cout << "Digite o raio de um circulo: ";
        cin >> r;

        a=3.14 * (r * r);//Erro de estouro nessa linha

        cout << "\nA área de um circulo com " << r << " de raio é igual a " << a <<"\n\n";


    return 0;
}

C26451: Arithmetic overflow using the operator '' a value of byte 4 and then converting the result to a value of byte 8. Convert the value to the widest type before calling the operator '' to avoid overflow (io.2).

From now on, thank you.

  • The constant 3.14 is considered to be of the type double and you declared float r, a;. To work only as float utilize a = 3.14f * (r * r);. (an f at the end of the floating point constant indicates that it is of the float type).

2 answers

0


The guy double is more accurate than the type float.

You can change the varietal type r and a for double or change the literal to 3.14f to be interpreted as float. If you don’t put the suffix f the value will be interpreted as double.

  • Very interesting, after some researches I managed to understand the difference from float to double, thank you very much for the help!

0

The warning C26451 is fired to indicate that the current operator is used in types that contain a greater amount of information, that is, there is a chance of data loss (accuracy of the numbers results of the expression). For this, you can try to convert the type of the variable during operation as in the link examples I left reference.

  • It helped a lot, thank you very much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.