Home Segments Index Top Previous Next

269: Mainline

Note that you must have a program perform a cast, of the sort you learned about in Segment 69, if you want the program to compare an integer with a floating-point number. The following program illustrates how such comparisons must be performed:

#include   
main ( ) { 
  int i = 50; 
  double d = 50.0; 
  cout << "i == (int) d    yields " << (i == (int) d)    << endl; 
  cout << "(double) i != d yields " << ((double) i != d) << endl; 
  cout << "i > (int) d     yields " << (i > (int) d)     << endl; 
  cout << "(double) i < d  yields " << ((double) i < d)  << endl; 
  cout << "i >= (int) d    yields " << (i >= (int) d)    << endl; 
  cout << "(double) i <= d yields " << ((double) i <= d) << endl; 
} 
--- Result --- 
i == (int) d    yields 1 
(double) i != d yields 0 
i > (int) d     yields 0 
(double) i < d  yields 0 
i >= (int) d    yields 1 
(double) i <= d yields 1