Summary
Add a Fraction type for exact rational number representation, avoiding floating-point errors.
Motivation
- Floating-point arithmetic introduces rounding errors.
- Useful for exact calculations in number theory, algebra, and symbolic math.
- Enables precise fraction representation (e.g.,
1/3, 5/8).
Proposed API
Fraction(int numerator, int denominator)
- Arithmetic:
+, -, *, /
- Reduction to simplest form
- Conversions:
double, decimal, string
- Comparisons:
==, <, >, <=, >=
Example Usage
var f1 = new Fraction(1, 3);
var f2 = new Fraction(2, 3);
var sum = f1 + f2; // 1/1
var product = f1 * f2; // 2/9
Summary
Add a
Fractiontype for exact rational number representation, avoiding floating-point errors.Motivation
1/3,5/8).Proposed API
Fraction(int numerator, int denominator)+,-,*,/double,decimal,string==,<,>,<=,>=Example Usage