CDT++ 1.0.0
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 cdt::mpfr_values
21{
23 using Value = CGAL::Gmpfr;
24
30 inline constexpr auto rounding_mode = MPFR_RNDN;
31
32 static_assert(std::is_nothrow_destructible_v<Value>,
33 "MPFR values must release their resources without throwing.");
34
36 inline constexpr auto precision =
37 static_cast<Value::Precision_type>(PRECISION);
38
40 [[nodiscard]] inline auto zero() -> Value { return Value{0, precision}; }
41
44 [[nodiscard]] inline auto from_integer(long const value) -> Value
45 {
46 auto result = zero();
47 mpfr_set_si(result.fr(), value, rounding_mode);
48 return result;
49 }
50
53 [[nodiscard]] inline auto from_long_double(long double const value) -> Value
54 {
55 auto result = zero();
56 mpfr_set_ld(result.fr(), value, rounding_mode);
57 return result;
58 }
59
64 [[nodiscard]] inline auto from_decimal(char const* value) -> Value
65 {
66 if (value == nullptr)
67 {
68 throw std::invalid_argument("MPFR decimal value must not be null.");
69 }
70 auto result = zero();
71 if (mpfr_set_str(result.fr(), value, 10, rounding_mode) != 0)
72 {
73 throw std::invalid_argument("Invalid decimal MPFR value.");
74 }
75 return result;
76 }
77
79 [[nodiscard]] inline auto pi() -> Value
80 {
81 auto result = zero();
82 mpfr_const_pi(result.fr(), rounding_mode);
83 return result;
84 }
85
89 [[nodiscard]] inline auto add(Value const& left, Value const& right) -> Value
90 {
91 auto result = zero();
92 mpfr_add(result.fr(), left.fr(), right.fr(), rounding_mode);
93 return result;
94 }
95
99 [[nodiscard]] inline auto subtract(Value const& left, Value const& right)
100 -> Value
101 {
102 auto result = zero();
103 mpfr_sub(result.fr(), left.fr(), right.fr(), rounding_mode);
104 return result;
105 }
106
110 [[nodiscard]] inline auto multiply(Value const& left, Value const& right)
111 -> Value
112 {
113 auto result = zero();
114 mpfr_mul(result.fr(), left.fr(), right.fr(), rounding_mode);
115 return result;
116 }
117
121 [[nodiscard]] inline auto divide(Value const& numerator,
122 Value const& denominator) -> Value
123 {
124 auto result = zero();
125 mpfr_div(result.fr(), numerator.fr(), denominator.fr(), rounding_mode);
126 return result;
127 }
128
131 [[nodiscard]] inline auto square_root(Value const& value) -> Value
132 {
133 auto result = zero();
134 mpfr_sqrt(result.fr(), value.fr(), rounding_mode);
135 return result;
136 }
137
140 [[nodiscard]] inline auto inverse_hyperbolic_sine(Value const& value) -> Value
141 {
142 auto result = zero();
143 mpfr_asinh(result.fr(), value.fr(), rounding_mode);
144 return result;
145 }
146
149 [[nodiscard]] inline auto arc_cosine(Value const& value) -> Value
150 {
151 auto result = zero();
152 mpfr_acos(result.fr(), value.fr(), rounding_mode);
153 return result;
154 }
155
158 [[nodiscard]] inline auto negate(Value const& value) -> Value
159 {
160 auto result = zero();
161 mpfr_neg(result.fr(), value.fr(), rounding_mode);
162 return result;
163 }
164
167 [[nodiscard]] inline auto exponential(Value const& value) -> Value
168 {
169 auto result = zero();
170 mpfr_exp(result.fr(), value.fr(), rounding_mode);
171 return result;
172 }
173
176 [[nodiscard]] inline auto to_double(Value const& value) -> double
177 { return mpfr_get_d(value.fr(), rounding_mode); }
178
181 [[nodiscard]] inline auto to_long_double(Value const& value) -> long double
182 { return mpfr_get_ld(value.fr(), rounding_mode); }
183} // namespace cdt::mpfr_values
184
185#endif // CDT_PLUSPLUS_MPFR_VALUE_HPP
auto multiply(Value const &left, Value const &right) -> Value
auto square_root(Value const &value) -> Value
auto negate(Value const &value) -> Value
constexpr auto rounding_mode
auto from_integer(long const value) -> Value
auto from_long_double(long double const value) -> Value
auto inverse_hyperbolic_sine(Value const &value) -> Value
auto subtract(Value const &left, Value const &right) -> Value
auto pi() -> Value
auto add(Value const &left, Value const &right) -> Value
auto from_decimal(char const *value) -> Value
auto to_double(Value const &value) -> double
auto arc_cosine(Value const &value) -> Value
auto exponential(Value const &value) -> Value
auto divide(Value const &numerator, Value const &denominator) -> Value
auto zero() -> Value
auto to_long_double(Value const &value) -> long double
constexpr auto precision
Precision in bits assigned to values created by this namespace.
CGAL::Gmpfr Value
Owning arbitrary-precision floating-point value used by CDT++.
Global integer and precision settings.
constexpr Int_precision PRECISION
Sets the precision for MPFR.
Definition Settings.hpp:44