Boost Library 1.75.0 을 설치하고
Visual Studio 2019 의 명령행 컴파일러 cl 로 컴파일하였다.
컴파일하기 전에 미리 환경변수 BOOST_LIB_PATH 와 MPIR_LIB_PATH 를 각각
Boost Library 와 MPIR Library 가 있는 경로로 설정해 놓아야 한다.
/*
* Filename: CalculateAreaOfDiskWithBoosLibrary.cpp
*
* Purpose: Test floating point numbers of arbitrary precision.
*
* Compile: cl CalculateAreaOfDiskWithBoosLibrary.cpp /I%BOOST_LIB_PATH% /I%MPIR_LIB_PATH% /EHsc /utf-8 MPIR_LIB_PATHmpir.lib
* Execute: CalculateAreaOfDiskWithBoosLibrary
*
* Date: 2021.03.29
* Revised Author: pkim ((AT)) scripts ((DOT)) pe ((DOT)) kr
*
* See: www.boost.org/doc/libs/1_56_0/libs/multiprecision/doc/html/boost_multiprecision/tut/floats/gmp_float.html
*/
#include<iostream>
#include <boost/multiprecision/cpp_bin_float.hpp>
#include <boost/multiprecision/cpp_dec_float.hpp>
#include <boost/math/constants/constants.hpp>
using boost::multiprecision::cpp_dec_float_50;
using boost::multiprecision::cpp_dec_float_100;
using boost::multiprecision::cpp_dec_float;
using boost::multiprecision::cpp_bin_float_quad;
#include <boost/multiprecision/gmp.hpp>
using namespace boost::multiprecision;
typedef boost::multiprecision::number<boost::multiprecision::cpp_dec_float<40> > flp_type_dec_40;
typedef boost::multiprecision::number<boost::multiprecision::cpp_dec_float<49> > flp_type_dec_49;
typedef boost::multiprecision::number<boost::multiprecision::cpp_dec_float<99> > flp_type_dec_99;
typedef boost::multiprecision::number<boost::multiprecision::cpp_dec_float<101> > flp_type_dec_101;
using namespace std;
template<typename T>
inline T area_of_a_circle(T r)
{
// pi represent predefined constant having value
// 3.1415926535897932384...
using boost::math::constants::pi;
return pi<T>() * r * r;
}
void testFloatNumbers()
{
float radius_f = 123.0/ 100;
float area_f = area_of_a_circle(radius_f);
double radius_d = 123.0 / 100;
double area_d = area_of_a_circle(radius_d);
cpp_dec_float_50 r_mp = 123.0 / 100;
cpp_dec_float_50 area_mp = area_of_a_circle(r_mp);
cpp_dec_float_100 r_100 = 123.0 / 100;
cpp_dec_float_100 area_100 = area_of_a_circle(r_100);
cpp_bin_float_quad r_quad= 123.0 / 100;
cpp_bin_float_quad area_quad = area_of_a_circle(r_quad);
flp_type_dec_40 r_40 = 123.0 / 100;
flp_type_dec_40 area_40 = area_of_a_circle(r_40);
flp_type_dec_49 r_49 = 123.0 / 100;
flp_type_dec_49 area_49 = area_of_a_circle(r_49);
flp_type_dec_99 r_99 = 123.0 / 100;
flp_type_dec_99 area_99 = area_of_a_circle(r_99);
flp_type_dec_101 r_101 = 123.0 / 100;
flp_type_dec_101 area_101 = area_of_a_circle(r_101);
using boost::math::constants::pi;
cout << "Let be given a circle with radius, r = " << radius_d << endl;
cout << "Calculate the area of the given circle,\n Area = PI*r^2 = " << pi<double>() << "*" << radius_d << "^2 = " << pi<double>() << "*" << (radius_d * radius_d) << ",\nin various precisions." << endl << endl;;
// numeric_limits::digits10 represent the number
// of decimal digits that can be held of particular
// data type without any loss.
// Area by using float data type
cout << " Float: "
<< setprecision(numeric_limits<float>::digits10)
<< area_f << endl;
// Area by using double data type
cout << "Double: "
<<setprecision(numeric_limits<double>::digits10)
<< area_d << endl;
// Area by using Boost 40 precision Multiprecision
cout << "Boost Multiprecision (40 digits): "
<< setprecision(numeric_limits<flp_type_dec_40>::digits10)
<< area_49 << endl;
// Area by using Boost 49 precision Multiprecision
cout << "Boost Multiprecision (49 digits): "
<< setprecision(numeric_limits<flp_type_dec_49>::digits10)
<< area_49 << endl;
// Area by using Boost 50 precision Multiprecision
cout << "Boost Multiprecision (50 digits): "
<< setprecision(numeric_limits<cpp_dec_float_50>::digits10)
<< area_mp << endl;
// Area by using Boost 99 precision Multiprecision
cout << "Boost Multiprecision (99 digits): "
<< setprecision(numeric_limits<flp_type_dec_99>::digits10)
<< area_99 << endl;
// Area by using Boost 100 precidion Multiprecision
cout << "Boost Multiprecision (100 digits): "
<< setprecision(numeric_limits<cpp_dec_float_100>::digits10)
<< area_100 << endl;
// Area by using Boost 101 precision Multiprecision
cout << "Boost Multiprecision (101 digits): "
<< setprecision(numeric_limits<flp_type_dec_101>::digits10)
<< area_100 << endl;
// Area by using Boost Quaduple Multiprecision
cout << "Boost Multiprecision (Quadruple): "
<< setprecision(numeric_limits<cpp_bin_float_quad>::digits10)
<< area_quad << endl;
}
int main()
{
testFloatNumbers();
return 0;
}
/*
Output:
-------------------------------------------
Let be given a circle with radius, r = 1.23
Calculate the area of the given circle,
Area = PI*r^2 = 3.14159*1.23^2 = 3.14159*1.5129,
in various precisions.
Float: 4.75292
Double: 4.752915525616
Boost Multiprecision (40 digits): 4.752915525615998053187629092943809341311
Boost Multiprecision (49 digits): 4.752915525615998053187629092943809341310825398145
Boost Multiprecision (50 digits): 4.7529155256159980531876290929438093413108253981451
Boost Multiprecision (99 digits): 4.75291552561599805318762909294380934131082539814514244132288567973008216166037385350291572422559648
Boost Multiprecision (100 digits): 4.752915525615998053187629092943809341310825398145142441322885679730082161660373853502915724225596483
Boost Multiprecision (101 digits): 4.7529155256159980531876290929438093413108253981451424413228856797300821616603738535029157242255964827
Boost Multiprecision (Quadruple): 4.75291552561599805318762909294381
-------------------------------------------
*/
'프로그래밍 > C++' 카테고리의 다른 글
부동소수점수의 소수점 아래에 홑 따옴표 문자 ' 붙여서 가독성 높이기 (0) | 2023.01.03 |
---|---|
맥 OS X에 gmplib 6.2.1 설치하고 g++로 설치 테스트하기 (0) | 2022.03.19 |
new 키워드로 생성된 배열의 크기 변경하여 재할당하기 (0) | 2021.02.24 |
utf8 인코딩을 이용한 다국어 hello world (0) | 2021.01.23 |
C++ 에서 C 함수를 불러 사용하기 (0) | 2021.01.07 |