MathParser\Extensions\Rational Class Reference

Implementation of rational number arithmetic. More...

Public Member Functions

 __construct ($p, $q, $normalize=true)
 Constuctor for Rational number class. More...
 
 signed ()
 convert rational number to string, adding a '+' if the number is positive More...
 
 is_nan ()
 test if the rational number is NAN More...
 
 __toString ()
 Convert Rational to string. More...
 

Static Public Member Functions

static add ($x, $y)
 add two rational numbers More...
 
static sub ($x, $y)
 subtract two rational numbers More...
 
static mul ($x, $y)
 multiply two rational numbers More...
 
static div ($x, $y)
 add two rational numbers More...
 
static parse ($value, $normalize=true)
 Convert $value to Rational. More...
 
static fromFloat ($float, $tolerance=1e-7)
 convert float to Rational More...
 

Public Attributes

 $p
 int $p numerator
 
 $q
 int $q denominator
 

Private Member Functions

 normalize ()
 Normalize, i.e. More...
 

Static Private Member Functions

static isInteger ($value)
 test whether a string represents an positive integer More...
 
static isSignedInteger ($value)
 test whether a string represents a signed integer More...
 

Detailed Description

Implementation of rational number arithmetic.

Example:

$a = new Rational(1, 4); // creates the rational number 1/4
$b = new Rational(2, 3); // creates the rational number -1+i
sum = Rational::add($a, $b) // computes the sum 1/4 + 2/3

Constructor & Destructor Documentation

MathParser\Extensions\Rational::__construct (   $p,
  $q,
  $normalize = true 
)

Constuctor for Rational number class.

$r = new Rational(2, 4) // creates 1/2 $r = new Rational(2, 4, false) // creates 2/4

Parameters
int$pnumerator
int$qdenominator
bool$normalize(default true) If true, store in normalized form, i.e. positive denominator and gcd($p, $q) = 1

Member Function Documentation

MathParser\Extensions\Rational::__toString ( )

Convert Rational to string.

Return values
string
static MathParser\Extensions\Rational::add (   $x,
  $y 
)
static

add two rational numbers

Rational::add($x, $y) computes and returns $x+$y

Parameters
mixed$x(Rational, or parsable to Rational)
mixed$y(Rational, or parsable to Rational)
Return values
Rational
static MathParser\Extensions\Rational::div (   $x,
  $y 
)
static

add two rational numbers

Rational::div($x, $y) computes and returns $x/$y

Parameters
mixed$x(Rational, or parsable to Rational)
mixed$y(Rational, or parsable to Rational)
Return values
Rational
static MathParser\Extensions\Rational::fromFloat (   $float,
  $tolerance = 1e-7 
)
static

convert float to Rational

Convert float to a continued fraction, with prescribed accuracy

Parameters
string | float$float
float$tolerance
Return values
Rational
MathParser\Extensions\Rational::is_nan ( )

test if the rational number is NAN

Return values
bool
static MathParser\Extensions\Rational::isInteger (   $value)
staticprivate

test whether a string represents an positive integer

Return values
bool
static MathParser\Extensions\Rational::isSignedInteger (   $value)
staticprivate

test whether a string represents a signed integer

Return values
bool
static MathParser\Extensions\Rational::mul (   $x,
  $y 
)
static

multiply two rational numbers

Rational::mul($x, $y) computes and returns $x*$y

Parameters
mixed$x(Rational, or parsable to Rational)
mixed$y(Rational, or parsable to Rational)
Return values
Rational
MathParser\Extensions\Rational::normalize ( )
private

Normalize, i.e.

make sure the denominator is positive and that the numerator and denominator have no common factors

static MathParser\Extensions\Rational::parse (   $value,
  $normalize = true 
)
static

Convert $value to Rational.

Parameters
$valuemixed
Exceptions
SyntaxErrorException
Return values
Rational
MathParser\Extensions\Rational::signed ( )

convert rational number to string, adding a '+' if the number is positive

Return values
string
static MathParser\Extensions\Rational::sub (   $x,
  $y 
)
static

subtract two rational numbers

Rational::sub($x, $y) computes and returns $x-$y

Parameters
mixed$x(Rational, or parsable to Rational)
mixed$y(Rational, or parsable to Rational)
Return values
Rational

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