1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
Copyright 2024 Owain Davies
SPDX-License-Identifier: Apache-2.0 OR MIT
*/
use crateAssign;
use crateArbi;
/// Construct an [`Arbi`] integer from a floating-point value.
///
/// # Panic
/// Panics when attempting to convert a `NaN` or infinity.
///
/// # Note
/// In Rust, when [casting a primitive float to a primitive integer type](
/// https://doc.rust-lang.org/reference/expressions/operator-expr.html#type-cast-expressions),
/// `NaN`s are converted to `0` and values with large magnitudes and infinities
/// are saturated to the maximum and minimum values of the integer type.
///
/// In contrast, this function panics when `NaN` or infinity is encountered.
///
/// Otherwise, the semantics of this function are consistent with Rust's
/// built-in behavior for casting floats to integers.
///
/// # Examples
/// ```
/// use arbi::Arbi;
///
/// let a = Arbi::from(12345.6789);
/// assert_eq!(a, 12345);
/// assert_eq!(a, 12345.6789 as i32)
/// ```
///
/// Attempting to convert infinity panics.
///
/// ```should_panic
/// use arbi::Arbi;
///
/// let a = Arbi::from(f64::INFINITY);
/// ```
///
/// Attempting to convert `NaN` panics.
///
/// ```should_panic
/// use arbi::Arbi;
///
/// let b = Arbi::from(f64::NAN);
/// ```
///
/// ## Complexity
/// \\( O(1) \\)