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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568
// Copyright © 2018–2019 Trevor Spiteri // This library is free software: you can redistribute it and/or // modify it under the terms of either // // * the Apache License, Version 2.0 or // * the MIT License // // at your option. // // You should have recieved copies of the Apache License and the MIT // License along with the library. If not, see // <https://www.apache.org/licenses/LICENSE-2.0> and // <https://opensource.org/licenses/MIT>. /*! # Fixed-point numbers The [*fixed* crate] provides fixed-point numbers. Currently it uses the [*typenum* crate] for the fractional bit count; it is planned to move to [const generics] when they are implemented by the Rust compiler. The crate provides the following types: * [`FixedI8`] is a signed eight-bit fixed-point number, * [`FixedI16`] is a signed 16-bit fixed-point number, * [`FixedI32`] is a signed 32-bit fixed-point number, * [`FixedI64`] is a signed 64-bit fixed-point number, * [`FixedI128`] is a signed 128-bit fixed-point number, * [`FixedU8`] is an unsigned eight-bit fixed-point number, * [`FixedU16`] is an unsigned 16-bit fixed-point number, * [`FixedU32`] is an unsigned 32-bit fixed-point number, * [`FixedU64`] is an unsigned 64-bit fixed-point number, and * [`FixedU128`] is an unsigned 128-bit fixed-point number. All fixed-point numbers can have `Frac` fractional bits, where `Frac` can have any value from 0 up to and including the size of the number in bits. When `Frac` is 0, the fixed-point number behaves like an integer. When `Frac` is equal to the number of bits, the value of the fixed-point number lies in the range −0.5 ≤ *x* < 0.5 for signed fixed-point numbers, and in the range 0 ≤ *x* < 1 for unsigned fixed-point numbers. Various conversion methods are available: * All lossless infallible conversions between fixed-point numbers and numeric primitives are implemented. You can use [`From`] or [`Into`] for conversions that always work without losing any bits. * For lossy infallible conversions between fixed-point numbers and numeric primitives, where the source type may have more fractional bits than the destination type, the [`LossyFrom`] and [`LossyInto`] traits can be used. Excess fractional bits are truncated. * Checked conversions are provided between fixed-point numbers and numeric primitives using the [`FromFixed`] and [`ToFixed`] traits, or using the [`from_num`] and [`to_num`] methods and their checked versions. * Fixed-point numbers can be parsed from decimal strings using [`FromStr`], or from binary, octal or hexadecimal using the [`from_str_binary`], [`from_str_octal`] or [`from_str_hex`] methods. The result is rounded to the nearest, with ties rounded to even. * Fixed-point numbers can be converted to strings using [`Display`], [`Binary`], [`Octal`], [`LowerHex`] and [`UpperHex`]. The output is rounded to the nearest, with ties rounded to even. ## Quick examples ```rust // 20 integer bits, 12 fractional bits use fixed::types::I20F12; // 19/3 = 6 1/3 let six_and_third = I20F12::from_num(19) / 3; // four decimal digits for 12 binary digits assert_eq!(six_and_third.to_string(), "6.3333"); // find the ceil and convert to i32 assert_eq!(six_and_third.ceil().to_num::<i32>(), 7); // we can also compare directly to integers assert_eq!(six_and_third.ceil(), 7); ``` The type [`I20F12`] is a 32-bit fixed-point signed number with 20 integer bits and 12 fractional bits. It is an alias to <code>[FixedI32][`FixedI32`]<[U12][`U12`]></code>. The unsigned counterpart would be [`U20F12`]. Aliases are provided for all combinations of integer and fractional bits adding up to a total of eight, 16, 32, 64 or 128 bits. ```rust // −8 ≤ I4F4 < 8 with steps of 1/16 (~0.06) use fixed::types::I4F4; let a = I4F4::from_num(1); // multiplication and division by integers are possible let ans1 = a / 5 * 17; // 1 / 5 × 17 = 3 2/5 (3.4), but we get 3 3/16 (~3.2) assert_eq!(ans1, I4F4::from_bits((3 << 4) + 3)); assert_eq!(ans1.to_string(), "3.2"); // −8 ≤ I4F12 < 8 with steps of 1/4096 (~0.0002) use fixed::types::I4F12; let wider_a = I4F12::from(a); let wider_ans = wider_a / 5 * 17; let ans2 = I4F4::from_num(wider_ans); // now the answer is the much closer 3 6/16 (~3.4) assert_eq!(ans2, I4F4::from_bits((3 << 4) + 6)); assert_eq!(ans2.to_string(), "3.4"); ``` The second example shows some precision and conversion issues. The low precision of `a` means that `a / 5` is 3⁄16 instead of 1⁄5, leading to an inaccurate result `ans1` = 3 3⁄16 (~3.2). With a higher precision, we get `wider_a / 5` equal to 819⁄4096, leading to a more accurate intermediate result `wider_ans` = 3 1635⁄4096. When we convert back to four fractional bits, we get `ans2` = 3 6⁄16 (~3.4). Note that we can convert from [`I4F4`] to [`I4F12`] using [`From`], as the target type has the same number of integer bits and a larger number of fractional bits. Converting from [`I4F12`] to [`I4F4`] cannot use [`From`] as we have less fractional bits, so we use [`from_num`] instead. ## Using the *fixed* crate The *fixed* crate is available on [crates.io][*fixed* crate]. To use it in your crate, add it as a dependency inside [*Cargo.toml*]: ```toml [dependencies] fixed = "0.4.3" ``` The *fixed* crate requires rustc version 1.34.0 or later. ## Optional features The *fixed* crate has two optional feature: 1. `f16`, disabled by default. This provides conversion to/from [`f16`]. This features requires the [*half* crate]. 2. `serde`, disabled by default. This provides serialization support for the fixed-point types. This feature requires the [*serde* crate]. To enable features, you can add the dependency like this to [*Cargo.toml*]: ```toml [dependencies.fixed] version = "0.4.3" features = ["f16", "serde"] ``` ## License This crate is free software: you can redistribute it and/or modify it under the terms of either * the [Apache License, Version 2.0][LICENSE-APACHE] or * the [MIT License][LICENSE-MIT] at your option. ### Contribution Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache License, Version 2.0, shall be dual licensed as above, without any additional terms or conditions. [*Cargo.toml*]: https://doc.rust-lang.org/cargo/guide/dependencies.html [*fixed* crate]: https://crates.io/crates/fixed [*half* crate]: https://crates.io/crates/half [*serde* crate]: https://crates.io/crates/serde [*typenum* crate]: https://crates.io/crates/typenum [LICENSE-APACHE]: https://www.apache.org/licenses/LICENSE-2.0 [LICENSE-MIT]: https://opensource.org/licenses/MIT [`Binary`]: https://doc.rust-lang.org/nightly/std/fmt/trait.Binary.html [`Display`]: https://doc.rust-lang.org/nightly/std/fmt/trait.Display.html [`FixedI128`]: struct.FixedI128.html [`FixedI16`]: struct.FixedI16.html [`FixedI32`]: struct.FixedI32.html [`FixedI64`]: struct.FixedI64.html [`FixedI8`]: struct.FixedI8.html [`FixedU128`]: struct.FixedU128.html [`FixedU16`]: struct.FixedU16.html [`FixedU32`]: struct.FixedU32.html [`FixedU64`]: struct.FixedU64.html [`FixedU8`]: struct.FixedU8.html [`FromFixed`]: traits/trait.FromFixed.html [`FromStr`]: https://doc.rust-lang.org/nightly/std/str/trait.FromStr.html [`From`]: https://doc.rust-lang.org/nightly/std/convert/trait.From.html [`I20F12`]: types/type.I20F12.html [`I4F12`]: types/type.I4F12.html [`I4F4`]: types/type.I4F4.html [`Into`]: https://doc.rust-lang.org/nightly/std/convert/trait.Into.html [`LossyFrom`]: traits/trait.LossyFrom.html [`LossyInto`]: traits/trait.LossyInto.html [`LowerHex`]: https://doc.rust-lang.org/nightly/std/fmt/trait.LowerHex.html [`Octal`]: https://doc.rust-lang.org/nightly/std/fmt/trait.Octal.html [`ToFixed`]: traits/trait.ToFixed.html [`U12`]: types/extra/type.U12.html [`U20F12`]: types/type.U20F12.html [`UpperHex`]: https://doc.rust-lang.org/nightly/std/fmt/trait.UpperHex.html [`f16`]: https://docs.rs/half/^1/half/struct.f16.html [`from_num`]: struct.FixedI32.html#method.from_num [`from_str_binary`]: struct.FixedI32.html#method.from_str_binary [`from_str_hex`]: struct.FixedI32.html#method.from_str_hex [`from_str_octal`]: struct.FixedI32.html#method.from_str_octal [`to_num`]: struct.FixedI32.html#method.to_num [const generics]: https://github.com/rust-lang/rust/issues/44580 */ #![no_std] #![warn(missing_docs)] #![doc(html_root_url = "https://docs.rs/fixed/0.4.3")] #![doc(test(attr(deny(warnings))))] #![cfg_attr(feature = "fail-on-warnings", deny(warnings))] #![allow(clippy::type_repetition_in_bounds)] #[cfg(test)] extern crate std; #[macro_use] mod macros; mod arith; mod cmp; pub mod consts; mod convert; mod display; mod float_helper; pub mod frac; mod from_str; mod helpers; mod int_helper; pub mod sealed; #[cfg(feature = "serde")] mod serdeize; pub mod traits; pub mod types; mod wide_div; mod wrapping; use crate::{ arith::MulDivDir, from_str::FromStrRadix, traits::{FromFixed, ToFixed}, types::extra::{LeEqU128, LeEqU16, LeEqU32, LeEqU64, LeEqU8}, }; pub use crate::{from_str::ParseFixedError, wrapping::Wrapping}; use core::{ cmp::Ordering, hash::{Hash, Hasher}, marker::PhantomData, mem, }; /// A prelude for users of the *fixed* crate. /// /// This prelude is similar to the [standard library’s /// prelude][prelude] in that you’ll almost always want to import its /// entire contents, but unlike the standard library’s prelude you’ll /// have to do so manually: /// /// ``` /// # #[allow(unused_imports)] /// use fixed::prelude::*; /// ``` /// /// The prelude may grow over time as additional items see ubiquitous use. /// /// [prelude]: https://doc.rust-lang.org/nightly/std/prelude/index.html pub mod prelude { pub use crate::traits::{FromFixed, ToFixed}; } #[macro_use] mod macros_from_to; #[macro_use] mod macros_round; #[macro_use] mod macros_deprecated; #[macro_use] mod macros_no_frac; #[macro_use] mod macros_frac; macro_rules! fixed { ( $description:expr, $Fixed:ident($Inner:ty, $LeEqU:tt, $s_nbits:expr), $UInner:ty, $Signedness:tt ) => { fixed! { $description, $Fixed[stringify!($Fixed)]($Inner[stringify!($Inner)], $LeEqU, $s_nbits), $UInner, $Signedness } }; ( $description:expr, $Fixed:ident[$s_fixed:expr]($Inner:ty[$s_inner:expr], $LeEqU:tt, $s_nbits:expr), $UInner:ty, $Signedness:tt ) => { comment!( $description, " number with `Frac` fractional bits. Currently `Frac` is an [`Unsigned`] as provided by the [typenum crate]; it is planned to move to [const generics] when they are implemented by the Rust compiler. # Examples ```rust use fixed::{types::extra::U3, ", $s_fixed, "}; let eleven = ", $s_fixed, "::<U3>::from_num(11); assert_eq!(eleven, ", $s_fixed, "::<U3>::from_bits(11 << 3)); assert_eq!(eleven, 11); assert_eq!(eleven.to_string(), \"11\"); let two_point_75 = eleven / 4; assert_eq!(two_point_75, ", $s_fixed, "::<U3>::from_bits(11 << 1)); assert_eq!(two_point_75, 2.75); assert_eq!(two_point_75.to_string(), \"2.8\"); ``` [`Unsigned`]: https://docs.rs/typenum/^1.3/typenum/marker_traits/trait.Unsigned.html [const generics]: https://github.com/rust-lang/rust/issues/44580 [typenum crate]: https://crates.io/crates/typenum "; #[repr(transparent)] pub struct $Fixed<Frac> { bits: $Inner, phantom: PhantomData<Frac>, } ); impl<Frac> Clone for $Fixed<Frac> { #[inline] fn clone(&self) -> $Fixed<Frac> { $Fixed { bits: self.bits, phantom: PhantomData, } } } impl<Frac> Copy for $Fixed<Frac> {} impl<Frac> Default for $Fixed<Frac> { #[inline] fn default() -> Self { $Fixed { bits: Default::default(), phantom: PhantomData } } } impl<Frac> Hash for $Fixed<Frac> { #[inline] fn hash<H: Hasher>(&self, state: &mut H) { self.bits.hash(state); } } // inherent methods that do not require Frac bounds, some of which can thus be const fixed_no_frac! { $description, $Fixed[$s_fixed]($Inner[$s_inner], $s_nbits), $UInner, $Signedness } // inherent methods that require Frac bounds, and cannot be const fixed_frac! { $description, $Fixed[$s_fixed]($Inner[$s_inner], $LeEqU, $s_nbits), $UInner, $Signedness } }; } fixed! { "An eight-bit fixed-point unsigned", FixedU8(u8, LeEqU8, "8"), u8, Unsigned } fixed! { "A 16-bit fixed-point unsigned", FixedU16(u16, LeEqU16, "16"), u16, Unsigned } fixed! { "A 32-bit fixed-point unsigned", FixedU32(u32, LeEqU32, "32"), u32, Unsigned } fixed! { "A 64-bit fixed-point unsigned", FixedU64(u64, LeEqU64, "64"), u64, Unsigned } fixed! { "A 128-bit fixed-point unsigned", FixedU128(u128, LeEqU128, "128"), u128, Unsigned } fixed! { "An eight-bit fixed-point signed", FixedI8(i8, LeEqU8, "8"), u8, Signed } fixed! { "A 16-bit fixed-point signed", FixedI16(i16, LeEqU16, "16"), u16, Signed } fixed! { "A 32-bit fixed-point signed", FixedI32(i32, LeEqU32, "32"), u32, Signed } fixed! { "A 64-bit fixed-point signed", FixedI64(i64, LeEqU64, "64"), u64, Signed } fixed! { "A 128-bit fixed-point signed", FixedI128(i128, LeEqU128, "128"), u128, Signed } #[cfg(test)] mod tests { use crate::types::{I0F32, I16F16, U0F32, U16F16}; #[cfg_attr(feature = "cargo-clippy", allow(clippy::cognitive_complexity))] #[test] fn rounding() { // -0.5 let f = I0F32::from_bits(-1 << 31); assert_eq!(f.to_num::<i32>(), -1); assert_eq!(f.overflowing_ceil(), (I0F32::from_num(0), false)); assert_eq!(f.overflowing_floor(), (I0F32::from_num(0), true)); assert_eq!(f.overflowing_round(), (I0F32::from_num(0), true)); // -0.5 + Δ let f = I0F32::from_bits((-1 << 31) + 1); assert_eq!(f.to_num::<i32>(), -1); assert_eq!(f.overflowing_ceil(), (I0F32::from_num(0), false)); assert_eq!(f.overflowing_floor(), (I0F32::from_num(0), true)); assert_eq!(f.overflowing_round(), (I0F32::from_num(0), false)); // 0.5 - Δ let f = I0F32::from_bits((1 << 30) - 1 + (1 << 30)); assert_eq!(f.to_num::<i32>(), 0); assert_eq!(f.overflowing_ceil(), (I0F32::from_num(0), true)); assert_eq!(f.overflowing_floor(), (I0F32::from_num(0), false)); assert_eq!(f.overflowing_round(), (I0F32::from_num(0), false)); // 0.5 - Δ let f = U0F32::from_bits((1 << 31) - 1); assert_eq!(f.to_num::<i32>(), 0); assert_eq!(f.overflowing_ceil(), (U0F32::from_num(0), true)); assert_eq!(f.overflowing_floor(), (U0F32::from_num(0), false)); assert_eq!(f.overflowing_round(), (U0F32::from_num(0), false)); // 0.5 let f = U0F32::from_bits(1 << 31); assert_eq!(f.to_num::<i32>(), 0); assert_eq!(f.overflowing_ceil(), (U0F32::from_num(0), true)); assert_eq!(f.overflowing_floor(), (U0F32::from_num(0), false)); assert_eq!(f.overflowing_round(), (U0F32::from_num(0), true)); // 0.5 + Δ let f = U0F32::from_bits((1 << 31) + 1); assert_eq!(f.to_num::<i32>(), 0); assert_eq!(f.overflowing_ceil(), (U0F32::from_num(0), true)); assert_eq!(f.overflowing_floor(), (U0F32::from_num(0), false)); assert_eq!(f.overflowing_round(), (U0F32::from_num(0), true)); // -3.5 - Δ let f = I16F16::from_bits(((-7) << 15) - 1); assert_eq!(f.to_num::<i32>(), -4); assert_eq!(f.overflowing_ceil(), (I16F16::from_num(-3), false)); assert_eq!(f.overflowing_floor(), (I16F16::from_num(-4), false)); assert_eq!(f.overflowing_round(), (I16F16::from_num(-4), false)); // -3.5 let f = I16F16::from_bits((-7) << 15); assert_eq!(f.to_num::<i32>(), -4); assert_eq!(f.overflowing_ceil(), (I16F16::from_num(-3), false)); assert_eq!(f.overflowing_floor(), (I16F16::from_num(-4), false)); assert_eq!(f.overflowing_round(), (I16F16::from_num(-4), false)); // -3.5 + Δ let f = I16F16::from_bits(((-7) << 15) + 1); assert_eq!(f.to_num::<i32>(), -4); assert_eq!(f.overflowing_ceil(), (I16F16::from_num(-3), false)); assert_eq!(f.overflowing_floor(), (I16F16::from_num(-4), false)); assert_eq!(f.overflowing_round(), (I16F16::from_num(-3), false)); // -0.5 - Δ let f = I16F16::from_bits(((-1) << 15) - 1); assert_eq!(f.to_num::<i32>(), -1); assert_eq!(f.overflowing_ceil(), (I16F16::from_num(0), false)); assert_eq!(f.overflowing_floor(), (I16F16::from_num(-1), false)); assert_eq!(f.overflowing_round(), (I16F16::from_num(-1), false)); // -0.5 let f = I16F16::from_bits((-1) << 15); assert_eq!(f.to_num::<i32>(), -1); assert_eq!(f.overflowing_ceil(), (I16F16::from_num(0), false)); assert_eq!(f.overflowing_floor(), (I16F16::from_num(-1), false)); assert_eq!(f.overflowing_round(), (I16F16::from_num(-1), false)); // -0.5 + Δ let f = I16F16::from_bits(((-1) << 15) + 1); assert_eq!(f.to_num::<i32>(), -1); assert_eq!(f.overflowing_ceil(), (I16F16::from_num(0), false)); assert_eq!(f.overflowing_floor(), (I16F16::from_num(-1), false)); assert_eq!(f.overflowing_round(), (I16F16::from_num(0), false)); // 0.5 - Δ let f = I16F16::from_bits((1 << 15) - 1); assert_eq!(f.to_num::<i32>(), 0); assert_eq!(f.overflowing_ceil(), (I16F16::from_num(1), false)); assert_eq!(f.overflowing_floor(), (I16F16::from_num(0), false)); assert_eq!(f.overflowing_round(), (I16F16::from_num(0), false)); // 0.5 let f = I16F16::from_bits(1 << 15); assert_eq!(f.to_num::<i32>(), 0); assert_eq!(f.overflowing_ceil(), (I16F16::from_num(1), false)); assert_eq!(f.overflowing_floor(), (I16F16::from_num(0), false)); assert_eq!(f.overflowing_round(), (I16F16::from_num(1), false)); // 0.5 + Δ let f = I16F16::from_bits((1 << 15) + 1); assert_eq!(f.to_num::<i32>(), 0); assert_eq!(f.overflowing_ceil(), (I16F16::from_num(1), false)); assert_eq!(f.overflowing_floor(), (I16F16::from_num(0), false)); assert_eq!(f.overflowing_round(), (I16F16::from_num(1), false)); // 3.5 - Δ let f = I16F16::from_bits((7 << 15) - 1); assert_eq!(f.to_num::<i32>(), 3); assert_eq!(f.overflowing_ceil(), (I16F16::from_num(4), false)); assert_eq!(f.overflowing_floor(), (I16F16::from_num(3), false)); assert_eq!(f.overflowing_round(), (I16F16::from_num(3), false)); // 3.5 let f = I16F16::from_bits(7 << 15); assert_eq!(f.to_num::<i32>(), 3); assert_eq!(f.overflowing_ceil(), (I16F16::from_num(4), false)); assert_eq!(f.overflowing_floor(), (I16F16::from_num(3), false)); assert_eq!(f.overflowing_round(), (I16F16::from_num(4), false)); // 3.5 + Δ let f = I16F16::from_bits((7 << 15) + 1); assert_eq!(f.to_num::<i32>(), 3); assert_eq!(f.overflowing_ceil(), (I16F16::from_num(4), false)); assert_eq!(f.overflowing_floor(), (I16F16::from_num(3), false)); assert_eq!(f.overflowing_round(), (I16F16::from_num(4), false)); // 0.5 - Δ let f = U16F16::from_bits((1 << 15) - 1); assert_eq!(f.to_num::<i32>(), 0); assert_eq!(f.overflowing_ceil(), (U16F16::from_num(1), false)); assert_eq!(f.overflowing_floor(), (U16F16::from_num(0), false)); assert_eq!(f.overflowing_round(), (U16F16::from_num(0), false)); // 0.5 let f = U16F16::from_bits(1 << 15); assert_eq!(f.to_num::<i32>(), 0); assert_eq!(f.overflowing_ceil(), (U16F16::from_num(1), false)); assert_eq!(f.overflowing_floor(), (U16F16::from_num(0), false)); assert_eq!(f.overflowing_round(), (U16F16::from_num(1), false)); // 0.5 + Δ let f = U16F16::from_bits((1 << 15) + 1); assert_eq!(f.to_num::<i32>(), 0); assert_eq!(f.overflowing_ceil(), (U16F16::from_num(1), false)); assert_eq!(f.overflowing_floor(), (U16F16::from_num(0), false)); assert_eq!(f.overflowing_round(), (U16F16::from_num(1), false)); // 3.5 - Δ let f = U16F16::from_bits((7 << 15) - 1); assert_eq!(f.to_num::<i32>(), 3); assert_eq!(f.overflowing_ceil(), (U16F16::from_num(4), false)); assert_eq!(f.overflowing_floor(), (U16F16::from_num(3), false)); assert_eq!(f.overflowing_round(), (U16F16::from_num(3), false)); // 3.5 let f = U16F16::from_bits(7 << 15); assert_eq!(f.to_num::<i32>(), 3); assert_eq!(f.overflowing_ceil(), (U16F16::from_num(4), false)); assert_eq!(f.overflowing_floor(), (U16F16::from_num(3), false)); assert_eq!(f.overflowing_round(), (U16F16::from_num(4), false)); // 3.5 + Δ let f = U16F16::from_bits((7 << 15) + 1); assert_eq!(f.to_num::<i32>(), 3); assert_eq!(f.overflowing_ceil(), (U16F16::from_num(4), false)); assert_eq!(f.overflowing_floor(), (U16F16::from_num(3), false)); assert_eq!(f.overflowing_round(), (U16F16::from_num(4), false)); } }