Hello *,
I was writing an assert() stmt when I realized the following (see code). Can anyone explain why/how. My final aim is to be able to write an assert stmt for verifying that my function (a demo function) works fine.
I heard from someone that there are special assert-macros/comparision-operators for floating point arithmatic. Is it trur?
#include <iostream>
using namespace std ;
int main()
{
double a = 2.2, b = 1234.5678 ;
double c = a * b ;
//accorging to windows' calc.exe 2.2 * 1234.5678 = 2716.04916
//which of course is correct if you multiply manually
if( 2716.04916 == c )
cout << "Cacl.exe and C++ give same results" << endl ;
else
cout << "Cacl.exe gives 2716.04916 and C++ gives "<< c << endl ;
//so we know that assert( 2716.04916 == c ) will fail for sure.
return 0;
}
When I run the code (compiled and run on VC 6.0) this is the output I get:
------------------------------------------------------------------
Cacl.exe gives 2716.04916 and C++ gives 2716.05
Press any key to continue
------------------------------------------------------------------