Skip to content

Instantly share code, notes, and snippets.

@yuki2006
Last active August 28, 2015 19:20
Show Gist options
  • Save yuki2006/86fc17dfc4512079522f to your computer and use it in GitHub Desktop.
Save yuki2006/86fc17dfc4512079522f to your computer and use it in GitHub Desktop.
C++で、引数の指定によってはsqrt(平方根)の値が異なることがある。 ref: http://qiita.com/yuki2006/items/f9de35affb660e64e895
#include <iostream>
#include <cmath>
#include <iomanip>
#include <assert.h>
using namespace std;
int main(){
long long int calc = sqrt(static_cast<long double>(18014415152484098LL));
cout << calc <<endl;
long long int calc2 = sqrt(static_cast<double>(18014415152484098LL));
cout << calc2 <<endl;
long long int calc3 = sqrt(static_cast<long long int>(18014415152484098LL));
cout << calc3 <<endl;
long long int calc4 = sqrt(18014415152484098LL);
cout << calc4 <<endl;
cout<<setprecision(20)<< 18014415152484098LL <<endl;
cout<<setprecision(20)<< static_cast<double>(18014415152484098LL) <<endl;
cout<<setprecision(20)<< static_cast<long double>(18014415152484098LL) <<endl;
return 0;
}
134217789
134217790
134217790
134217790
18014415152484098
18014415152484096
18014415152484098
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment