]>
Commit | Line | Data |
---|---|---|
ff7c6d11 XL |
1 | //! This module provides constants which are specific to the implementation |
2 | //! of the `f32` floating point data type. | |
3 | //! | |
ff7c6d11 | 4 | //! *[See also the `f32` primitive type](../../std/primitive.f32.html).* |
94b46f34 XL |
5 | //! |
6 | //! Mathematically significant numbers are provided in the `consts` sub-module. | |
74b04a01 XL |
7 | //! |
8 | //! Although using these constants won’t cause compilation warnings, | |
9 | //! new code should use the associated constants directly on the primitive type. | |
1a4d82fc | 10 | |
85aaf69f | 11 | #![stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc | 12 | |
60c5eb7d | 13 | use crate::convert::FloatToInt; |
dc9dc135 XL |
14 | #[cfg(not(test))] |
15 | use crate::intrinsics; | |
48663c56 XL |
16 | use crate::mem; |
17 | use crate::num::FpCategory; | |
1a4d82fc | 18 | |
5bcae85e | 19 | /// The radix or base of the internal representation of `f32`. |
74b04a01 | 20 | /// Use [`f32::RADIX`](../../std/primitive.f32.html#associatedconstant.RADIX) instead. |
c34b1796 | 21 | #[stable(feature = "rust1", since = "1.0.0")] |
74b04a01 | 22 | pub const RADIX: u32 = f32::RADIX; |
1a4d82fc | 23 | |
5bcae85e | 24 | /// Number of significant digits in base 2. |
74b04a01 | 25 | /// Use [`f32::MANTISSA_DIGITS`](../../std/primitive.f32.html#associatedconstant.MANTISSA_DIGITS) instead. |
c34b1796 | 26 | #[stable(feature = "rust1", since = "1.0.0")] |
74b04a01 | 27 | pub const MANTISSA_DIGITS: u32 = f32::MANTISSA_DIGITS; |
5bcae85e | 28 | /// Approximate number of significant digits in base 10. |
74b04a01 | 29 | /// Use [`f32::DIGITS`](../../std/primitive.f32.html#associatedconstant.DIGITS) instead. |
c34b1796 | 30 | #[stable(feature = "rust1", since = "1.0.0")] |
74b04a01 | 31 | pub const DIGITS: u32 = f32::DIGITS; |
1a4d82fc | 32 | |
94b46f34 | 33 | /// [Machine epsilon] value for `f32`. |
74b04a01 | 34 | /// Use [`f32::EPSILON`](../../std/primitive.f32.html#associatedconstant.EPSILON) instead. |
94b46f34 | 35 | /// |
60c5eb7d | 36 | /// This is the difference between `1.0` and the next larger representable number. |
94b46f34 XL |
37 | /// |
38 | /// [Machine epsilon]: https://en.wikipedia.org/wiki/Machine_epsilon | |
85aaf69f | 39 | #[stable(feature = "rust1", since = "1.0.0")] |
74b04a01 | 40 | pub const EPSILON: f32 = f32::EPSILON; |
1a4d82fc | 41 | |
5bcae85e | 42 | /// Smallest finite `f32` value. |
74b04a01 | 43 | /// Use [`f32::MIN`](../../std/primitive.f32.html#associatedconstant.MIN) instead. |
85aaf69f | 44 | #[stable(feature = "rust1", since = "1.0.0")] |
74b04a01 | 45 | pub const MIN: f32 = f32::MIN; |
5bcae85e | 46 | /// Smallest positive normal `f32` value. |
74b04a01 | 47 | /// Use [`f32::MIN_POSITIVE`](../../std/primitive.f32.html#associatedconstant.MIN_POSITIVE) instead. |
85aaf69f | 48 | #[stable(feature = "rust1", since = "1.0.0")] |
74b04a01 | 49 | pub const MIN_POSITIVE: f32 = f32::MIN_POSITIVE; |
5bcae85e | 50 | /// Largest finite `f32` value. |
74b04a01 | 51 | /// Use [`f32::MAX`](../../std/primitive.f32.html#associatedconstant.MAX) instead. |
85aaf69f | 52 | #[stable(feature = "rust1", since = "1.0.0")] |
74b04a01 | 53 | pub const MAX: f32 = f32::MAX; |
85aaf69f | 54 | |
5bcae85e | 55 | /// One greater than the minimum possible normal power of 2 exponent. |
74b04a01 | 56 | /// Use [`f32::MIN_EXP`](../../std/primitive.f32.html#associatedconstant.MIN_EXP) instead. |
c34b1796 | 57 | #[stable(feature = "rust1", since = "1.0.0")] |
74b04a01 | 58 | pub const MIN_EXP: i32 = f32::MIN_EXP; |
5bcae85e | 59 | /// Maximum possible power of 2 exponent. |
74b04a01 | 60 | /// Use [`f32::MAX_EXP`](../../std/primitive.f32.html#associatedconstant.MAX_EXP) instead. |
c34b1796 | 61 | #[stable(feature = "rust1", since = "1.0.0")] |
74b04a01 | 62 | pub const MAX_EXP: i32 = f32::MAX_EXP; |
1a4d82fc | 63 | |
5bcae85e | 64 | /// Minimum possible normal power of 10 exponent. |
74b04a01 | 65 | /// Use [`f32::MIN_10_EXP`](../../std/primitive.f32.html#associatedconstant.MIN_10_EXP) instead. |
c34b1796 | 66 | #[stable(feature = "rust1", since = "1.0.0")] |
74b04a01 | 67 | pub const MIN_10_EXP: i32 = f32::MIN_10_EXP; |
5bcae85e | 68 | /// Maximum possible power of 10 exponent. |
74b04a01 | 69 | /// Use [`f32::MAX_10_EXP`](../../std/primitive.f32.html#associatedconstant.MAX_10_EXP) instead. |
c34b1796 | 70 | #[stable(feature = "rust1", since = "1.0.0")] |
74b04a01 | 71 | pub const MAX_10_EXP: i32 = f32::MAX_10_EXP; |
1a4d82fc | 72 | |
5bcae85e | 73 | /// Not a Number (NaN). |
74b04a01 | 74 | /// Use [`f32::NAN`](../../std/primitive.f32.html#associatedconstant.NAN) instead. |
85aaf69f | 75 | #[stable(feature = "rust1", since = "1.0.0")] |
74b04a01 | 76 | pub const NAN: f32 = f32::NAN; |
5bcae85e | 77 | /// Infinity (∞). |
74b04a01 | 78 | /// Use [`f32::INFINITY`](../../std/primitive.f32.html#associatedconstant.INFINITY) instead. |
85aaf69f | 79 | #[stable(feature = "rust1", since = "1.0.0")] |
74b04a01 | 80 | pub const INFINITY: f32 = f32::INFINITY; |
dfeec247 | 81 | /// Negative infinity (−∞). |
74b04a01 | 82 | /// Use [`f32::NEG_INFINITY`](../../std/primitive.f32.html#associatedconstant.NEG_INFINITY) instead. |
85aaf69f | 83 | #[stable(feature = "rust1", since = "1.0.0")] |
74b04a01 | 84 | pub const NEG_INFINITY: f32 = f32::NEG_INFINITY; |
1a4d82fc | 85 | |
b039eaaf | 86 | /// Basic mathematical constants. |
c34b1796 | 87 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
88 | pub mod consts { |
89 | // FIXME: replace with mathematical constants from cmath. | |
90 | ||
5bcae85e | 91 | /// Archimedes' constant (π) |
c34b1796 | 92 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
93 | pub const PI: f32 = 3.14159265358979323846264338327950288_f32; |
94 | ||
60c5eb7d XL |
95 | /// The full circle constant (τ) |
96 | /// | |
97 | /// Equal to 2π. | |
98 | #[unstable(feature = "tau_constant", issue = "66770")] | |
99 | pub const TAU: f32 = 6.28318530717958647692528676655900577_f32; | |
100 | ||
5bcae85e | 101 | /// π/2 |
c34b1796 | 102 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
103 | pub const FRAC_PI_2: f32 = 1.57079632679489661923132169163975144_f32; |
104 | ||
5bcae85e | 105 | /// π/3 |
c34b1796 | 106 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
107 | pub const FRAC_PI_3: f32 = 1.04719755119659774615421446109316763_f32; |
108 | ||
5bcae85e | 109 | /// π/4 |
c34b1796 | 110 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
111 | pub const FRAC_PI_4: f32 = 0.785398163397448309615660845819875721_f32; |
112 | ||
5bcae85e | 113 | /// π/6 |
c34b1796 | 114 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
115 | pub const FRAC_PI_6: f32 = 0.52359877559829887307710723054658381_f32; |
116 | ||
5bcae85e | 117 | /// π/8 |
c34b1796 | 118 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
119 | pub const FRAC_PI_8: f32 = 0.39269908169872415480783042290993786_f32; |
120 | ||
5bcae85e | 121 | /// 1/π |
c34b1796 | 122 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
123 | pub const FRAC_1_PI: f32 = 0.318309886183790671537767526745028724_f32; |
124 | ||
5bcae85e | 125 | /// 2/π |
c34b1796 | 126 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
127 | pub const FRAC_2_PI: f32 = 0.636619772367581343075535053490057448_f32; |
128 | ||
5bcae85e | 129 | /// 2/sqrt(π) |
c34b1796 AL |
130 | #[stable(feature = "rust1", since = "1.0.0")] |
131 | pub const FRAC_2_SQRT_PI: f32 = 1.12837916709551257389615890312154517_f32; | |
132 | ||
5bcae85e | 133 | /// sqrt(2) |
c34b1796 AL |
134 | #[stable(feature = "rust1", since = "1.0.0")] |
135 | pub const SQRT_2: f32 = 1.41421356237309504880168872420969808_f32; | |
136 | ||
5bcae85e | 137 | /// 1/sqrt(2) |
c34b1796 AL |
138 | #[stable(feature = "rust1", since = "1.0.0")] |
139 | pub const FRAC_1_SQRT_2: f32 = 0.707106781186547524400844362104849039_f32; | |
140 | ||
5bcae85e | 141 | /// Euler's number (e) |
c34b1796 | 142 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
143 | pub const E: f32 = 2.71828182845904523536028747135266250_f32; |
144 | ||
5bcae85e | 145 | /// log<sub>2</sub>(e) |
c34b1796 | 146 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
147 | pub const LOG2_E: f32 = 1.44269504088896340735992468100189214_f32; |
148 | ||
94b46f34 | 149 | /// log<sub>2</sub>(10) |
74b04a01 | 150 | #[stable(feature = "extra_log_consts", since = "1.43.0")] |
94b46f34 XL |
151 | pub const LOG2_10: f32 = 3.32192809488736234787031942948939018_f32; |
152 | ||
5bcae85e | 153 | /// log<sub>10</sub>(e) |
c34b1796 | 154 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
155 | pub const LOG10_E: f32 = 0.434294481903251827651128918916605082_f32; |
156 | ||
94b46f34 | 157 | /// log<sub>10</sub>(2) |
74b04a01 | 158 | #[stable(feature = "extra_log_consts", since = "1.43.0")] |
94b46f34 XL |
159 | pub const LOG10_2: f32 = 0.301029995663981195213738894724493027_f32; |
160 | ||
5bcae85e | 161 | /// ln(2) |
c34b1796 | 162 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
163 | pub const LN_2: f32 = 0.693147180559945309417232121458176568_f32; |
164 | ||
5bcae85e | 165 | /// ln(10) |
c34b1796 | 166 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
167 | pub const LN_10: f32 = 2.30258509299404568401799145468436421_f32; |
168 | } | |
169 | ||
94b46f34 XL |
170 | #[lang = "f32"] |
171 | #[cfg(not(test))] | |
172 | impl f32 { | |
74b04a01 XL |
173 | /// The radix or base of the internal representation of `f32`. |
174 | #[stable(feature = "assoc_int_consts", since = "1.43.0")] | |
175 | pub const RADIX: u32 = 2; | |
176 | ||
177 | /// Number of significant digits in base 2. | |
178 | #[stable(feature = "assoc_int_consts", since = "1.43.0")] | |
179 | pub const MANTISSA_DIGITS: u32 = 24; | |
180 | ||
181 | /// Approximate number of significant digits in base 10. | |
182 | #[stable(feature = "assoc_int_consts", since = "1.43.0")] | |
183 | pub const DIGITS: u32 = 6; | |
184 | ||
185 | /// [Machine epsilon] value for `f32`. | |
186 | /// | |
187 | /// This is the difference between `1.0` and the next larger representable number. | |
188 | /// | |
189 | /// [Machine epsilon]: https://en.wikipedia.org/wiki/Machine_epsilon | |
190 | #[stable(feature = "assoc_int_consts", since = "1.43.0")] | |
191 | pub const EPSILON: f32 = 1.19209290e-07_f32; | |
192 | ||
193 | /// Smallest finite `f32` value. | |
194 | #[stable(feature = "assoc_int_consts", since = "1.43.0")] | |
195 | pub const MIN: f32 = -3.40282347e+38_f32; | |
196 | /// Smallest positive normal `f32` value. | |
197 | #[stable(feature = "assoc_int_consts", since = "1.43.0")] | |
198 | pub const MIN_POSITIVE: f32 = 1.17549435e-38_f32; | |
199 | /// Largest finite `f32` value. | |
200 | #[stable(feature = "assoc_int_consts", since = "1.43.0")] | |
201 | pub const MAX: f32 = 3.40282347e+38_f32; | |
202 | ||
203 | /// One greater than the minimum possible normal power of 2 exponent. | |
204 | #[stable(feature = "assoc_int_consts", since = "1.43.0")] | |
205 | pub const MIN_EXP: i32 = -125; | |
206 | /// Maximum possible power of 2 exponent. | |
207 | #[stable(feature = "assoc_int_consts", since = "1.43.0")] | |
208 | pub const MAX_EXP: i32 = 128; | |
209 | ||
210 | /// Minimum possible normal power of 10 exponent. | |
211 | #[stable(feature = "assoc_int_consts", since = "1.43.0")] | |
212 | pub const MIN_10_EXP: i32 = -37; | |
213 | /// Maximum possible power of 10 exponent. | |
214 | #[stable(feature = "assoc_int_consts", since = "1.43.0")] | |
215 | pub const MAX_10_EXP: i32 = 38; | |
216 | ||
217 | /// Not a Number (NaN). | |
218 | #[stable(feature = "assoc_int_consts", since = "1.43.0")] | |
219 | pub const NAN: f32 = 0.0_f32 / 0.0_f32; | |
220 | /// Infinity (∞). | |
221 | #[stable(feature = "assoc_int_consts", since = "1.43.0")] | |
222 | pub const INFINITY: f32 = 1.0_f32 / 0.0_f32; | |
223 | /// Negative infinity (-∞). | |
224 | #[stable(feature = "assoc_int_consts", since = "1.43.0")] | |
225 | pub const NEG_INFINITY: f32 = -1.0_f32 / 0.0_f32; | |
226 | ||
9fa01778 | 227 | /// Returns `true` if this value is `NaN`. |
83c7162d XL |
228 | /// |
229 | /// ``` | |
83c7162d XL |
230 | /// let nan = f32::NAN; |
231 | /// let f = 7.0_f32; | |
232 | /// | |
233 | /// assert!(nan.is_nan()); | |
234 | /// assert!(!f.is_nan()); | |
235 | /// ``` | |
236 | #[stable(feature = "rust1", since = "1.0.0")] | |
237 | #[inline] | |
94b46f34 XL |
238 | pub fn is_nan(self) -> bool { |
239 | self != self | |
240 | } | |
83c7162d | 241 | |
0731742a XL |
242 | // FIXME(#50145): `abs` is publicly unavailable in libcore due to |
243 | // concerns about portability, so this implementation is for | |
244 | // private use internally. | |
245 | #[inline] | |
246 | fn abs_private(self) -> f32 { | |
247 | f32::from_bits(self.to_bits() & 0x7fff_ffff) | |
248 | } | |
249 | ||
9fa01778 XL |
250 | /// Returns `true` if this value is positive infinity or negative infinity, and |
251 | /// `false` otherwise. | |
83c7162d XL |
252 | /// |
253 | /// ``` | |
83c7162d XL |
254 | /// let f = 7.0f32; |
255 | /// let inf = f32::INFINITY; | |
256 | /// let neg_inf = f32::NEG_INFINITY; | |
257 | /// let nan = f32::NAN; | |
258 | /// | |
259 | /// assert!(!f.is_infinite()); | |
260 | /// assert!(!nan.is_infinite()); | |
261 | /// | |
262 | /// assert!(inf.is_infinite()); | |
263 | /// assert!(neg_inf.is_infinite()); | |
264 | /// ``` | |
265 | #[stable(feature = "rust1", since = "1.0.0")] | |
266 | #[inline] | |
94b46f34 | 267 | pub fn is_infinite(self) -> bool { |
0731742a | 268 | self.abs_private() == INFINITY |
94b46f34 | 269 | } |
83c7162d XL |
270 | |
271 | /// Returns `true` if this number is neither infinite nor `NaN`. | |
272 | /// | |
273 | /// ``` | |
83c7162d XL |
274 | /// let f = 7.0f32; |
275 | /// let inf = f32::INFINITY; | |
276 | /// let neg_inf = f32::NEG_INFINITY; | |
277 | /// let nan = f32::NAN; | |
278 | /// | |
279 | /// assert!(f.is_finite()); | |
280 | /// | |
281 | /// assert!(!nan.is_finite()); | |
282 | /// assert!(!inf.is_finite()); | |
283 | /// assert!(!neg_inf.is_finite()); | |
284 | /// ``` | |
285 | #[stable(feature = "rust1", since = "1.0.0")] | |
286 | #[inline] | |
94b46f34 | 287 | pub fn is_finite(self) -> bool { |
0731742a XL |
288 | // There's no need to handle NaN separately: if self is NaN, |
289 | // the comparison is not true, exactly as desired. | |
290 | self.abs_private() < INFINITY | |
94b46f34 | 291 | } |
83c7162d XL |
292 | |
293 | /// Returns `true` if the number is neither zero, infinite, | |
dfeec247 | 294 | /// [subnormal], or `NaN`. |
83c7162d XL |
295 | /// |
296 | /// ``` | |
83c7162d XL |
297 | /// let min = f32::MIN_POSITIVE; // 1.17549435e-38f32 |
298 | /// let max = f32::MAX; | |
299 | /// let lower_than_min = 1.0e-40_f32; | |
300 | /// let zero = 0.0_f32; | |
301 | /// | |
302 | /// assert!(min.is_normal()); | |
303 | /// assert!(max.is_normal()); | |
304 | /// | |
305 | /// assert!(!zero.is_normal()); | |
306 | /// assert!(!f32::NAN.is_normal()); | |
307 | /// assert!(!f32::INFINITY.is_normal()); | |
308 | /// // Values between `0` and `min` are Subnormal. | |
309 | /// assert!(!lower_than_min.is_normal()); | |
310 | /// ``` | |
311 | /// [subnormal]: https://en.wikipedia.org/wiki/Denormal_number | |
312 | #[stable(feature = "rust1", since = "1.0.0")] | |
313 | #[inline] | |
94b46f34 XL |
314 | pub fn is_normal(self) -> bool { |
315 | self.classify() == FpCategory::Normal | |
316 | } | |
83c7162d XL |
317 | |
318 | /// Returns the floating point category of the number. If only one property | |
319 | /// is going to be tested, it is generally faster to use the specific | |
320 | /// predicate instead. | |
321 | /// | |
322 | /// ``` | |
323 | /// use std::num::FpCategory; | |
83c7162d XL |
324 | /// |
325 | /// let num = 12.4_f32; | |
326 | /// let inf = f32::INFINITY; | |
327 | /// | |
328 | /// assert_eq!(num.classify(), FpCategory::Normal); | |
329 | /// assert_eq!(inf.classify(), FpCategory::Infinite); | |
330 | /// ``` | |
331 | #[stable(feature = "rust1", since = "1.0.0")] | |
94b46f34 XL |
332 | pub fn classify(self) -> FpCategory { |
333 | const EXP_MASK: u32 = 0x7f800000; | |
334 | const MAN_MASK: u32 = 0x007fffff; | |
335 | ||
336 | let bits = self.to_bits(); | |
337 | match (bits & MAN_MASK, bits & EXP_MASK) { | |
338 | (0, 0) => FpCategory::Zero, | |
339 | (_, 0) => FpCategory::Subnormal, | |
340 | (0, EXP_MASK) => FpCategory::Infinite, | |
341 | (_, EXP_MASK) => FpCategory::Nan, | |
342 | _ => FpCategory::Normal, | |
343 | } | |
344 | } | |
83c7162d | 345 | |
9fa01778 | 346 | /// Returns `true` if `self` has a positive sign, including `+0.0`, `NaN`s with |
83c7162d XL |
347 | /// positive sign bit and positive infinity. |
348 | /// | |
349 | /// ``` | |
350 | /// let f = 7.0_f32; | |
351 | /// let g = -7.0_f32; | |
352 | /// | |
353 | /// assert!(f.is_sign_positive()); | |
354 | /// assert!(!g.is_sign_positive()); | |
355 | /// ``` | |
356 | #[stable(feature = "rust1", since = "1.0.0")] | |
357 | #[inline] | |
94b46f34 XL |
358 | pub fn is_sign_positive(self) -> bool { |
359 | !self.is_sign_negative() | |
360 | } | |
83c7162d | 361 | |
9fa01778 | 362 | /// Returns `true` if `self` has a negative sign, including `-0.0`, `NaN`s with |
83c7162d XL |
363 | /// negative sign bit and negative infinity. |
364 | /// | |
365 | /// ``` | |
366 | /// let f = 7.0f32; | |
367 | /// let g = -7.0f32; | |
368 | /// | |
369 | /// assert!(!f.is_sign_negative()); | |
370 | /// assert!(g.is_sign_negative()); | |
371 | /// ``` | |
372 | #[stable(feature = "rust1", since = "1.0.0")] | |
373 | #[inline] | |
94b46f34 XL |
374 | pub fn is_sign_negative(self) -> bool { |
375 | // IEEE754 says: isSignMinus(x) is true if and only if x has negative sign. isSignMinus | |
376 | // applies to zeros and NaNs as well. | |
377 | self.to_bits() & 0x8000_0000 != 0 | |
378 | } | |
83c7162d XL |
379 | |
380 | /// Takes the reciprocal (inverse) of a number, `1/x`. | |
381 | /// | |
382 | /// ``` | |
83c7162d | 383 | /// let x = 2.0_f32; |
e1599b0c | 384 | /// let abs_difference = (x.recip() - (1.0 / x)).abs(); |
83c7162d XL |
385 | /// |
386 | /// assert!(abs_difference <= f32::EPSILON); | |
387 | /// ``` | |
388 | #[stable(feature = "rust1", since = "1.0.0")] | |
389 | #[inline] | |
94b46f34 XL |
390 | pub fn recip(self) -> f32 { |
391 | 1.0 / self | |
392 | } | |
83c7162d XL |
393 | |
394 | /// Converts radians to degrees. | |
395 | /// | |
396 | /// ``` | |
ba9703b0 | 397 | /// let angle = std::f32::consts::PI; |
83c7162d XL |
398 | /// |
399 | /// let abs_difference = (angle.to_degrees() - 180.0).abs(); | |
400 | /// | |
401 | /// assert!(abs_difference <= f32::EPSILON); | |
402 | /// ``` | |
dfeec247 | 403 | #[stable(feature = "f32_deg_rad_conversions", since = "1.7.0")] |
83c7162d | 404 | #[inline] |
94b46f34 XL |
405 | pub fn to_degrees(self) -> f32 { |
406 | // Use a constant for better precision. | |
407 | const PIS_IN_180: f32 = 57.2957795130823208767981548141051703_f32; | |
408 | self * PIS_IN_180 | |
409 | } | |
83c7162d XL |
410 | |
411 | /// Converts degrees to radians. | |
412 | /// | |
413 | /// ``` | |
83c7162d XL |
414 | /// let angle = 180.0f32; |
415 | /// | |
ba9703b0 | 416 | /// let abs_difference = (angle.to_radians() - std::f32::consts::PI).abs(); |
83c7162d XL |
417 | /// |
418 | /// assert!(abs_difference <= f32::EPSILON); | |
419 | /// ``` | |
dfeec247 | 420 | #[stable(feature = "f32_deg_rad_conversions", since = "1.7.0")] |
83c7162d | 421 | #[inline] |
94b46f34 XL |
422 | pub fn to_radians(self) -> f32 { |
423 | let value: f32 = consts::PI; | |
424 | self * (value / 180.0f32) | |
425 | } | |
83c7162d XL |
426 | |
427 | /// Returns the maximum of the two numbers. | |
428 | /// | |
429 | /// ``` | |
430 | /// let x = 1.0f32; | |
431 | /// let y = 2.0f32; | |
432 | /// | |
433 | /// assert_eq!(x.max(y), y); | |
434 | /// ``` | |
435 | /// | |
436 | /// If one of the arguments is NaN, then the other argument is returned. | |
437 | #[stable(feature = "rust1", since = "1.0.0")] | |
438 | #[inline] | |
439 | pub fn max(self, other: f32) -> f32 { | |
dc9dc135 | 440 | intrinsics::maxnumf32(self, other) |
83c7162d XL |
441 | } |
442 | ||
443 | /// Returns the minimum of the two numbers. | |
444 | /// | |
445 | /// ``` | |
446 | /// let x = 1.0f32; | |
447 | /// let y = 2.0f32; | |
448 | /// | |
449 | /// assert_eq!(x.min(y), x); | |
450 | /// ``` | |
451 | /// | |
452 | /// If one of the arguments is NaN, then the other argument is returned. | |
453 | #[stable(feature = "rust1", since = "1.0.0")] | |
454 | #[inline] | |
455 | pub fn min(self, other: f32) -> f32 { | |
dc9dc135 | 456 | intrinsics::minnumf32(self, other) |
83c7162d XL |
457 | } |
458 | ||
60c5eb7d XL |
459 | /// Rounds toward zero and converts to any primitive integer type, |
460 | /// assuming that the value is finite and fits in that type. | |
461 | /// | |
462 | /// ``` | |
60c5eb7d | 463 | /// let value = 4.6_f32; |
ba9703b0 | 464 | /// let rounded = unsafe { value.to_int_unchecked::<u16>() }; |
60c5eb7d XL |
465 | /// assert_eq!(rounded, 4); |
466 | /// | |
467 | /// let value = -128.9_f32; | |
ba9703b0 XL |
468 | /// let rounded = unsafe { value.to_int_unchecked::<i8>() }; |
469 | /// assert_eq!(rounded, i8::MIN); | |
60c5eb7d XL |
470 | /// ``` |
471 | /// | |
472 | /// # Safety | |
473 | /// | |
474 | /// The value must: | |
475 | /// | |
476 | /// * Not be `NaN` | |
477 | /// * Not be infinite | |
478 | /// * Be representable in the return type `Int`, after truncating off its fractional part | |
ba9703b0 | 479 | #[stable(feature = "float_approx_unchecked_to", since = "1.44.0")] |
60c5eb7d | 480 | #[inline] |
ba9703b0 | 481 | pub unsafe fn to_int_unchecked<Int>(self) -> Int |
dfeec247 XL |
482 | where |
483 | Self: FloatToInt<Int>, | |
484 | { | |
ba9703b0 | 485 | FloatToInt::<Int>::to_int_unchecked(self) |
60c5eb7d XL |
486 | } |
487 | ||
83c7162d XL |
488 | /// Raw transmutation to `u32`. |
489 | /// | |
490 | /// This is currently identical to `transmute::<f32, u32>(self)` on all platforms. | |
491 | /// | |
492 | /// See `from_bits` for some discussion of the portability of this operation | |
493 | /// (there are almost no issues). | |
494 | /// | |
495 | /// Note that this function is distinct from `as` casting, which attempts to | |
496 | /// preserve the *numeric* value, and not the bitwise value. | |
497 | /// | |
498 | /// # Examples | |
499 | /// | |
500 | /// ``` | |
501 | /// assert_ne!((1f32).to_bits(), 1f32 as u32); // to_bits() is not casting! | |
502 | /// assert_eq!((12.5f32).to_bits(), 0x41480000); | |
503 | /// | |
504 | /// ``` | |
505 | #[stable(feature = "float_bits_conv", since = "1.20.0")] | |
506 | #[inline] | |
507 | pub fn to_bits(self) -> u32 { | |
60c5eb7d | 508 | // SAFETY: `u32` is a plain old datatype so we can always transmute to it |
94b46f34 | 509 | unsafe { mem::transmute(self) } |
83c7162d XL |
510 | } |
511 | ||
512 | /// Raw transmutation from `u32`. | |
513 | /// | |
514 | /// This is currently identical to `transmute::<u32, f32>(v)` on all platforms. | |
515 | /// It turns out this is incredibly portable, for two reasons: | |
516 | /// | |
517 | /// * Floats and Ints have the same endianness on all supported platforms. | |
518 | /// * IEEE-754 very precisely specifies the bit layout of floats. | |
519 | /// | |
520 | /// However there is one caveat: prior to the 2008 version of IEEE-754, how | |
521 | /// to interpret the NaN signaling bit wasn't actually specified. Most platforms | |
522 | /// (notably x86 and ARM) picked the interpretation that was ultimately | |
523 | /// standardized in 2008, but some didn't (notably MIPS). As a result, all | |
524 | /// signaling NaNs on MIPS are quiet NaNs on x86, and vice-versa. | |
525 | /// | |
526 | /// Rather than trying to preserve signaling-ness cross-platform, this | |
a1dfa0c6 | 527 | /// implementation favors preserving the exact bits. This means that |
83c7162d XL |
528 | /// any payloads encoded in NaNs will be preserved even if the result of |
529 | /// this method is sent over the network from an x86 machine to a MIPS one. | |
530 | /// | |
531 | /// If the results of this method are only manipulated by the same | |
532 | /// architecture that produced them, then there is no portability concern. | |
533 | /// | |
534 | /// If the input isn't NaN, then there is no portability concern. | |
535 | /// | |
536 | /// If you don't care about signalingness (very likely), then there is no | |
537 | /// portability concern. | |
538 | /// | |
539 | /// Note that this function is distinct from `as` casting, which attempts to | |
540 | /// preserve the *numeric* value, and not the bitwise value. | |
541 | /// | |
542 | /// # Examples | |
543 | /// | |
544 | /// ``` | |
83c7162d | 545 | /// let v = f32::from_bits(0x41480000); |
416331ca | 546 | /// assert_eq!(v, 12.5); |
83c7162d XL |
547 | /// ``` |
548 | #[stable(feature = "float_bits_conv", since = "1.20.0")] | |
549 | #[inline] | |
550 | pub fn from_bits(v: u32) -> Self { | |
60c5eb7d | 551 | // SAFETY: `u32` is a plain old datatype so we can always transmute from it |
94b46f34 XL |
552 | // It turns out the safety issues with sNaN were overblown! Hooray! |
553 | unsafe { mem::transmute(v) } | |
83c7162d | 554 | } |
416331ca XL |
555 | |
556 | /// Return the memory representation of this floating point number as a byte array in | |
557 | /// big-endian (network) byte order. | |
558 | /// | |
559 | /// # Examples | |
560 | /// | |
561 | /// ``` | |
416331ca XL |
562 | /// let bytes = 12.5f32.to_be_bytes(); |
563 | /// assert_eq!(bytes, [0x41, 0x48, 0x00, 0x00]); | |
564 | /// ``` | |
e74abb32 | 565 | #[stable(feature = "float_to_from_bytes", since = "1.40.0")] |
416331ca XL |
566 | #[inline] |
567 | pub fn to_be_bytes(self) -> [u8; 4] { | |
568 | self.to_bits().to_be_bytes() | |
569 | } | |
570 | ||
571 | /// Return the memory representation of this floating point number as a byte array in | |
572 | /// little-endian byte order. | |
573 | /// | |
574 | /// # Examples | |
575 | /// | |
576 | /// ``` | |
416331ca XL |
577 | /// let bytes = 12.5f32.to_le_bytes(); |
578 | /// assert_eq!(bytes, [0x00, 0x00, 0x48, 0x41]); | |
579 | /// ``` | |
e74abb32 | 580 | #[stable(feature = "float_to_from_bytes", since = "1.40.0")] |
416331ca XL |
581 | #[inline] |
582 | pub fn to_le_bytes(self) -> [u8; 4] { | |
583 | self.to_bits().to_le_bytes() | |
584 | } | |
585 | ||
586 | /// Return the memory representation of this floating point number as a byte array in | |
587 | /// native byte order. | |
588 | /// | |
589 | /// As the target platform's native endianness is used, portable code | |
590 | /// should use [`to_be_bytes`] or [`to_le_bytes`], as appropriate, instead. | |
591 | /// | |
592 | /// [`to_be_bytes`]: #method.to_be_bytes | |
593 | /// [`to_le_bytes`]: #method.to_le_bytes | |
594 | /// | |
595 | /// # Examples | |
596 | /// | |
597 | /// ``` | |
416331ca XL |
598 | /// let bytes = 12.5f32.to_ne_bytes(); |
599 | /// assert_eq!( | |
600 | /// bytes, | |
601 | /// if cfg!(target_endian = "big") { | |
602 | /// [0x41, 0x48, 0x00, 0x00] | |
603 | /// } else { | |
604 | /// [0x00, 0x00, 0x48, 0x41] | |
605 | /// } | |
606 | /// ); | |
607 | /// ``` | |
e74abb32 | 608 | #[stable(feature = "float_to_from_bytes", since = "1.40.0")] |
416331ca XL |
609 | #[inline] |
610 | pub fn to_ne_bytes(self) -> [u8; 4] { | |
611 | self.to_bits().to_ne_bytes() | |
612 | } | |
613 | ||
614 | /// Create a floating point value from its representation as a byte array in big endian. | |
615 | /// | |
616 | /// # Examples | |
617 | /// | |
618 | /// ``` | |
416331ca XL |
619 | /// let value = f32::from_be_bytes([0x41, 0x48, 0x00, 0x00]); |
620 | /// assert_eq!(value, 12.5); | |
621 | /// ``` | |
e74abb32 | 622 | #[stable(feature = "float_to_from_bytes", since = "1.40.0")] |
416331ca XL |
623 | #[inline] |
624 | pub fn from_be_bytes(bytes: [u8; 4]) -> Self { | |
625 | Self::from_bits(u32::from_be_bytes(bytes)) | |
626 | } | |
627 | ||
628 | /// Create a floating point value from its representation as a byte array in little endian. | |
629 | /// | |
630 | /// # Examples | |
631 | /// | |
632 | /// ``` | |
416331ca XL |
633 | /// let value = f32::from_le_bytes([0x00, 0x00, 0x48, 0x41]); |
634 | /// assert_eq!(value, 12.5); | |
635 | /// ``` | |
e74abb32 | 636 | #[stable(feature = "float_to_from_bytes", since = "1.40.0")] |
416331ca XL |
637 | #[inline] |
638 | pub fn from_le_bytes(bytes: [u8; 4]) -> Self { | |
639 | Self::from_bits(u32::from_le_bytes(bytes)) | |
640 | } | |
641 | ||
642 | /// Create a floating point value from its representation as a byte array in native endian. | |
643 | /// | |
644 | /// As the target platform's native endianness is used, portable code | |
645 | /// likely wants to use [`from_be_bytes`] or [`from_le_bytes`], as | |
646 | /// appropriate instead. | |
647 | /// | |
648 | /// [`from_be_bytes`]: #method.from_be_bytes | |
649 | /// [`from_le_bytes`]: #method.from_le_bytes | |
650 | /// | |
651 | /// # Examples | |
652 | /// | |
653 | /// ``` | |
416331ca XL |
654 | /// let value = f32::from_ne_bytes(if cfg!(target_endian = "big") { |
655 | /// [0x41, 0x48, 0x00, 0x00] | |
656 | /// } else { | |
657 | /// [0x00, 0x00, 0x48, 0x41] | |
658 | /// }); | |
659 | /// assert_eq!(value, 12.5); | |
660 | /// ``` | |
e74abb32 | 661 | #[stable(feature = "float_to_from_bytes", since = "1.40.0")] |
416331ca XL |
662 | #[inline] |
663 | pub fn from_ne_bytes(bytes: [u8; 4]) -> Self { | |
664 | Self::from_bits(u32::from_ne_bytes(bytes)) | |
665 | } | |
83c7162d | 666 | } |