NTLib - Number Theory Library 0.9
Loading...
Searching...
No Matches
rational.cpp File Reference
module rational

Primary module interface unit for module rational. More...

#include <cassert>
#include <compare>
#include <ostream>
#include <string>
#include <type_traits>
import base;
Include dependency graph for rational.cpp:

Classes

class  ntlib::rational< T >
 Represents a rational number. More...
class  ntlib::algebra_traits< rational< T > >
 Specialization of ntlib::algebra_traits for rational. More...

Functions

template<Integer T>
std::string ntlib::to_string (const rational< T > &r)
 Creates a string "a/b" where a and b are coprime.
template<Integer T>
std::ostream & ntlib::operator<< (std::ostream &os, const rational< T > &r)
 Stream insertion operator.
template<Integer T>
bool ntlib::operator== (const rational< T > &lhs, const rational< T > &rhs)
 Equality operator.
template<Integer T>
std::strong_ordering ntlib::operator<=> (const rational< T > &lhs, const rational< T > &rhs)
 Three-way comparison operator.
template<Integer T>
rational< T > ntlib::operator- (const rational< T > &r)
 Unary minus operator.

Detailed Description

Primary module interface unit for module rational.

Function Documentation

◆ operator-()

template<Integer T>
rational< T > ntlib::operator- ( const rational< T > & r)
nodiscardexport

Unary minus operator.

Template Parameters
TAn integer-like type.
Parameters
rThe rational.
Returns
The rational with the same absolute value but opposite sign.

◆ operator<<()

template<Integer T>
std::ostream & ntlib::operator<< ( std::ostream & os,
const rational< T > & r )
export

Stream insertion operator.

Template Parameters
TAn integer-like type.
Parameters
osThe output stream.
rThe rational.
Returns
The output stream.

◆ operator<=>()

template<Integer T>
std::strong_ordering ntlib::operator<=> ( const rational< T > & lhs,
const rational< T > & rhs )
nodiscardexport

Three-way comparison operator.

Template Parameters
TAn integer-like type.
Parameters
lhsThe left hand side to compare.
rhsThe right hand side to compare.
Return values
<0If lhs < rhs.
0If lhs == rhs.
>0If lhs > rhs.

◆ operator==()

template<Integer T>
bool ntlib::operator== ( const rational< T > & lhs,
const rational< T > & rhs )
nodiscardexport

Equality operator.

Parameters
lhsThe left hand side to compare.
rhsThe right hand side to compare.
Returns
True, if and only if lhs is equal to rhs.

◆ to_string()

template<Integer T>
std::string ntlib::to_string ( const rational< T > & r)
nodiscardexport

Creates a string "a/b" where a and b are coprime.

Template Parameters
TAn integer-like type.
Parameters
rA rational.
Returns
A string representation.