0
Weird lost of variable c++
I am trying to translate a code from matlab to c++. But i've experienced serious problems with that. Today i am looking for help about a error in which i assing a value for a variable i use it without redefining but when i verify its value has change. It goes: double dt = 3e-14; double verif = dt; //here is ok while (En_k[i] <=100){ trace[i] = trace[i-1] + v*dt + a/2*dt^2; vel=vel + a*dt; En_k[i+1] = ... time[i+1] = time[i] + dt; //but when i verify dt if (verif !=dt){ cout << "error";} \\happens
3 odpowiedzi
+ 6
Please refer:
https://stackoverflow.com/questions/590822/dealing-with-accuracy-problems-in-floating-point-numbers
As to why strict equality should not be used to evaluate float/double values, and how to overcome the issue.
+ 6
@Raúl Moreno
If that is the case, we may need to inspect the entire code.
+ 2
to extend my explanation when i do:
double dt =3e-14;
double verif=dt;
cout<< verif; // prints 3 e-14
//but after operations
cout << verif; // prints something diferent, example 35358