Skip to main content

rug/rational/
mod.rs

1// Copyright © 2016–2026 Trevor Spiteri
2
3// This program is free software: you can redistribute it and/or modify it under
4// the terms of the GNU Lesser General Public License as published by the Free
5// Software Foundation, either version 3 of the License, or (at your option) any
6// later version.
7//
8// This program is distributed in the hope that it will be useful, but WITHOUT
9// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
10// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
11// details.
12//
13// You should have received a copy of the GNU Lesser General Public License and
14// a copy of the GNU General Public License along with this program. If not, see
15// <https://www.gnu.org/licenses/>.
16
17/*!
18Arbitrary-precision rational numbers.
19
20This module provides support for arbitrary-precision rational numbers of type
21[`Rational`][crate::Rational].
22*/
23
24mod arith;
25pub(crate) mod big;
26mod borrow;
27#[cfg(feature = "borsh")]
28mod borsh;
29mod casts;
30mod cmp;
31#[cfg(feature = "num-traits")]
32mod impl_num_traits;
33mod mini;
34#[cfg(feature = "serde")]
35mod serde;
36mod small;
37#[cfg(test)]
38mod tests;
39mod traits;
40
41pub use crate::rational::big::ParseRationalError;
42pub use crate::rational::borrow::BorrowRational;
43pub use crate::rational::mini::MiniRational;
44#[allow(deprecated)]
45pub use crate::rational::small::SmallRational;
46use core::error::Error;
47use core::fmt::{Display, Formatter, Result as FmtResult};
48
49/**
50An error which can be returned when a checked conversion from a floating-point
51number to a [`Rational`][crate::Rational] number fails.
52
53# Examples
54
55```rust
56use rug::rational::TryFromFloatError;
57use rug::Rational;
58// This is not finite and cannot be converted to Rational.
59let inf = 1.0f32 / 0.0;
60let error: TryFromFloatError = match Rational::try_from(inf) {
61    Ok(_) => unreachable!(),
62    Err(error) => error,
63};
64println!("Error: {}", error);
65```
66*/
67#[derive(Clone, Copy, Debug, Eq, PartialEq)]
68pub struct TryFromFloatError {
69    pub(crate) _unused: (),
70}
71
72impl TryFromFloatError {
73    fn desc(&self) -> &str {
74        "conversion of infinite or NaN value attempted"
75    }
76}
77
78impl Display for TryFromFloatError {
79    fn fmt(&self, f: &mut Formatter) -> FmtResult {
80        Display::fmt(self.desc(), f)
81    }
82}
83
84impl Error for TryFromFloatError {
85    #[allow(deprecated)]
86    fn description(&self) -> &str {
87        self.desc()
88    }
89}