0

recently I bump into a problem while comparing a double in an if statement. I was trying to cout the number of whole numbers in a double. Being a beginner, I am not sure what gone wrong in my code.

This is my code:

#include <iostream>

using namespace std;

int main(){
int x=0;//convert double to int
long double Out;//Result
long double In=10;//Input double

//Loop Begin
while(In>0){
x=In;//convert double to int
Out= (x/In);//Out(test if whole number, will return 1)

//test for 1
////////////////
if(Out == 1 ){
    cout<<"[Whole Number] ";
}
////////////////
//test end

cout<<"In :"<<In<<", ";
cout<<"X :"<<x<<", ";
cout<<"Out :"<<Out<<endl;
In-=0.1;//decrease to finish loop (eventually)
}
//Loop End

cin.get();
return 0;
}

This program will test and output the whole numbers in the double (In). I realized that the accuracy of the double was affecting the if statement which is why I can't get the "[Whole Number]" result. Although I found out that if I used (0.9999) in "if(Out >= 0.9999)" the comparison would work. But I am not sure of a solution, please help! Much appreciated!

firepants
  • 1
  • 1
  • 1
    possible duplicate of [How should I do floating point comparison?](http://stackoverflow.com/questions/4915462/how-should-i-do-floating-point-comparison) – Slava Nov 04 '14 at 18:49

5 Answers5

0

Your while loop never stops , its a infinite loop . You are not doing anything with the value of "In" in the while loop hence it will always be greater than 0 ,therefore a infinite loop .

voltaa7
  • 203
  • 1
  • 8
0

You should probably approach the problem more directly with modf:

double int_part, frac_part;

frac_part = std::modf(in, &int_part);

if (frac_part == 0) {
  // int_part contains integer value.
} else {
  // process the double non-integer floating point value.
}
Gene
  • 46,253
  • 4
  • 58
  • 96
0

Your code works perfectly fine. If you subtract 0.1 from 10.0, then chances are that the result is not an integer due to rounding errors, and your code tells you exactly that. The code isn't wrong, your expectations are wrong.

if (Out >= 0.9999)

is obviously not a solution, because it will always be true if In >= 10000.0.

gnasher729
  • 51,477
  • 5
  • 75
  • 98
0

Do to the way floating point numbers are converted to binary representation by the computer they are inherently inaccurate and thus make logical comparisons somewhat challenging (http://en.wikipedia.org/wiki/Floating_point#Accuracy_problems). When performing such comparisons to floating point numbers you typically will do so utilizing an epsilon constant (http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm) that represents the maximum acceptable error in the comparison. In your case you need to select a suitable value for epsilon (say .000001). Then change your comparison to:

if(abs(out - 1) < epsilon){ //Take the difference between out and 1
    cout<<"[Whole Number]"; //If it is "close enough" print to console

}

I am more of a Java guy but I believe you will need #include stdlib.h to utilize the abs() function.

Hope that helps!

-2

Try using the modulus operator: http://www.cprogramming.com/tutorial/modulus.html

Something like if(In % 1 == 0) should work.

Toby
  • 56
  • 2