20 template<
typename FloatType =
double>
41 template<
typename IntType =
int>
45 bool willBeReduce =
false,
46 bool willDenominatorBeVerified =
true
54 template<
typename FloatingType>
55 constexpr explicit ERational(
const FloatingType &nonRational);
68 template<
typename IntType>
81 FloatType numMultiplier,
short int numExponent,
82 FloatType denMultiplier,
short int denExponent
91 std::pair<FloatType, short int> numerator,
92 std::pair<FloatType, short int> denominator
104 [[maybe_unused]] [[nodiscard]]
constexpr inline std::pair<FloatType,short int>
getNumerator() const noexcept {
return m_numerator; }
117 constexpr inline void setNumerator(FloatType newMultiplier,
short int newExponent)
noexcept {
m_numerator = std::make_pair(newMultiplier, newExponent); }
122 constexpr inline void setDenominator(FloatType newMultiplier,
short int newExponent)
noexcept {
m_denominator = std::make_pair(newMultiplier, newExponent); }
199 [[maybe_unused]] [[nodiscard]]
inline bool isNegative() const noexcept {
251 template<typename AnotherFloatType =
double>
252 [[nodiscard]] inline constexpr AnotherFloatType
toRealNumber() const noexcept {
253 return static_cast<AnotherFloatType
>
262 [[nodiscard]]
inline std::string
toString() const noexcept {
277 template<
typename IntType =
int>
286 template<
typename IntType =
int>
318 ERational <FloatType> &firstERational,
319 ERational <FloatType> &secondERational
345 template<
typename FloatType>
351 template<
typename FloatType>
352 template<
typename IntType>
354 const IntType numerator,
355 const IntType denominator,
356 const bool willBeReduce,
357 const bool willDenominatorBeVerified
358 ) : m_numerator(transformOperandToPair(numerator)),
359 m_denominator(transformOperandToPair(denominator))
365 const int gcd = std::gcd(numerator, denominator);
366 assert(gcd != 0 &&
"GCD shouldn't be equal to 0");
373 template<
typename FloatType>
374 template<
typename AnotherFloatType>
376 verifyTemplateType();
384 template<
typename FloatType>
386 const FloatType numMultiplier,
387 const short int numExponent,
388 const FloatType denMultiplier,
389 const short int denExponent
391 m_numerator = std::make_pair(numMultiplier, numExponent);
392 m_denominator = std::make_pair(denMultiplier, denExponent);
396 template<
typename FloatType>
398 std::pair<FloatType, short int> numerator,
399 std::pair<FloatType, short int> denominator
401 m_numerator = numerator;
402 m_denominator = denominator;
409 template<
typename FloatType>
414 setAtSameDenominator(firstERational, secondERational);
415 setAtSameNumeratorExponent(firstERational, secondERational);
430 template<
typename FloatType>
435 setAtSameDenominator(firstERational, secondERational);
436 setAtSameNumeratorExponent(firstERational, secondERational);
451 template<
typename FloatType>
465 template<
typename FloatType>
479 template<
typename FloatType>
497 template<
typename FloatType>
499 return (*
this == anotherERational) ==
false;
506 template<
typename FloatType>
508 const int exponentDifference = std::abs(getNumExponent() - getDenExponent());
510 if (exponentDifference == 0 && getNumExponent() != 0) {
515 const int minExponent = std::min(getNumExponent(), getDenExponent());
516 if (exponentDifference != 0 && minExponent > 0) {
517 setNumExponent(getNumExponent() - minExponent);
518 setDenExponent(getDenExponent() - minExponent);
528 template<
typename FloatType>
529 template<
typename IntType>
531 setNumerator(transformOperandToPair(numerator));
532 setDenominator(transformOperandToPair(denominator));
535 template<
typename FloatType>
536 template<
typename IntType>
539 return std::make_pair(
540 operand * std::pow(10, -operandLength),
545 template<
typename FloatType>
547 if (getDenMultiplier() == 0. && checkIfDenominatorIsNull)
550 if (getDenMultiplier() < 0.) {
551 setNumMultiplier(-getNumMultiplier());
552 setDenMultiplier(-getDenMultiplier());
556 template<
typename FloatType>
566 "The two numerator exponents should be equal");
569 template<
typename FloatType>
571 ERational <FloatType> &firstERational,
572 ERational <FloatType> &secondERational
575 secondERational= secondERational *
ERational(this->getDenominator(), this->getDenominator());
577 "The two denominators should be equal");
591 template<
typename FloatType>
593 return stream << rational.
toString();
This class can be used to express big rationals.
constexpr ERational< FloatType > inverse() const
Get the inverse of an ERational.
constexpr bool operator==(const ERational< FloatType > &anotherERational) const
Comparison between 2 ERationals.
constexpr ERational(const Rational< IntType > &reference)
Copy constructor from a Rational.
constexpr std::pair< FloatType, short int > getDenominator() const noexcept
constexpr void verifyTemplateType() const
Verify if the template is correct. Throw an exception if the template is an integral.
constexpr ERational< FloatType > operator*(const ERational< FloatType > &anotherERational) const
Multiplication operation between 2 experimental rationals.
constexpr ERational()
Default constructor : instantiate an object without parameters.
constexpr short int getNumExponent() const noexcept
static constexpr std::pair< FloatType, short int > transformOperandToPair(IntType operand)
Transform a numerator/denominator into a pair.
constexpr void setNumExponent(short int newNumExponent) noexcept
constexpr void setDenMultiplier(FloatType newDenMultiplier) noexcept
constexpr ERational< FloatType > operator/(const ERational< FloatType > &anotherERational) const
Division operation between 2 experimental rationals.
bool isNegative() const noexcept
constexpr ERational(const FloatingType &nonRational)
Create an experimental rational from a floating number.
constexpr void setAtSameDenominator(ERational< FloatType > &firstERational, ERational< FloatType > &secondERational) const
Set firstERational and secondERational with the same denominator exponent.
bool isZero() const noexcept
std::string toString() const noexcept
Return ( numerator / denominator ) as a string.
constexpr FloatType getDenMultiplier() const noexcept
bool isInteger() const noexcept
constexpr ERational< FloatType > abs() const
Apply absolute value on the ERational.
constexpr ERational< FloatType > operator+(const ERational< FloatType > &anotherERational) const
Sum operation between 2 experimental rationals.
constexpr bool operator!=(const ERational< FloatType > &anotherERational) const
Different comparison between 2 erationals.
constexpr void setAtSameNumeratorExponent(ERational< FloatType > &firstERational, ERational< FloatType > &secondERational) const
Set firstERational and secondERational with the same numerator exponent.
constexpr ERational< FloatType > simplify() noexcept
Simplify the Rational with GCD.
constexpr void setNumerator(std::pair< FloatType, short int > numerator) noexcept
constexpr void verifyDenominator(bool checkIfDenominatorIsNull=true)
Verify if the denominator is null or negative.
~ERational()=default
Default Destructor.
constexpr void setNumerator(FloatType newMultiplier, short int newExponent) noexcept
constexpr void setDenominator(std::pair< FloatType, short int > denominator) noexcept
std::pair< FloatType, short int > m_denominator
constexpr void setNumMultiplier(FloatType newNumMultiplier) noexcept
constexpr void setDenExponent(short int newDenExponent) noexcept
constexpr void setDenominator(FloatType newMultiplier, short int newExponent) noexcept
constexpr AnotherFloatType toRealNumber() const noexcept
Get an approximation floating point number of the experimental ratio.
constexpr FloatType getNumMultiplier() const noexcept
constexpr short int getDenExponent() const noexcept
constexpr void transformToExperimental(IntType numerator, IntType denominator)
Set the parameters from a normal numerator and denominator.
constexpr ERational< FloatType > operator-(const ERational< FloatType > &anotherERational) const
Subtraction operation between 2 experimental rationals.
constexpr ERational(const ERational< FloatType > &reference)=default
Copy constructor.
constexpr std::pair< FloatType, short int > getNumerator() const noexcept
std::pair< FloatType, short int > m_numerator
An exception to handle divide by 0.
An exception to handle ERational multiplier in a non floating point type.
This class can be used to express rationals.
constexpr IntType getDenominator() const noexcept
static constexpr Rational< IntType > fromFloatingPoint(FloatingType floatingRatio, size_t iter=Constant::DEFAULT_ITERATIONS_FROM_FP)
A method who automatically set numerator and denominator to approach the float parameter.
constexpr IntType getNumerator() const noexcept
std::ostream & operator<<(std::ostream &stream, const ERational< FloatType > &rational)
<< operator override to allow std::cout