1 //! This module provides constants which are specific to the implementation
2 //! of the `f64` floating point data type.
4 //! *[See also the `f64` primitive type](../../std/primitive.f64.html).*
6 //! Mathematically significant numbers are provided in the `consts` sub-module.
8 #![stable(feature = "rust1", since = "1.0.0")]
11 use crate::intrinsics;
14 use crate::num::FpCategory;
16 /// The radix or base of the internal representation of `f64`.
17 #[stable(feature = "rust1", since = "1.0.0")]
18 pub const RADIX: u32 = 2;
20 /// Number of significant digits in base 2.
21 #[stable(feature = "rust1", since = "1.0.0")]
22 pub const MANTISSA_DIGITS: u32 = 53;
23 /// Approximate number of significant digits in base 10.
24 #[stable(feature = "rust1", since = "1.0.0")]
25 pub const DIGITS: u32 = 15;
27 /// [Machine epsilon] value for `f64`.
29 /// This is the difference between `1.0` and the next larger representable number.
31 /// [Machine epsilon]: https://en.wikipedia.org/wiki/Machine_epsilon
32 #[stable(feature = "rust1", since = "1.0.0")]
33 pub const EPSILON: f64 = 2.2204460492503131e-16_f64;
35 /// Smallest finite `f64` value.
36 #[stable(feature = "rust1", since = "1.0.0")]
37 pub const MIN: f64 = -1.7976931348623157e+308_f64;
38 /// Smallest positive normal `f64` value.
39 #[stable(feature = "rust1", since = "1.0.0")]
40 pub const MIN_POSITIVE: f64 = 2.2250738585072014e-308_f64;
41 /// Largest finite `f64` value.
42 #[stable(feature = "rust1", since = "1.0.0")]
43 pub const MAX: f64 = 1.7976931348623157e+308_f64;
45 /// One greater than the minimum possible normal power of 2 exponent.
46 #[stable(feature = "rust1", since = "1.0.0")]
47 pub const MIN_EXP: i32 = -1021;
48 /// Maximum possible power of 2 exponent.
49 #[stable(feature = "rust1", since = "1.0.0")]
50 pub const MAX_EXP: i32 = 1024;
52 /// Minimum possible normal power of 10 exponent.
53 #[stable(feature = "rust1", since = "1.0.0")]
54 pub const MIN_10_EXP: i32 = -307;
55 /// Maximum possible power of 10 exponent.
56 #[stable(feature = "rust1", since = "1.0.0")]
57 pub const MAX_10_EXP: i32 = 308;
59 /// Not a Number (NaN).
60 #[stable(feature = "rust1", since = "1.0.0")]
61 pub const NAN: f64 = 0.0_f64 / 0.0_f64;
63 #[stable(feature = "rust1", since = "1.0.0")]
64 pub const INFINITY: f64 = 1.0_f64 / 0.0_f64;
65 /// Negative infinity (-∞).
66 #[stable(feature = "rust1", since = "1.0.0")]
67 pub const NEG_INFINITY: f64 = -1.0_f64 / 0.0_f64;
69 /// Basic mathematical constants.
70 #[stable(feature = "rust1", since = "1.0.0")]
72 // FIXME: replace with mathematical constants from cmath.
74 /// Archimedes' constant (π)
75 #[stable(feature = "rust1", since = "1.0.0")]
76 pub const PI: f64 = 3.14159265358979323846264338327950288_f64;
78 /// The full circle constant (τ)
81 #[unstable(feature = "tau_constant", issue = "66770")]
82 pub const TAU: f64 = 6.28318530717958647692528676655900577_f64;
85 #[stable(feature = "rust1", since = "1.0.0")]
86 pub const FRAC_PI_2: f64 = 1.57079632679489661923132169163975144_f64;
89 #[stable(feature = "rust1", since = "1.0.0")]
90 pub const FRAC_PI_3: f64 = 1.04719755119659774615421446109316763_f64;
93 #[stable(feature = "rust1", since = "1.0.0")]
94 pub const FRAC_PI_4: f64 = 0.785398163397448309615660845819875721_f64;
97 #[stable(feature = "rust1", since = "1.0.0")]
98 pub const FRAC_PI_6: f64 = 0.52359877559829887307710723054658381_f64;
101 #[stable(feature = "rust1", since = "1.0.0")]
102 pub const FRAC_PI_8: f64 = 0.39269908169872415480783042290993786_f64;
105 #[stable(feature = "rust1", since = "1.0.0")]
106 pub const FRAC_1_PI: f64 = 0.318309886183790671537767526745028724_f64;
109 #[stable(feature = "rust1", since = "1.0.0")]
110 pub const FRAC_2_PI: f64 = 0.636619772367581343075535053490057448_f64;
113 #[stable(feature = "rust1", since = "1.0.0")]
114 pub const FRAC_2_SQRT_PI: f64 = 1.12837916709551257389615890312154517_f64;
117 #[stable(feature = "rust1", since = "1.0.0")]
118 pub const SQRT_2: f64 = 1.41421356237309504880168872420969808_f64;
121 #[stable(feature = "rust1", since = "1.0.0")]
122 pub const FRAC_1_SQRT_2: f64 = 0.707106781186547524400844362104849039_f64;
124 /// Euler's number (e)
125 #[stable(feature = "rust1", since = "1.0.0")]
126 pub const E: f64 = 2.71828182845904523536028747135266250_f64;
128 /// log<sub>2</sub>(10)
129 #[unstable(feature = "extra_log_consts", issue = "50540")]
130 pub const LOG2_10: f64 = 3.32192809488736234787031942948939018_f64;
132 /// log<sub>2</sub>(e)
133 #[stable(feature = "rust1", since = "1.0.0")]
134 pub const LOG2_E: f64 = 1.44269504088896340735992468100189214_f64;
136 /// log<sub>10</sub>(2)
137 #[unstable(feature = "extra_log_consts", issue = "50540")]
138 pub const LOG10_2: f64 = 0.301029995663981195213738894724493027_f64;
140 /// log<sub>10</sub>(e)
141 #[stable(feature = "rust1", since = "1.0.0")]
142 pub const LOG10_E: f64 = 0.434294481903251827651128918916605082_f64;
145 #[stable(feature = "rust1", since = "1.0.0")]
146 pub const LN_2: f64 = 0.693147180559945309417232121458176568_f64;
149 #[stable(feature = "rust1", since = "1.0.0")]
150 pub const LN_10: f64 = 2.30258509299404568401799145468436421_f64;
156 /// Returns `true` if this value is `NaN`.
161 /// let nan = f64::NAN;
164 /// assert!(nan.is_nan());
165 /// assert!(!f.is_nan());
167 #[stable(feature = "rust1", since = "1.0.0")]
169 pub fn is_nan(self) -> bool {
173 // FIXME(#50145): `abs` is publicly unavailable in libcore due to
174 // concerns about portability, so this implementation is for
175 // private use internally.
177 fn abs_private(self) -> f64 {
178 f64::from_bits(self.to_bits() & 0x7fff_ffff_ffff_ffff)
181 /// Returns `true` if this value is positive infinity or negative infinity, and
182 /// `false` otherwise.
188 /// let inf = f64::INFINITY;
189 /// let neg_inf = f64::NEG_INFINITY;
190 /// let nan = f64::NAN;
192 /// assert!(!f.is_infinite());
193 /// assert!(!nan.is_infinite());
195 /// assert!(inf.is_infinite());
196 /// assert!(neg_inf.is_infinite());
198 #[stable(feature = "rust1", since = "1.0.0")]
200 pub fn is_infinite(self) -> bool {
201 self.abs_private() == INFINITY
204 /// Returns `true` if this number is neither infinite nor `NaN`.
210 /// let inf: f64 = f64::INFINITY;
211 /// let neg_inf: f64 = f64::NEG_INFINITY;
212 /// let nan: f64 = f64::NAN;
214 /// assert!(f.is_finite());
216 /// assert!(!nan.is_finite());
217 /// assert!(!inf.is_finite());
218 /// assert!(!neg_inf.is_finite());
220 #[stable(feature = "rust1", since = "1.0.0")]
222 pub fn is_finite(self) -> bool {
223 // There's no need to handle NaN separately: if self is NaN,
224 // the comparison is not true, exactly as desired.
225 self.abs_private() < INFINITY
228 /// Returns `true` if the number is neither zero, infinite,
229 /// [subnormal][subnormal], or `NaN`.
234 /// let min = f64::MIN_POSITIVE; // 2.2250738585072014e-308f64
235 /// let max = f64::MAX;
236 /// let lower_than_min = 1.0e-308_f64;
237 /// let zero = 0.0f64;
239 /// assert!(min.is_normal());
240 /// assert!(max.is_normal());
242 /// assert!(!zero.is_normal());
243 /// assert!(!f64::NAN.is_normal());
244 /// assert!(!f64::INFINITY.is_normal());
245 /// // Values between `0` and `min` are Subnormal.
246 /// assert!(!lower_than_min.is_normal());
248 /// [subnormal]: https://en.wikipedia.org/wiki/Denormal_number
249 #[stable(feature = "rust1", since = "1.0.0")]
251 pub fn is_normal(self) -> bool {
252 self.classify() == FpCategory::Normal
255 /// Returns the floating point category of the number. If only one property
256 /// is going to be tested, it is generally faster to use the specific
257 /// predicate instead.
260 /// use std::num::FpCategory;
263 /// let num = 12.4_f64;
264 /// let inf = f64::INFINITY;
266 /// assert_eq!(num.classify(), FpCategory::Normal);
267 /// assert_eq!(inf.classify(), FpCategory::Infinite);
269 #[stable(feature = "rust1", since = "1.0.0")]
270 pub fn classify(self) -> FpCategory {
271 const EXP_MASK: u64 = 0x7ff0000000000000;
272 const MAN_MASK: u64 = 0x000fffffffffffff;
274 let bits = self.to_bits();
275 match (bits & MAN_MASK, bits & EXP_MASK) {
276 (0, 0) => FpCategory::Zero,
277 (_, 0) => FpCategory::Subnormal,
278 (0, EXP_MASK) => FpCategory::Infinite,
279 (_, EXP_MASK) => FpCategory::Nan,
280 _ => FpCategory::Normal,
284 /// Returns `true` if `self` has a positive sign, including `+0.0`, `NaN`s with
285 /// positive sign bit and positive infinity.
289 /// let g = -7.0_f64;
291 /// assert!(f.is_sign_positive());
292 /// assert!(!g.is_sign_positive());
294 #[stable(feature = "rust1", since = "1.0.0")]
296 pub fn is_sign_positive(self) -> bool {
297 !self.is_sign_negative()
300 #[stable(feature = "rust1", since = "1.0.0")]
301 #[rustc_deprecated(since = "1.0.0", reason = "renamed to is_sign_positive")]
304 pub fn is_positive(self) -> bool {
305 self.is_sign_positive()
308 /// Returns `true` if `self` has a negative sign, including `-0.0`, `NaN`s with
309 /// negative sign bit and negative infinity.
313 /// let g = -7.0_f64;
315 /// assert!(!f.is_sign_negative());
316 /// assert!(g.is_sign_negative());
318 #[stable(feature = "rust1", since = "1.0.0")]
320 pub fn is_sign_negative(self) -> bool {
321 self.to_bits() & 0x8000_0000_0000_0000 != 0
324 #[stable(feature = "rust1", since = "1.0.0")]
325 #[rustc_deprecated(since = "1.0.0", reason = "renamed to is_sign_negative")]
328 pub fn is_negative(self) -> bool {
329 self.is_sign_negative()
332 /// Takes the reciprocal (inverse) of a number, `1/x`.
336 /// let abs_difference = (x.recip() - (1.0 / x)).abs();
338 /// assert!(abs_difference < 1e-10);
340 #[stable(feature = "rust1", since = "1.0.0")]
342 pub fn recip(self) -> f64 {
346 /// Converts radians to degrees.
349 /// use std::f64::consts;
351 /// let angle = consts::PI;
353 /// let abs_difference = (angle.to_degrees() - 180.0).abs();
355 /// assert!(abs_difference < 1e-10);
357 #[stable(feature = "rust1", since = "1.0.0")]
359 pub fn to_degrees(self) -> f64 {
360 // The division here is correctly rounded with respect to the true
361 // value of 180/π. (This differs from f32, where a constant must be
362 // used to ensure a correctly rounded result.)
363 self * (180.0f64 / consts::PI)
366 /// Converts degrees to radians.
369 /// use std::f64::consts;
371 /// let angle = 180.0_f64;
373 /// let abs_difference = (angle.to_radians() - consts::PI).abs();
375 /// assert!(abs_difference < 1e-10);
377 #[stable(feature = "rust1", since = "1.0.0")]
379 pub fn to_radians(self) -> f64 {
380 let value: f64 = consts::PI;
381 self * (value / 180.0)
384 /// Returns the maximum of the two numbers.
390 /// assert_eq!(x.max(y), y);
393 /// If one of the arguments is NaN, then the other argument is returned.
394 #[stable(feature = "rust1", since = "1.0.0")]
396 pub fn max(self, other: f64) -> f64 {
397 intrinsics::maxnumf64(self, other)
400 /// Returns the minimum of the two numbers.
406 /// assert_eq!(x.min(y), x);
409 /// If one of the arguments is NaN, then the other argument is returned.
410 #[stable(feature = "rust1", since = "1.0.0")]
412 pub fn min(self, other: f64) -> f64 {
413 intrinsics::minnumf64(self, other)
416 /// Raw transmutation to `u64`.
418 /// This is currently identical to `transmute::<f64, u64>(self)` on all platforms.
420 /// See `from_bits` for some discussion of the portability of this operation
421 /// (there are almost no issues).
423 /// Note that this function is distinct from `as` casting, which attempts to
424 /// preserve the *numeric* value, and not the bitwise value.
429 /// assert!((1f64).to_bits() != 1f64 as u64); // to_bits() is not casting!
430 /// assert_eq!((12.5f64).to_bits(), 0x4029000000000000);
433 #[stable(feature = "float_bits_conv", since = "1.20.0")]
435 pub fn to_bits(self) -> u64 {
436 // SAFETY: `u64` is a plain old datatype so we can always transmute to it
437 unsafe { mem::transmute(self) }
440 /// Raw transmutation from `u64`.
442 /// This is currently identical to `transmute::<u64, f64>(v)` on all platforms.
443 /// It turns out this is incredibly portable, for two reasons:
445 /// * Floats and Ints have the same endianness on all supported platforms.
446 /// * IEEE-754 very precisely specifies the bit layout of floats.
448 /// However there is one caveat: prior to the 2008 version of IEEE-754, how
449 /// to interpret the NaN signaling bit wasn't actually specified. Most platforms
450 /// (notably x86 and ARM) picked the interpretation that was ultimately
451 /// standardized in 2008, but some didn't (notably MIPS). As a result, all
452 /// signaling NaNs on MIPS are quiet NaNs on x86, and vice-versa.
454 /// Rather than trying to preserve signaling-ness cross-platform, this
455 /// implementation favours preserving the exact bits. This means that
456 /// any payloads encoded in NaNs will be preserved even if the result of
457 /// this method is sent over the network from an x86 machine to a MIPS one.
459 /// If the results of this method are only manipulated by the same
460 /// architecture that produced them, then there is no portability concern.
462 /// If the input isn't NaN, then there is no portability concern.
464 /// If you don't care about signalingness (very likely), then there is no
465 /// portability concern.
467 /// Note that this function is distinct from `as` casting, which attempts to
468 /// preserve the *numeric* value, and not the bitwise value.
473 /// let v = f64::from_bits(0x4029000000000000);
474 /// assert_eq!(v, 12.5);
476 #[stable(feature = "float_bits_conv", since = "1.20.0")]
478 pub fn from_bits(v: u64) -> Self {
479 // SAFETY: `u64` is a plain old datatype so we can always transmute from it
480 // It turns out the safety issues with sNaN were overblown! Hooray!
481 unsafe { mem::transmute(v) }
484 /// Return the memory representation of this floating point number as a byte array in
485 /// big-endian (network) byte order.
490 /// let bytes = 12.5f64.to_be_bytes();
491 /// assert_eq!(bytes, [0x40, 0x29, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
493 #[stable(feature = "float_to_from_bytes", since = "1.40.0")]
495 pub fn to_be_bytes(self) -> [u8; 8] {
496 self.to_bits().to_be_bytes()
499 /// Return the memory representation of this floating point number as a byte array in
500 /// little-endian byte order.
505 /// let bytes = 12.5f64.to_le_bytes();
506 /// assert_eq!(bytes, [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x29, 0x40]);
508 #[stable(feature = "float_to_from_bytes", since = "1.40.0")]
510 pub fn to_le_bytes(self) -> [u8; 8] {
511 self.to_bits().to_le_bytes()
514 /// Return the memory representation of this floating point number as a byte array in
515 /// native byte order.
517 /// As the target platform's native endianness is used, portable code
518 /// should use [`to_be_bytes`] or [`to_le_bytes`], as appropriate, instead.
520 /// [`to_be_bytes`]: #method.to_be_bytes
521 /// [`to_le_bytes`]: #method.to_le_bytes
526 /// let bytes = 12.5f64.to_ne_bytes();
529 /// if cfg!(target_endian = "big") {
530 /// [0x40, 0x29, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
532 /// [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x29, 0x40]
536 #[stable(feature = "float_to_from_bytes", since = "1.40.0")]
538 pub fn to_ne_bytes(self) -> [u8; 8] {
539 self.to_bits().to_ne_bytes()
542 /// Create a floating point value from its representation as a byte array in big endian.
547 /// let value = f64::from_be_bytes([0x40, 0x29, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
548 /// assert_eq!(value, 12.5);
550 #[stable(feature = "float_to_from_bytes", since = "1.40.0")]
552 pub fn from_be_bytes(bytes: [u8; 8]) -> Self {
553 Self::from_bits(u64::from_be_bytes(bytes))
556 /// Create a floating point value from its representation as a byte array in little endian.
561 /// let value = f64::from_le_bytes([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x29, 0x40]);
562 /// assert_eq!(value, 12.5);
564 #[stable(feature = "float_to_from_bytes", since = "1.40.0")]
566 pub fn from_le_bytes(bytes: [u8; 8]) -> Self {
567 Self::from_bits(u64::from_le_bytes(bytes))
570 /// Create a floating point value from its representation as a byte array in native endian.
572 /// As the target platform's native endianness is used, portable code
573 /// likely wants to use [`from_be_bytes`] or [`from_le_bytes`], as
574 /// appropriate instead.
576 /// [`from_be_bytes`]: #method.from_be_bytes
577 /// [`from_le_bytes`]: #method.from_le_bytes
582 /// let value = f64::from_ne_bytes(if cfg!(target_endian = "big") {
583 /// [0x40, 0x29, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
585 /// [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x29, 0x40]
587 /// assert_eq!(value, 12.5);
589 #[stable(feature = "float_to_from_bytes", since = "1.40.0")]
591 pub fn from_ne_bytes(bytes: [u8; 8]) -> Self {
592 Self::from_bits(u64::from_ne_bytes(bytes))