noisy_float/types.rs
1// Copyright 2016-2021 Matthew D. Michelotti
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Standard definitions of `NoisyFloat`.
16//!
17//! Definitions in this module all use `debug_assert!`
18//! to check for valid values, so there is no overhead
19//! when running in an optimized build.
20
21use core::marker::PhantomData;
22use crate::{
23 checkers::{FiniteChecker, NumChecker},
24 NoisyFloat,
25};
26
27/// A floating point number behaving like `f32` that does not allow NaN.
28///
29/// The "N" in the name stands for "Number", since all values of this type
30/// are "numbers", i.e. they are not "not-a-number".
31pub type N32 = NoisyFloat<f32, NumChecker>;
32
33/// A floating point number behaving like `f64` that does not allow NaN.
34///
35/// The "N" in the name stands for "Number", since all values of this type
36/// are "numbers", i.e. they are not "not-a-number".
37pub type N64 = NoisyFloat<f64, NumChecker>;
38
39/// A floating point number behaving like `f32` that does not allow NaN or +/- Infinity.
40///
41/// The "R" in the name stands for "Real", since in Mathematics, the Real
42/// numbers do not include NaN or +/- Infinity.
43pub type R32 = NoisyFloat<f32, FiniteChecker>;
44
45/// A floating point number behaving like `f64` that does not allow NaN or +/- Infinity.
46///
47/// The "R" in the name stands for "Real", since in Mathematics, the Real
48/// numbers do not include NaN or +/- Infinity.
49pub type R64 = NoisyFloat<f64, FiniteChecker>;
50
51/// Shorthand for `N32::new(value)`.
52#[inline]
53pub fn n32(value: f32) -> N32 {
54 N32::new(value)
55}
56
57/// Shorthand for `N64::new(value)`.
58#[inline]
59pub fn n64(value: f64) -> N64 {
60 N64::new(value)
61}
62
63/// Shorthand for `R32::new(value)`.
64#[inline]
65pub fn r32(value: f32) -> R32 {
66 R32::new(value)
67}
68
69/// Shorthand for `R64::new(value)`.
70#[inline]
71pub fn r64(value: f64) -> R64 {
72 R64::new(value)
73}
74
75macro_rules! const_fns {
76 ($type:ty, $raw:ty) => {
77 impl $type {
78 /// A const constructor that does not check whether `value` is valid.
79 ///
80 /// WARNING: This constructor does not panic even in debug mode.
81 /// As always, it is the user's responsibility to ensure `value` is valid.
82 /// Until Rust supports panics in const functions, this constructor
83 /// is necessary to create a NoisyFloat in a const setting.
84 pub const fn unchecked_new(value: $raw) -> Self {
85 Self {
86 value,
87 checker: PhantomData,
88 }
89 }
90
91 /// A const function that returns the underlying float value.
92 pub const fn const_raw(self) -> $raw {
93 self.value
94 }
95 }
96 };
97}
98
99const_fns!(N32, f32);
100const_fns!(N64, f64);
101const_fns!(R32, f32);
102const_fns!(R64, f64);