watch value is different with actually value?

Write your question here.

Hi,every one, I've try to use dev c++ and minwg+code::block
but both side have same result...Is something wrong about zlib1.dll?
I have two computer ,one OS is winxp and one win2000,
all have same problem,in witches window catch value of double always
be wrong , if anyone know why, please save me, thank you!!
I have print screen and past on my facebook.
https://www.facebook.com/olive4953
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <stdio.h>
#include <string>
#include <w32api.h>
#include <stdlib.h>
using namespace std;
int main(){
    char myTest[50];
    double dTest= 1030;
    dTest /=100.0;
    if(dTest==10.30)
        sprintf(myTest,"%f\n",dTest);
    cout << myTest << endl;
  	return 0;
}

the code will print out
10.300000
in the watches window always be
10.300000000000001
why?!
Last edited on
Floating point values are not 100% precise for all values.
there is a tenbyte (80 bit) type on many compilers (its nonstandardish) and you can do the math with 2 of those, and put that into a double, and it MIGHT come out exact, or it may not, but it improves your odds.

The general rule of thumb is don't use == for double apart from checking zero, and even then, its not always the best thing to do.

there is a tiny value in limits or one of the headers, I think it is called eps or epsilon (epsilon is often used symbol for error bounding in math) so you can check
value-eps < value < value + eps or the like, depending on what you need.


Last edited on
Epsilon is a specific value: it is the difference between 1.0 and the next representable number. So it is not directly suitable for that specific task.

A simple way to check for equality, take the absolute value of the difference between number and the expected number, and see if it is less than some precision value: 0.01 say.

There is a fancier example of almost_equal here:

http://en.cppreference.com/w/cpp/types/numeric_limits/epsilon
Thank you very much,so that is not system problem, It's normal.
I got it. thanks.
double compare is wrong way that I write that code, but why witches value is different with
actually value? It should be same .
but why witches value is different with
actually value? It should be same .

It is simply shown with more precision in the watch window than it is presented with when printed. The actual value is the same.
Yes, It's same value!
I've change the code :
using namespace std;
int main(){
char myTest[50];
double dTest1= 1030,dTest2;
dTest1 /=100.0;
sprintf(myTest,"test1=%f\n",dTest1);
cout << myTest << endl;
dTest2 = 10.300000000000001;
sprintf(myTest,"test2=%f\n",dTest2);
cout << myTest << endl;
return 0;
}
screen print out:
test1=10.300000
test2=10.300000

no more question now, thanks everyone!
Topic archived. No new replies allowed.