Skip to content

Commit

Permalink
finished exponential, made changes to division
Browse files Browse the repository at this point in the history
  • Loading branch information
universenox authored and sdavtaker committed Jul 28, 2019
1 parent 71fa91a commit 07c5363
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 20 deletions.
Binary file removed a.out
Binary file not shown.
57 changes: 56 additions & 1 deletion include/real/exact_number.hpp
Expand Up @@ -5,12 +5,20 @@
#include <string>
#include <algorithm>
#include <math.h>
<<<<<<< HEAD
#include <type_traits>
=======
#include<type_traits>
>>>>>>> finished exponential, made changes to division

namespace boost {
namespace real {
struct exact_number {
<<<<<<< HEAD
using exponent_t = int;
=======
typedef int exponent_t;
>>>>>>> finished exponential, made changes to division

std::vector<int> digits = {};
exponent_t exponent = 0;
Expand Down Expand Up @@ -216,7 +224,11 @@ namespace boost {

if (divisor == tmp) {
this->exponent = exponent_dif + 1;
<<<<<<< HEAD
this->positive = positive;
=======
(*this).positive = positive;
>>>>>>> finished exponential, made changes to division
return;
}

Expand All @@ -226,7 +238,11 @@ namespace boost {
return;
}

<<<<<<< HEAD
exact_number zero = exact_number();
=======
exact_number zero = exact_number();
>>>>>>> finished exponential, made changes to division

if (divisor == zero) {
throw(boost::real::divide_by_zero());
Expand All @@ -250,7 +266,11 @@ namespace boost {
(*this) = left + distance;
// N/D = Q -> QD -N = 0
residual = (*this) * divisor - numerator;
<<<<<<< HEAD
if (residual == zero) {
=======
if (residual == 0) {
>>>>>>> finished exponential, made changes to division
this->exponent += exponent_dif;
this->positive = positive;
return;
Expand Down Expand Up @@ -290,7 +310,11 @@ namespace boost {
// recalculate residual N/D = Q ---> QD - N = residual
residual = ((*this) * divisor) - numerator;
residual.normalize();
<<<<<<< HEAD
}
=======
} // end while
>>>>>>> finished exponential, made changes to division
// at this point, we may have exited early. we should be within +- min_boundary_p of the solution.

// truncate (*this)
Expand Down Expand Up @@ -405,9 +429,14 @@ namespace boost {
if(this->digits[index] != 0) {
--this->digits[index];
keep_carrying = false;
<<<<<<< HEAD
} else { // digits[index] == 0, we keep carrying
this->digits[index] = DIGIT_MAX;
}
=======
} else // digits[index] == 0, we keep carrying
this->digits[index] = DIGIT_MAX;
>>>>>>> finished exponential, made changes to division
--index;
}
// we should be ok at this point because the first number in digits should != 0
Expand All @@ -423,9 +452,21 @@ namespace boost {
/// ctor from vector of digits, integer exponent, and optional bool positive
exact_number(std::vector<int> vec, int exp, bool pos = true) : digits(vec), exponent(exp), positive(pos) {};

<<<<<<< HEAD
/// ctor from any integral type
template<typename I, typename std::enable_if_t<std::is_integral<I>::value>>
exact_number(I x) {
=======

// exact_number(std::initializer_list<int> list, int exp, bool pos= true) :
// digits(list), exponent(exp), positive(pos) {};

/// ctor from any integral type
template<typename I>
exact_number(I x) {
static_assert(std::numeric_limits<I>::is_integer);

>>>>>>> finished exponential, made changes to division
if (x < 0)
positive = false;
else
Expand All @@ -439,6 +480,7 @@ namespace boost {
}
}

<<<<<<< HEAD
explicit exact_number (const std::string& number) {
std::regex decimal("((\\+|-)?[[:digit:]]*)(\\.(([[:digit:]]+)?))?((e|E)(((\\+|-)?)[[:digit:]]+))?");
if (!std::regex_match (number, decimal))
Expand Down Expand Up @@ -491,6 +533,8 @@ namespace boost {
}
}

=======
>>>>>>> finished exponential, made changes to division
/**
* @brief *Copy constructor:* It constructs a new boost::real::exact_number that is a copy of the
* other boost::real::exact_number.
Expand Down Expand Up @@ -555,7 +599,11 @@ namespace boost {
if (this->digits == zero || this->digits.empty()) {
return !(other.digits == zero || other.positive || other.digits.empty());
} else {
<<<<<<< HEAD
if ((other.digits == zero) && this->positive)
=======
if ((other.digits == zero || other.digits.empty()) && this->positive)
>>>>>>> finished exponential, made changes to division
return true;
}

Expand All @@ -579,11 +627,19 @@ namespace boost {
}

bool operator>=(const exact_number& other) const {
<<<<<<< HEAD
return (!((*this) < other));
}

bool operator<=(const exact_number& other) const {
return (!((*this) > other));
=======
return (((*this) == other) || ((*this) > other));
}

bool operator<=(const exact_number& other) const {
return (((*this) == other) || ((*this) < other));
>>>>>>> finished exponential, made changes to division
}

/**
Expand Down Expand Up @@ -662,7 +718,6 @@ namespace boost {
*this = *this * other;
}


/**
* @brief Generates a string representation of the boost::real::exact_number.
*
Expand Down
7 changes: 7 additions & 0 deletions include/real/irrational_helpers.hpp
Expand Up @@ -4,7 +4,11 @@
#include <vector>
#include <algorithm>
#include <real/real.hpp>
<<<<<<< HEAD
#include <math.h>
=======
#include<math.h>
>>>>>>> finished exponential, made changes to division

namespace boost {
namespace real {
Expand Down Expand Up @@ -92,6 +96,7 @@ namespace boost {

return current_value.digits[n];
}
<<<<<<< HEAD

int pi_get_nth_digit(unsigned int n) {
if (n > 2000)
Expand All @@ -100,6 +105,8 @@ namespace boost {
exact_number pi("3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196442881097566593344612847564823378678316527120190914564856692346034861045432664821339360726024914127372458700660631558817488152092096282925409171536436789259036001133053054882046652138414695194151160943305727036575959195309218611738193261179310511854807446237996274956735188575272489122793818301194912983367336244065664308602139494639522473719070217986094370277053921717629317675238467481846766940513200056812714526356082778577134275778960917363717872146844090122495343014654958537105079227968925892354201995611212902196086403441815981362977477130996051870721134999999837297804995105973173281609631859502445945534690830264252230825334468503526193118817101000313783875288658753320838142061717766914730359825349042875546873115956286388235378759375195778185778053217122680661300192787661119590921642019893809525720106548586327886593615338182796823030195203530185296899577362259941389124972177528347913151557485724245415069595082953311686172785588907509838175463746493931925506040092770167113900984882401285836160356370766010471018194295559619894676783744944825537977472684710404753464620804668425906949129331367702898915210475216205696602405803815019351125338243003558764024749647326391419927260426992279678235478163600934172164121992458631503028618297455570674983850549458858692699569092721079750930295532116534498720275596023648066549911988183479775356636980742654252786255181841757467289097777279380008164706001614524919217321721477235014144197356854816136115735255213347574184946843852332390739414333454776241686251898356948556209921922218427255025425688767179049460165346680498862723279178608578438382796797668145410095388378636095068006422512520511739298489608412848862694560424196528502221066118630674427862203919494504712371378696095636437191728746776465757396241389086583264599581339047802759009");
return pi[n];
}
=======
>>>>>>> finished exponential, made changes to division
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions include/real/real.hpp
Expand Up @@ -494,7 +494,7 @@ namespace boost {
* @param other - the right side operand boost::real::real number.
*/
void operator*=(real other) {
this->_real_p =
this->_real_p =
std::make_shared<real_data>(real_operation(this->_real_p, other._real_p, OPERATION::MULTIPLICATION));
}

Expand Down Expand Up @@ -695,7 +695,7 @@ namespace boost {
}

inline boost::real::real operator "" _r(long double x) {
return boost::real::real (std::to_string(x));
return boost::real::real(std::to_string(x));
}

inline boost::real::real operator "" _r(unsigned long long x) {
Expand Down
2 changes: 0 additions & 2 deletions include/real/real_data.hpp
Expand Up @@ -226,11 +226,9 @@ namespace boost {
if (residual.abs() > zero) {
this->_approximation_interval.upper_bound.round_up();
}

// if both operands are numbers (not intervals), then we can skip doing the lower bound separately
if (ro.get_rhs_itr().get_interval().is_a_number() && ro.get_lhs_itr().get_interval().is_a_number()) {
_approximation_interval.lower_bound = quotient;

if (residual == zero) {
_approximation_interval.upper_bound = _approximation_interval.lower_bound;
} else {
Expand Down
13 changes: 0 additions & 13 deletions test.cpp

This file was deleted.

2 changes: 0 additions & 2 deletions test/real_iterator_division_test.cpp
Expand Up @@ -43,7 +43,6 @@ TEST_CASE("Operator / boost::real::const_precision_iterator") { // assumes max p
boost::real::real result = b/a;
// 12 / 144
// [10, 20] / [100, 200] = [10/200, 20/100] = [.05, .2]

auto result_it = result.get_real_itr().cbegin();

boost::real::interval expected_interval({});
Expand All @@ -56,7 +55,6 @@ TEST_CASE("Operator / boost::real::const_precision_iterator") { // assumes max p
CHECK(expected_interval == result_it.get_interval());

result_it = result.get_real_itr().cend();

// [12, 12] / [144, 144] = [.08333...3, .0833...4]
expected_interval.lower_bound.positive = true;
expected_interval.upper_bound.positive = true;
Expand Down

0 comments on commit 07c5363

Please sign in to comment.