long double xxx = 0.0001000001000001;
std::cout << "right: " << 0.0001000001000001e21 << std::endl;
std::cout << "wrong: " << xxx*1e21 << std::endl;
std::cout << "wrong: " << xxx*10000000000000000*100000 << std::endl;
for(int yyy = 0; yyy < 21; yyy++){
xxx = xxx * 10;
}
std::cout << "wrong: " << xxx << std::endl;
right: 100000100000100000
wrong: 100000100000100002.3359375
wrong: 100000100000100002.328125
wrong: 100000100000100002.328125
Where I point out right & wrong by common sense and help of math&calculator. Can someone explain WHY? And how can I avoid this?
Thanks, Janis
On 2015–04–30, at 5:01 PM, Janis Taranda <janis....@gmail.com> wrote:Where I point out right & wrong by common sense and help of math&calculator. Can someone explain WHY?
And how can I avoid this?
long double xxx = 1000001000001L / 1e17L;
for(int yyy = 0; yyy < 21; yyy++){
xxx = xxx * 10L;
}
std::cout << "wrong: " << xxx << std::endl;
wrong: 10000010000009999.9990234375
The only solution I see, is to round it, when ^17 is reached. But in my code, it's very hard to get those point. This is just a snippet from a larger project. :(
Best, Janis
xxx to as string, add + 'e21', and than back to double. Then I get a clean number, but feels stupid to go such long way :(
On 2015–04–30, at 5:55 PM, Janis Taranda <janis....@gmail.com> wrote:wrong: 10000010000009999.9990234375
The only solution I see, is to round it, when ^17 is reached. But in my code, it's very hard to get those point. This is just a snippet from a larger project. :(