CDT++ 1.0.0-rc1
Causal Dynamical Triangulations in C++
Loading...
Searching...
No Matches
Mpfr_value.hpp
Go to the documentation of this file.
1/*******************************************************************************
2 Causal Dynamical Triangulations in C++ using CGAL
3
4 Copyright © 2026 Adam Getchell
5 ******************************************************************************/
6
9
10#ifndef CDT_PLUSPLUS_MPFR_VALUE_HPP
11#define CDT_PLUSPLUS_MPFR_VALUE_HPP
12
13#include <CGAL/Gmpfr.h>
14
15#include <stdexcept>
16#include <type_traits>
17
18#include "Settings.hpp"
19
20namespace mpfr_values
21{
22 using Value = CGAL::Gmpfr;
23
29 static inline auto constexpr rounding_mode = MPFR_RNDN;
30
31 static_assert(std::is_nothrow_destructible_v<Value>,
32 "MPFR values must release their resources without throwing.");
33
34 inline auto constexpr precision =
35 static_cast<Value::Precision_type>(PRECISION);
36
37 [[nodiscard]] inline auto zero() -> Value { return Value{0, precision}; }
38
39 [[nodiscard]] inline auto from_integer(long const value) -> Value
40 {
41 auto result = zero();
42 mpfr_set_si(result.fr(), value, rounding_mode);
43 return result;
44 }
45
46 [[nodiscard]] inline auto from_long_double(long double const value) -> Value
47 {
48 auto result = zero();
49 mpfr_set_ld(result.fr(), value, rounding_mode);
50 return result;
51 }
52
53 [[nodiscard]] inline auto from_decimal(char const* value) -> Value
54 {
55 if (value == nullptr)
56 {
57 throw std::invalid_argument("MPFR decimal value must not be null.");
58 }
59 auto result = zero();
60 if (mpfr_set_str(result.fr(), value, 10, rounding_mode) != 0)
61 {
62 throw std::invalid_argument("Invalid decimal MPFR value.");
63 }
64 return result;
65 }
66
67 [[nodiscard]] inline auto pi() -> Value
68 {
69 auto result = zero();
70 mpfr_const_pi(result.fr(), rounding_mode);
71 return result;
72 }
73
74 [[nodiscard]] inline auto add(Value const& left, Value const& right) -> Value
75 {
76 auto result = zero();
77 mpfr_add(result.fr(), left.fr(), right.fr(), rounding_mode);
78 return result;
79 }
80
81 [[nodiscard]] inline auto subtract(Value const& left, Value const& right)
82 -> Value
83 {
84 auto result = zero();
85 mpfr_sub(result.fr(), left.fr(), right.fr(), rounding_mode);
86 return result;
87 }
88
89 [[nodiscard]] inline auto multiply(Value const& left, Value const& right)
90 -> Value
91 {
92 auto result = zero();
93 mpfr_mul(result.fr(), left.fr(), right.fr(), rounding_mode);
94 return result;
95 }
96
97 [[nodiscard]] inline auto divide(Value const& numerator,
98 Value const& denominator) -> Value
99 {
100 auto result = zero();
101 mpfr_div(result.fr(), numerator.fr(), denominator.fr(), rounding_mode);
102 return result;
103 }
104
105 [[nodiscard]] inline auto square_root(Value const& value) -> Value
106 {
107 auto result = zero();
108 mpfr_sqrt(result.fr(), value.fr(), rounding_mode);
109 return result;
110 }
111
112 [[nodiscard]] inline auto inverse_hyperbolic_sine(Value const& value) -> Value
113 {
114 auto result = zero();
115 mpfr_asinh(result.fr(), value.fr(), rounding_mode);
116 return result;
117 }
118
119 [[nodiscard]] inline auto arc_cosine(Value const& value) -> Value
120 {
121 auto result = zero();
122 mpfr_acos(result.fr(), value.fr(), rounding_mode);
123 return result;
124 }
125
126 [[nodiscard]] inline auto negate(Value const& value) -> Value
127 {
128 auto result = zero();
129 mpfr_neg(result.fr(), value.fr(), rounding_mode);
130 return result;
131 }
132
133 [[nodiscard]] inline auto exponential(Value const& value) -> Value
134 {
135 auto result = zero();
136 mpfr_exp(result.fr(), value.fr(), rounding_mode);
137 return result;
138 }
139
140 [[nodiscard]] inline auto to_double(Value const& value) -> double
141 { return mpfr_get_d(value.fr(), rounding_mode); }
142
143 [[nodiscard]] inline auto to_long_double(Value const& value) -> long double
144 { return mpfr_get_ld(value.fr(), rounding_mode); }
145} // namespace mpfr_values
146
147#endif // CDT_PLUSPLUS_MPFR_VALUE_HPP
static auto constexpr rounding_mode
Global integer and precision settings.
static Int_precision constexpr PRECISION
Sets the precision for MPFR.
Definition Settings.hpp:40