# Thread: problem with relational operators

1. ## problem with relational operators

When I run this program everything works fine except I cannot enter 0.016 for thickness. Can anyone give me some help on what the problem might be?

Code:
```#include <iostream>
#include <iomanip>
using namespace std;

int main ()
{
float length;
float height;
float thickness;
float weight;
cout.setf(ios::fixed, ios::floatfield);
cout.setf(ios::showpoint);
cout << setprecision(2);
cout << "Please enter the package length(inches): ";
cin >> length;
cout << "\nHeight(inches): ";
cin >> height;
cout << "\nThickness(inches): ";
cin >> thickness;
cout << "\nWeight(pounds): ";
cin >> weight;
if (length >= 3.5 && length <= 4.25)
{
if (height >= 3.5 && height <= 6)
{
if (thickness >= 0.007 && thickness <= 0.016)
{
cout << "\nYour post card is a regular post card. ";
cout << "The cost to mail your post card is \$" << ((weight / 0.0625) * 0.2)
<< ".";
}
}
}
else if (length > 4.25 && length < 6)
{
if (height > 6 && height < 11.5)
{
if (thickness >= 0.007 && thickness <= 0.016)
{
cout << "\nYour post card is a large post card. ";
cout << "The cost to mail your post card is \$" << ((weight / 0.0625) * 0.3)
<< ".";
}
}
}
return 0;
}```

2. Code:
`if (thickness >= 0.007 && thickness <= 0.016)`
That's kind of dangerous to do with floating point numbers. The number "0.016" that you enter might be stored as "0.016000102", making that expression false.

You might want to round the number, or compare it to 0.0164 or something.

3. but the thing is, I will need to use 0.016 specifically because the specifications for an envelope thickness will be between .016 and .25(not inclusive). I havn't done this part yet because I want to get past this first. Also, if I enter .007 it works fine.

4. Then round the number:
Code:
```#include <iostream>

int main(void) {
double unrounded = .016004;
double rounded = (double)((int)((unrounded*1000)+.5)/1000.0);

std::cout << unrounded << std::endl << rounded << std::endl;

return 0;
}```
That should output
Code:
```0.016400
0.016000```
although I don't have a compiler here and I can't tell you for sure.

5. Also, if I enter .007 it works fine.
Perhaps your .007 is being stored as .007001004, making the expression true.

6. You might want to do some research on how Floats are stored in the machine. To put it simply, floats aren't exactly as precise as you might think.

7. Especially where == and != are concerned. This code might not output anything, depending on SOME_CONSTANT:
Code:
```double x = SOME_CONSTANT;

if(x == SOME_CONSTANT) {
std::cout << "equal\n";
}```

8. I pluged this in and it work:

Code:
`if (thickness >= 0.007 && thickness <= (((0.016 * 1000) + .5) / 1000))`
you're good dwks

9. Store everything internally as 1000th's of an inch, then all your comparisons become integer comparisons.

10. Originally Posted by dwks
Then round the number:
Code:
```#include <iostream>

int main(void) {
double unrounded = .016004;
double rounded = (double)((int)((unrounded*1000)+.5)/1000.0);

std::cout << unrounded << std::endl << rounded << std::endl;

return 0;
}```
That should output
Code:
```0.016400
0.016000```
although I don't have a compiler here and I can't tell you for sure.
Weather I round it or not it just automatically rounds up to .02. Do you think it could be a problem with the compiler that I'm using?

11. no, its not the compiler at all, its simply how floating point numbers are stored in binary. Seriously, do some research on the format, and try the conversion for yourself.

EDIT: Did a quick search for you
http://en.wikipedia.org/wiki/IEEE_fl...point_standard
This might give you some insight
http://en.wikipedia.org/wiki/Binary_...system#Decimal
Check that link on how to convert from decimal into binary