/**************************************************************** **** **** This file belongs with the course **** Introduction to Scientific Programming in C++/Fortran2003 **** copyright 2016-2021 Victor Eijkhout eijkhout@tacc.utexas.edu **** **** equals.cxx : equality in floating point is tricky **** ****************************************************************/ #include using std::cin, std::cout; #include using std::setw; using std::scientific; using std::setprecision; #include using std::vector; template < typename floattype,char ft > void testfloat( int div ) { floattype one = static_cast(1); floattype reciprocal = one/static_cast(div); cout << ft << ": "; if (ft=='s') cout << setw(2) << div << " * 1/" << setw(2) << div << " - 1 = "; else cout << " "; cout << scientific << setprecision(3) << div*reciprocal-one << "; "; if (ft=='s') cout << setw(2) << div << "^2 * 1/" << setw(2) << div << "^2 -1 = "; else cout << " "; cout << scientific << setprecision(3) << div*div*(reciprocal*reciprocal)-one << '\n'; } int main() { int div; cin >> div; testfloat(div); testfloat(div); return 0; }