fork(1) download
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cmath>
  4. using namespace std;
  5.  
  6. int main() {
  7. double d_total = 1.2345678;
  8. float f_total = (float)d_total;
  9. double some_dbl = 6.7809123;
  10.  
  11. double actual = (d_total * some_dbl);
  12. float no_cast = (float)(f_total * some_dbl);
  13. float with_cast = (float)(f_total * (float)some_dbl);
  14.  
  15. cout << "actual: " << setprecision(25) << actual << endl;
  16. cout << "no_cast: " << setprecision(25) << no_cast << endl;
  17. cout << "with_cast: " << setprecision(25) << with_cast << endl;
  18. cout << "no_cast, nextafter: " << setprecision(25) << nextafter(no_cast, 500.0f) << endl;
  19.  
  20. cout << endl;
  21.  
  22. cout << "Diff no_cast: " << setprecision(25) << actual - no_cast << endl;
  23. cout << "Diff with_cast: " << setprecision(25) << with_cast - actual << endl;
  24. return 0;
  25. }
Success #stdin #stdout 0s 3340KB
stdin
Standard input is empty
stdout
actual:               8.371495980203938813701825
no_cast:              8.37149524688720703125
with_cast:            8.3714962005615234375
no_cast, nextafter:   8.3714962005615234375

Diff no_cast:   7.333167317824518249835819e-07
Diff with_cast: 2.203575846237981750164181e-07