`floor`/`ceil`/`trunc` return an int; reject non-finite or out-of-i128 values like CPython int conversion.
(x: f64)
| 210 | |
| 211 | // `floor`/`ceil`/`trunc` return an int; reject non-finite or out-of-i128 values like CPython int conversion. |
| 212 | fn to_int(x: f64) -> Result<i128> { |
| 213 | if !x.is_finite() { |
| 214 | return Err(Error::Value(String::from("cannot convert float NaN or infinity to integer"))); |
| 215 | } |
| 216 | // Values at or beyond 2^127 saturate on cast; reject so the result is never silently clamped. |
| 217 | if x.abs() >= 170141183460469231731687303715884105728.0 { |
| 218 | return Err(Error::Value(String::from("int too large to convert"))); |
| 219 | } |
| 220 | Ok(x as i128) |
| 221 | } |
| 222 | |
| 223 | #[plugin_fn] |
| 224 | fn floor(x: f64) -> Result<i128> { to_int(libm::floor(x)) } |