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

11th Jul 2017, 4:37 PM
Raúl Moreno
Raúl Moreno - avatar
3 Antworten
+ 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.
11th Jul 2017, 5:30 PM
Hatsy Rei
Hatsy Rei - avatar
+ 6
@Raúl Moreno If that is the case, we may need to inspect the entire code.
12th Jul 2017, 2:14 AM
Hatsy Rei
Hatsy Rei - avatar
+ 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
11th Jul 2017, 7:39 PM
Raúl Moreno
Raúl Moreno - avatar