0.1.5 |
|
---|---|
0.1.4 |
|
#16 in #i64
24KB
369 lines
This crate is a simple, naive implementation of a Rational Number object. It provides constructors from integer and str inputs, but not floats. If you are already working with floats, there's no point in pretending to be all accurate now.
The +,-,/,* operators are overloaded as well as their assign counterparts: +=,-=,/=,*=
It is not truly a rational number crate in the sense that it allows the creation of fractions with zero denominators, and they will definitely break stuff. If this upsets you, contact OSHA.
Numerator and Denominator are stored as i64s, and negatives are reassigned to the numerator. (3/-2 becomes -3/2).
Examples
use bens_naive_rational_number::Rational;
// Calculating the price of an item
let orig_price = Rational::from_str("13.67").unwrap();
let tax = Rational::from_str("0.09").unwrap();
let discount = Rational::from_str("25/100").unwrap();
let one = Rational::from_integer(1).unwrap();
let final_price = orig_price * (one - discount) * (one + tax);
assert_eq!(final_price, Rational::from_str("447009/40000").unwrap());