1 #ifndef BOOST_REAL_REAL_HELPERS_HPP 2 #define BOOST_REAL_REAL_HELPERS_HPP 6 #include <real/interval.hpp> 7 #include <real/boundary.hpp> 13 boundary abs(
const boundary& b) {
15 result.positive =
true;
33 int add_vectors(
const std::vector<int> &lhs,
35 const std::vector<int> &rhs,
37 std::vector<int> &result) {
40 int fractional_length = std::max((
int)lhs.size() - lhs_exponent, (int)rhs.size() - rhs_exponent);
41 int integral_length = std::max(lhs_exponent, rhs_exponent);
44 for (
int i = fractional_length - 1; i >= -integral_length; i--) {
47 if (0 <= lhs_exponent + i && lhs_exponent + i < (
int)lhs.size()) {
48 lhs_digit = lhs[lhs_exponent + i];
52 if (0 <= rhs_exponent + i && rhs_exponent + i < (
int)rhs.size()) {
53 rhs_digit = rhs[rhs_exponent + i];
56 int digit = carry + lhs_digit + rhs_digit;
65 result.insert(result.begin(), digit);
69 result.insert(result.begin(), 1);
73 return integral_length;
92 int subtract_vectors(
const std::vector<int> &lhs,
94 const std::vector<int> &rhs,
96 std::vector<int> &result) {
98 int fractional_length = std::max((
int)lhs.size() - lhs_exponent, (int)rhs.size() - rhs_exponent);
99 int integral_length = std::max(lhs_exponent, rhs_exponent);
103 for (
int i = fractional_length - 1; i >= -integral_length; i--) {
106 if (0 <= lhs_exponent + i && lhs_exponent + i < (
int)lhs.size()) {
107 lhs_digit = lhs[lhs_exponent + i];
111 if (0 <= rhs_exponent + i && rhs_exponent + i < (
int)rhs.size()) {
112 rhs_digit = rhs[rhs_exponent + i];
115 if (lhs_digit < borrow) {
116 lhs_digit += (10 - borrow);
122 if (lhs_digit < rhs_digit) {
127 result.insert(result.begin(), lhs_digit - rhs_digit);
147 int multiply_vectors(
148 const std::vector<int>& lhs,
150 const std::vector<int>& rhs,
152 std::vector<int>& result
158 size_t new_size = lhs.size() + rhs.size();
159 if (lhs_exponent < 0) new_size -= lhs_exponent;
160 if (rhs_exponent < 0) new_size -= rhs_exponent;
162 if (!result.empty()) result.clear();
163 for (
int i = 0; i < (int)new_size; i++) result.push_back(0);
169 auto i_n1 = (int) result.size() - 1;
172 for (
int i = (
int)lhs.size()-1; i>=0; i--) {
180 for (
int j = (
int)rhs.size()-1; j>=0; j--) {
184 int sum = lhs[i]*rhs[j] + result[i_n1 - i_n2] + carry;
190 result[i_n1 - i_n2] = sum % 10;
197 result[i_n1 - i_n2] += carry;
205 int fractional_part = ((int)lhs.size() - lhs_exponent) + ((
int)rhs.size() - rhs_exponent);
206 int result_exponent = (int)result.size() - fractional_part;
208 return result_exponent;
220 void add_boundaries(
const boundary &lhs,
223 if (lhs.positive == rhs.positive) {
224 result.exponent = add_vectors(lhs.digits,
229 result.positive = lhs.positive;
230 }
else if (abs(rhs) < abs(lhs)) {
231 result.exponent = subtract_vectors(lhs.digits,
236 result.positive = lhs.positive;
238 result.exponent = subtract_vectors(rhs.digits,
243 result.positive = rhs.positive;
258 void subtract_boundaries(
const boundary &lhs,
261 if (lhs.positive != rhs.positive) {
262 result.exponent = add_vectors(lhs.digits,
267 result.positive = lhs.positive;
270 if (abs(rhs) < abs(lhs)) {
271 result.exponent = subtract_vectors(lhs.digits,
276 result.positive = lhs.positive;
278 result.exponent = subtract_vectors(rhs.digits,
283 result.positive = !lhs.positive;
299 void multiply_boundaries(
const boundary &lhs,
303 result.positive = lhs.positive == rhs.positive;
304 result.exponent = multiply_vectors(lhs.digits,
316 #endif //BOOST_REAL_REAL_HELPERS_HPP Definition: boundary.hpp:7