Boost.Real  1.0.0
Boost.Real numerical data type for real numbers representation using range arithmetic.
boost::real::real Class Reference

boost::real::real is a C++ class that represent real numbers as abstract entities that can be dynamically approximated as much as needed (until a set maximum precision) to be able to operate with them. Numbers can be added, subtracted, multiplied and compared by lower than and equality. More...

#include <real.hpp>

Classes

class  const_precision_iterator
 is a forward iterator that iterates a boost::real::real number approximation intervals. The iterator calculates the initial interval with the initial precision and then it increase the precision in each iteration (++) and recalculate the interval. More...
 

Public Member Functions

 real ()=default
 Default constructor: Constructr a boost::real::real with undefined representation and behaviour. More...
 
 real (const real &other)
 Copy constructor: Creates a copy of the boost::real::real number other, if the number is an operation, then, the constructor recursively creates new copies of the other operands. More...
 
 real (const std::string &number)
 String constructor: Creates a boost::real::real instance by parsing the string. The string must have a valid number, in other case, the constructor will throw an boost::real::invalid_string_number exception. More...
 
 real (std::initializer_list< int > digits)
 Initializer list constructor: Creates a boost::real::real_explicit instance that represents an integer number where all the digits parameter numbers are form the integer part in the same order. The number is set as positive. More...
 
 real (std::initializer_list< int > digits, bool positive)
 Signed initializer list constructor: Creates a boost::real::real instance that represents the number where the positive parameter is used to set the number sign and the elements of the digits parameter list are the number digits in the same order. More...
 
 real (std::initializer_list< int > digits, int exponent)
 Initializer list constructor with exponent: Creates a boost::real::real instance that represents the number where the exponent is used to set the number integer part and the elements of the digits list are the digits the number in the same order. The number is set as positive. More...
 
 real (std::initializer_list< int > digits, int exponent, bool positive)
 Initializer list constructor with exponent and sign: Creates a boost::real::real instance that represents the number where the exponent is used to set the number integer part and the elements of the digit list are the digits the number in the same order. This constructor uses the sign to determine if the number is positive or negative. More...
 
 real (int(*get_nth_digit)(unsigned int), int exponent)
 Lambda function constructor with exponent: Creates a boost::real::real instance that represents the number where the exponent is used to set the number integer part and the lambda function digits is used to know the number digits, this function returns the n-th number digit. More...
 
 real (int(*get_nth_digit)(unsigned int), int exponent, bool positive)
 Lambda function constructor with exponent and sign: Creates a boost::real::real instance that represents the number where the exponent is used to set the number integer part and the lambda function digits is used to know the number digit, this function returns the n-th number digit. This constructor uses the sign to determine if the number is positive or negative. More...
 
 ~real ()
 Default destructor: If the number is an operator, the destructor destroys its operands.
 
unsigned int max_precision () const
 Returns te maximum allowed precision, if that precision is reached and an operator need more precision, a precision_exception should be thrown. More...
 
void set_maximum_precision (unsigned int maximum_precision)
 Set a new maximum precision for the instance. More...
 
const_precision_iterator cbegin () const
 Construct a new boost::real::real::con_precision_iterator that iterates the number approximation intervals from in increasing order according to the approximation precision. More...
 
const_precision_iterator cend () const
 Construct a new boost::real::real::con_precision_iterator that iterates the number approximation intervals from in increasing order according to the approximation precision. More...
 
int operator[] (unsigned int n) const
 If the number is an explicit or algorithm number it returns the n-th digit of the represented number. If the number is an operation it throws an invalid_representation_exception. More...
 
realoperator+= (const real &other)
 Convert the number from its current representation to an operation number representation where the operands are copies of the other and this numbers (before the conversion). The new number represent the addition operation between *this and other. More...
 
real operator+ (const real &other) const
 Creates a new boost::real::real representing an operation number where the operands are copies of the other and this numbers. The number represent the addition operation between *this and other. More...
 
realoperator-= (const real &other)
 Convert the number from its current representation to an operation number representation where the operands are copies of the other and this numbers (before the conversion). The new number representation represent the subtraction operation between *this and other. More...
 
real operator- (const real &other) const
 Creates a new boost::real::real representing an operation number where the operands are copies of the other and this numbers. The number represent the subtraction operation between *this and other. More...
 
realoperator*= (const real &other)
 Convert the number from its current representation to an operation number representation where the operands are copies of the other and this numbers (before the conversion). The new number represent the multiplication between *this and other. More...
 
real operator* (const real &other) const
 Creates a new boost::real::real representing an operation number where the operands are copies of the other and this numbers. The number represent the multiplication operation between *this and other. More...
 
realoperator= (const real &other)
 It assign a new copy of the other boost::real::real number in the *this boost::real::real number. More...
 
realoperator= (const std::string &number)
 It constructs and boost::real::real number from the std::string and assign it in the *this boost::real::real number. More...
 
bool operator< (const real &other) const
 Compares the *this boost::real::real number against the other boost::real::real number to determine if the number represented by *this is lower than the number represented by other. If the maximum precision is reached and the operator was not yet able to determine the value of the result, a precision_exception is thrown. More...
 
bool operator> (const real &other) const
 Compares the *this boost::real::real number against the other boost::real::real number to determine if the number represented by *this is greater than the number represented by other. If the maximum precision is reached and the operator was not yet able to determine the value of the result, a precision_exception is thrown. More...
 
bool operator== (const real &other) const
 Compares the *this boost::real::real number against the other boost::real::real number to determine if the number represented by *this is the same than the number represented by other. If the maximum precision is reached and the operator was not yet able to determine the value of the result, a precision_exception is thrown. More...
 

Static Public Attributes

static unsigned int maximum_precision
 Determines the maximum precision to use.
 

Detailed Description

boost::real::real is a C++ class that represent real numbers as abstract entities that can be dynamically approximated as much as needed (until a set maximum precision) to be able to operate with them. Numbers can be added, subtracted, multiplied and compared by lower than and equality.

Author
Laouen Mayal Louan Belloli

A boost::real::real number is represented by the operations from which the number is created, the entire operation is represented as a binary tree where the leaves are literal numbers and the internal nodes are the operations. Also, boost::real::real allow to represent irrational numbers by taking as parameter a function pointer or lambda function that given un unsigned integer "n", the function returns the n-th digit of the irrational number.

A number can be one of the following three kind:

  1. Explicit number: A number is a vector of digits sorted as in the number natural representation. To determine where the integer part ends and the fractional part starts, an integer is used as the exponent of a floating point number and determines where the integer part start and the fractional ends. Also a boolean is used to set the number as positive (True) or negative (False)
  2. Algorithmic number: This representation is equal to the Explicit number but instead of using a vector of digits, a lambda function must be provided. The lambda function takes an unsigned integer "n" as parameter and returns the n-th digit of the number.
  3. A number is a composition of two numbers related by an operator (+, -, *), the number creates pointers to the operands and each time the number is used, the operation is evaluated to return the result.

Two boost::real::real numbers can be compared by the lower operator "<" and by the equal operator "==" but for those cases where the class is not able to decide the value of the result before reaching the maximum precision, a precision_exception is thrown.

Constructor & Destructor Documentation

boost::real::real::real ( )
default

Default constructor: Constructr a boost::real::real with undefined representation and behaviour.

Note
This constructor exist to allow working with other libraries as std::map or std::tuple
boost::real::real::real ( const real other)
inline

Copy constructor: Creates a copy of the boost::real::real number other, if the number is an operation, then, the constructor recursively creates new copies of the other operands.

Parameters
other- the boost::real::real instance to copy.
boost::real::real::real ( const std::string &  number)
inline

String constructor: Creates a boost::real::real instance by parsing the string. The string must have a valid number, in other case, the constructor will throw an boost::real::invalid_string_number exception.

Parameters
number- a valid string representing a number.
Exceptions
boost::real::invalid_string_numberexception
boost::real::real::real ( std::initializer_list< int >  digits)
inline

Initializer list constructor: Creates a boost::real::real_explicit instance that represents an integer number where all the digits parameter numbers are form the integer part in the same order. The number is set as positive.

Parameters
digits- a initializer_list<int> that represents the number digits.
boost::real::real::real ( std::initializer_list< int >  digits,
bool  positive 
)
inline

Signed initializer list constructor: Creates a boost::real::real instance that represents the number where the positive parameter is used to set the number sign and the elements of the digits parameter list are the number digits in the same order.

Parameters
digits- an initializer_list<int> that represent the number digits.
positive- a bool that represent the number sign. If positive is set to true, the number is positive, otherwise is negative.
boost::real::real::real ( std::initializer_list< int >  digits,
int  exponent 
)
inline

Initializer list constructor with exponent: Creates a boost::real::real instance that represents the number where the exponent is used to set the number integer part and the elements of the digits list are the digits the number in the same order. The number is set as positive.

Parameters
digits- an initializer_list<int> that represent the number digits.
exponent- an integer representing the number exponent.
boost::real::real::real ( std::initializer_list< int >  digits,
int  exponent,
bool  positive 
)
inline

Initializer list constructor with exponent and sign: Creates a boost::real::real instance that represents the number where the exponent is used to set the number integer part and the elements of the digit list are the digits the number in the same order. This constructor uses the sign to determine if the number is positive or negative.

Parameters
digits- an initializer_list<int> that represent the number digits.
exponent- an integer representing the number exponent.
positive- a bool that represent the number sign. If positive is set to true, the number is positive, otherwise is negative.
boost::real::real::real ( int(*)(unsigned int)  get_nth_digit,
int  exponent 
)
inline

Lambda function constructor with exponent: Creates a boost::real::real instance that represents the number where the exponent is used to set the number integer part and the lambda function digits is used to know the number digits, this function returns the n-th number digit.

Parameters
get_nth_digit- a function pointer or lambda function that given an unsigned int "n" as parameter, it returns the number n-th digit.
exponent- an integer representing the number exponent.
boost::real::real::real ( int(*)(unsigned int)  get_nth_digit,
int  exponent,
bool  positive 
)
inline

Lambda function constructor with exponent and sign: Creates a boost::real::real instance that represents the number where the exponent is used to set the number integer part and the lambda function digits is used to know the number digit, this function returns the n-th number digit. This constructor uses the sign to determine if the number is positive or negative.

Parameters
get_nth_digit- a function pointer or lambda function that given an unsigned int "n" as parameter, it returns the number n-th digit.
exponent- an integer representing the number exponent.
positive- a bool that represent the number sign. If positive is set to true, the number is positive, otherwise is negative.

Member Function Documentation

const_precision_iterator boost::real::real::cbegin ( ) const
inline

Construct a new boost::real::real::con_precision_iterator that iterates the number approximation intervals from in increasing order according to the approximation precision.

The iterator starts pointing the interval with the minimum precision.

Returns
a boost::real::real::const_precision_iterator of the number.
const_precision_iterator boost::real::real::cend ( ) const
inline

Construct a new boost::real::real::con_precision_iterator that iterates the number approximation intervals from in increasing order according to the approximation precision.

The iterator starts pointing the interval with the maximum allowed precision.

Returns
a boost::real::real::const_precision_iterator of the number.
unsigned int boost::real::real::max_precision ( ) const
inline

Returns te maximum allowed precision, if that precision is reached and an operator need more precision, a precision_exception should be thrown.

Returns
and integer with the maximum allowed precision.
real boost::real::real::operator* ( const real other) const
inline

Creates a new boost::real::real representing an operation number where the operands are copies of the other and this numbers. The number represent the multiplication operation between *this and other.

Parameters
other- the right side operand boost::real::real number.
Returns
A reference to the new boost::real::real number representation.
real& boost::real::real::operator*= ( const real other)
inline

Convert the number from its current representation to an operation number representation where the operands are copies of the other and this numbers (before the conversion). The new number represent the multiplication between *this and other.

Parameters
other- the right side operand boost::real::real number.
Returns
A reference to the new boost::real::real number representation.
real boost::real::real::operator+ ( const real other) const
inline

Creates a new boost::real::real representing an operation number where the operands are copies of the other and this numbers. The number represent the addition operation between *this and other.

Parameters
other- the right side operand boost::real::real number.
Returns
A reference to the new boost::real::real number representation.
real& boost::real::real::operator+= ( const real other)
inline

Convert the number from its current representation to an operation number representation where the operands are copies of the other and this numbers (before the conversion). The new number represent the addition operation between *this and other.

Parameters
other- the right side operand boost::real::real number.
Returns
A reference to the new boost::real::real number representation.
real boost::real::real::operator- ( const real other) const
inline

Creates a new boost::real::real representing an operation number where the operands are copies of the other and this numbers. The number represent the subtraction operation between *this and other.

Parameters
other- the right side operand boost::real::real number.
Returns
A reference to the new boost::real::real number representation.
real& boost::real::real::operator-= ( const real other)
inline

Convert the number from its current representation to an operation number representation where the operands are copies of the other and this numbers (before the conversion). The new number representation represent the subtraction operation between *this and other.

Parameters
other- the right side operand boost::real::real number.
Returns
A reference to the new boost::real::real number representation.
bool boost::real::real::operator< ( const real other) const
inline

Compares the *this boost::real::real number against the other boost::real::real number to determine if the number represented by *this is lower than the number represented by other. If the maximum precision is reached and the operator was not yet able to determine the value of the result, a precision_exception is thrown.

Parameters
other- a boost::real::real number to compare against.
Returns
a bool that is true if *this < other and false in other cases.
Exceptions
boost::real::precision_exception
real& boost::real::real::operator= ( const real other)
inline

It assign a new copy of the other boost::real::real number in the *this boost::real::real number.

Parameters
other- the boost::real::real number to copy.
Returns
a reference of *this with the new represented number.
real& boost::real::real::operator= ( const std::string &  number)
inline

It constructs and boost::real::real number from the std::string and assign it in the *this boost::real::real number.

Parameters
number- a valid string representing a number.
Returns
a reference of *this with the new represented number.
bool boost::real::real::operator== ( const real other) const
inline

Compares the *this boost::real::real number against the other boost::real::real number to determine if the number represented by *this is the same than the number represented by other. If the maximum precision is reached and the operator was not yet able to determine the value of the result, a precision_exception is thrown.

Parameters
other- a boost::real::real number to compare against.
Returns
a bool that is true if *this < other and false in other cases.
Exceptions
boost::real::precision_exception
bool boost::real::real::operator> ( const real other) const
inline

Compares the *this boost::real::real number against the other boost::real::real number to determine if the number represented by *this is greater than the number represented by other. If the maximum precision is reached and the operator was not yet able to determine the value of the result, a precision_exception is thrown.

Parameters
other- a boost::real::real number to compare against.
Returns
a bool that is true if *this > other and false in other cases.
Exceptions
boost::real::precision_exception
int boost::real::real::operator[] ( unsigned int  n) const
inline

If the number is an explicit or algorithm number it returns the n-th digit of the represented number. If the number is an operation it throws an invalid_representation_exception.

Parameters
n- an unsigned int number indicating the index of the requested digit.
Returns
an integer with the value of the number n-th digit.
Exceptions
boost::real::invalid_representation_exception
void boost::real::real::set_maximum_precision ( unsigned int  maximum_precision)
inline

Set a new maximum precision for the instance.

Note
Setting zero as the maximum precision causes the instance to use the default static unsigned int boost::real::real::maximum_precision.
Parameters
maximum_precision- an unsigned int to set as the new precision.

The documentation for this class was generated from the following file: