#rational-numbers #rational #integer #i64 #ratio

yanked bens_naive_rational_number

A crate centered around a naive implementation of a Rational Number structure

0.1.5 Jan 22, 2021
0.1.4 Jan 22, 2021

#15 in #rational

Download history 61/week @ 2024-03-30 12/week @ 2024-04-06

73 downloads per month

MIT license

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());

No runtime deps