The class presented in this article implements a rational value type with basic mathematical functionality.
Using the code
This class has no public constructors.
Implicit casting to/from
Int64 (and therefore other integer types) and decimal (and therefore
double) is supported.
PIEBALD.Types.Rational a = 1 ;
PIEBALD.Types.Rational b = 2.3 ;
PIEBALD.Types.Rational c = 4.5M ;
Conversion from decimal can be controlled by the
static property. Currently, the
DecimalConversionMethod enumeration defines two values:
Decimal simply uses a power of ten as the denominator.
BestGuess tries (with some success) to determine what numbers can be divided to produce the value.
PIEBALD.Types.Rational d = 1M / 3M ;
PIEBALD.Types.Rational e = 1M / 3M ;
Strings containing expressions can be assigned with the
PIEBALD.Types.Rational f =
PIEBALD.Types.Rational.ParseInfix ( "1/2" ) ;
PIEBALD.Types.Rational g =
PIEBALD.Types.Rational.ParseInfix ( "(1/2) / (3/4)" ) ;
PIEBALD.Types.Rational h =
PIEBALD.Types.Rational.ParseRpn ( "1 2 /" ) ;
PIEBALD.Lib.LibRpn class for information on how infix is transformed to RPN.
The mathematical operators (+, -, *, /, %, ^ (exponentiation)) are supported. There are other static and instance properties as well.
- First posted - 2006/01/16.