1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
|
///////////////////////////////////////////////////////////////
// Copyright Christopher Kormanyos 2002 - 2011.
// Copyright 2011 John Maddock. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt
//
// This work is based on an earlier work:
// "Algorithm 910: A Portable C++ Multiple-Precision System for Special-Function Calculations",
// in ACM TOMS, {VOL 37, ISSUE 4, (February 2011)} (C) ACM, 2011. http://doi.acm.org/10.1145/1916461.1916469
#ifdef _MSC_VER
#define _SCL_SECURE_NO_WARNINGS
#endif
#include <boost/detail/lightweight_test.hpp>
#include "test.hpp"
#include <boost/multiprecision/gmp.hpp>
template <class T>
T make_rvalue_copy(const T a)
{
return a;
}
int main()
{
using namespace boost::multiprecision;
//
// Test change of default precision:
//
mpf_float::default_precision(100);
mpf_float a("0.1");
BOOST_CHECK_GE(a.precision(), 100);
mpf_float::default_precision(20);
{
// test assignment from lvalue:
mpf_float b(2);
BOOST_CHECK_GE(b.precision(), 20);
b = a;
BOOST_CHECK_EQUAL(b.precision(), a.precision());
}
{
// test assignment from rvalue:
mpf_float b(2);
BOOST_CHECK_GE(b.precision(), 20);
b = make_rvalue_copy(a);
BOOST_CHECK_EQUAL(b.precision(), a.precision());
}
mpf_float::default_precision(20);
{
// test construct from lvalue:
mpf_float b(a);
BOOST_CHECK_EQUAL(b.precision(), a.precision());
}
{
// test construct from rvalue:
mpf_float b(make_rvalue_copy(a));
BOOST_CHECK_EQUAL(b.precision(), a.precision());
}
{
mpf_float f150(2, 150);
BOOST_CHECK_GE(f150.precision(), 150);
}
{
mpf_float f150("1.2", 150);
BOOST_CHECK_GE(f150.precision(), 150);
}
//
// From https://github.com/boostorg/multiprecision/issues/65
//
{
mpf_float a(2);
a.precision(100);
BOOST_CHECK_EQUAL(a, 2);
BOOST_CHECK_GE(a.precision(), 100);
}
//
// string_view with explicit precision:
//
#ifndef BOOST_NO_CXX17_HDR_STRING_VIEW
{
std::string s("222");
std::string_view v(s.c_str(), 1);
mpf_float f(v, 100);
BOOST_CHECK_EQUAL(f, 2);
BOOST_CHECK_GE(f.precision(), 100);
}
#endif
// Swap:
{
mpf_float x(2, 100); // 100 digits precision.
mpf_float y(3, 50); // 50 digits precision.
swap(x, y);
BOOST_CHECK_EQUAL(x, 3);
BOOST_CHECK_EQUAL(y, 2);
BOOST_CHECK_GE(x.precision(), 50);
BOOST_CHECK_GE(y.precision(), 100);
x.swap(y);
BOOST_CHECK_EQUAL(x, 2);
BOOST_CHECK_EQUAL(y, 3);
BOOST_CHECK_GE(x.precision(), 100);
BOOST_CHECK_GE(y.precision(), 50);
x = std::move(mpf_float(y));
BOOST_CHECK_EQUAL(x, y);
BOOST_CHECK_EQUAL(x.precision(), y.precision());
}
{
mpf_float c(4), d(8), e(9), f;
f = (c + d) * d / e;
mpf_float g((c + d) * d / e);
}
{
mpf_float::default_precision(100);
mpf_float f1;
f1 = 3;
BOOST_CHECK_GE(f1.precision(), 100);
f1 = 3.5;
BOOST_CHECK_GE(f1.precision(), 100);
mpf_float f2(3.5);
BOOST_CHECK_GE(f2.precision(), 100);
mpf_float f3("5.1");
BOOST_CHECK_GE(f3.precision(), 100);
mpf_float::default_precision(50);
mpf_float f4(f3, 50);
BOOST_CHECK_GE(f4.precision(), 50);
f4.assign(f1, f4.precision());
BOOST_CHECK_GE(f4.precision(), 50);
}
return boost::report_errors();
}
|