num-rational-parse
Extension for num_rational providing flexible string parsing for Ratio<T> types.
It allows you to parse:
- Fractions:
"3/4" - Decimals:
"1.25" - Scientific notation:
"1.2e-3","1E5"
Usage
Add this to your Cargo.toml:
[]
= "0.4"
= "0.1"
Then import the RationalParse trait:
use Ratio;
use RationalParse;
The standard from_str implementation in num_rational only supports the numerator/denominator format. num-rational-parse extends this to support decimals and scientific notation.
Note on Precision
Unlike parsing to a floating-point number (like f64) and then converting to a fraction, num-rational-parse parses decimal strings directly into their exact rational representation. This avoids precision loss or rounding errors commonly associated with floating-point math.
License
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Credits
The parsing approach is inspired by Python®'s fractions module.
Trademark Notice
"Python" is a registered trademark of the Python Software Foundation.