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")]
10 #[cfg(not(bootstrap))]
11 use crate::convert::FloatToInt;
13 use crate::intrinsics;
15 use crate::num::FpCategory;
17 /// The radix or base of the internal representation of `f64`.
18 #[stable(feature = "rust1", since = "1.0.0")]
19 pub const RADIX: u32 = 2;
21 /// Number of significant digits in base 2.
22 #[stable(feature = "rust1", since = "1.0.0")]
23 pub const MANTISSA_DIGITS: u32 = 53;
24 /// Approximate number of significant digits in base 10.
25 #[stable(feature = "rust1", since = "1.0.0")]
26 pub const DIGITS: u32 = 15;
28 /// [Machine epsilon] value for `f64`.
30 /// This is the difference between `1.0` and the next larger representable number.
32 /// [Machine epsilon]: https://en.wikipedia.org/wiki/Machine_epsilon
33 #[stable(feature = "rust1", since = "1.0.0")]
34 pub const EPSILON: f64 = 2.2204460492503131e-16_f64;
36 /// Smallest finite `f64` value.
37 #[stable(feature = "rust1", since = "1.0.0")]
38 pub const MIN: f64 = -1.7976931348623157e+308_f64;
39 /// Smallest positive normal `f64` value.
40 #[stable(feature = "rust1", since = "1.0.0")]
41 pub const MIN_POSITIVE: f64 = 2.2250738585072014e-308_f64;
42 /// Largest finite `f64` value.
43 #[stable(feature = "rust1", since = "1.0.0")]
44 pub const MAX: f64 = 1.7976931348623157e+308_f64;
46 /// One greater than the minimum possible normal power of 2 exponent.
47 #[stable(feature = "rust1", since = "1.0.0")]
48 pub const MIN_EXP: i32 = -1021;
49 /// Maximum possible power of 2 exponent.
50 #[stable(feature = "rust1", since = "1.0.0")]
51 pub const MAX_EXP: i32 = 1024;
53 /// Minimum possible normal power of 10 exponent.
54 #[stable(feature = "rust1", since = "1.0.0")]
55 pub const MIN_10_EXP: i32 = -307;
56 /// Maximum possible power of 10 exponent.
57 #[stable(feature = "rust1", since = "1.0.0")]
58 pub const MAX_10_EXP: i32 = 308;
60 /// Not a Number (NaN).
61 #[stable(feature = "rust1", since = "1.0.0")]
62 pub const NAN: f64 = 0.0_f64 / 0.0_f64;
64 #[stable(feature = "rust1", since = "1.0.0")]
65 pub const INFINITY: f64 = 1.0_f64 / 0.0_f64;
66 /// Negative infinity (-∞).
67 #[stable(feature = "rust1", since = "1.0.0")]
68 pub const NEG_INFINITY: f64 = -1.0_f64 / 0.0_f64;
70 /// Basic mathematical constants.
71 #[stable(feature = "rust1", since = "1.0.0")]
73 // FIXME: replace with mathematical constants from cmath.
75 /// Archimedes' constant (π)
76 #[stable(feature = "rust1", since = "1.0.0")]
77 pub const PI: f64 = 3.14159265358979323846264338327950288_f64;
79 /// The full circle constant (τ)
82 #[unstable(feature = "tau_constant", issue = "66770")]
83 pub const TAU: f64 = 6.28318530717958647692528676655900577_f64;
86 #[stable(feature = "rust1", since = "1.0.0")]
87 pub const FRAC_PI_2: f64 = 1.57079632679489661923132169163975144_f64;
90 #[stable(feature = "rust1", since = "1.0.0")]
91 pub const FRAC_PI_3: f64 = 1.04719755119659774615421446109316763_f64;
94 #[stable(feature = "rust1", since = "1.0.0")]
95 pub const FRAC_PI_4: f64 = 0.785398163397448309615660845819875721_f64;
98 #[stable(feature = "rust1", since = "1.0.0")]
99 pub const FRAC_PI_6: f64 = 0.52359877559829887307710723054658381_f64;
102 #[stable(feature = "rust1", since = "1.0.0")]
103 pub const FRAC_PI_8: f64 = 0.39269908169872415480783042290993786_f64;
106 #[stable(feature = "rust1", since = "1.0.0")]
107 pub const FRAC_1_PI: f64 = 0.318309886183790671537767526745028724_f64;
110 #[stable(feature = "rust1", since = "1.0.0")]
111 pub const FRAC_2_PI: f64 = 0.636619772367581343075535053490057448_f64;
114 #[stable(feature = "rust1", since = "1.0.0")]
115 pub const FRAC_2_SQRT_PI: f64 = 1.12837916709551257389615890312154517_f64;
118 #[stable(feature = "rust1", since = "1.0.0")]
119 pub const SQRT_2: f64 = 1.41421356237309504880168872420969808_f64;
122 #[stable(feature = "rust1", since = "1.0.0")]
123 pub const FRAC_1_SQRT_2: f64 = 0.707106781186547524400844362104849039_f64;
125 /// Euler's number (e)
126 #[stable(feature = "rust1", since = "1.0.0")]
127 pub const E: f64 = 2.71828182845904523536028747135266250_f64;
129 /// log<sub>2</sub>(10)
130 #[unstable(feature = "extra_log_consts", issue = "50540")]
131 pub const LOG2_10: f64 = 3.32192809488736234787031942948939018_f64;
133 /// log<sub>2</sub>(e)
134 #[stable(feature = "rust1", since = "1.0.0")]
135 pub const LOG2_E: f64 = 1.44269504088896340735992468100189214_f64;
137 /// log<sub>10</sub>(2)
138 #[unstable(feature = "extra_log_consts", issue = "50540")]
139 pub const LOG10_2: f64 = 0.301029995663981195213738894724493027_f64;
141 /// log<sub>10</sub>(e)
142 #[stable(feature = "rust1", since = "1.0.0")]
143 pub const LOG10_E: f64 = 0.434294481903251827651128918916605082_f64;
146 #[stable(feature = "rust1", since = "1.0.0")]
147 pub const LN_2: f64 = 0.693147180559945309417232121458176568_f64;
150 #[stable(feature = "rust1", since = "1.0.0")]
151 pub const LN_10: f64 = 2.30258509299404568401799145468436421_f64;
157 /// Returns `true` if this value is `NaN`.
162 /// let nan = f64::NAN;
165 /// assert!(nan.is_nan());
166 /// assert!(!f.is_nan());
168 #[stable(feature = "rust1", since = "1.0.0")]
170 pub fn is_nan(self) -> bool {
174 // FIXME(#50145): `abs` is publicly unavailable in libcore due to
175 // concerns about portability, so this implementation is for
176 // private use internally.
178 fn abs_private(self) -> f64 {
179 f64::from_bits(self.to_bits() & 0x7fff_ffff_ffff_ffff)
182 /// Returns `true` if this value is positive infinity or negative infinity, and
183 /// `false` otherwise.
189 /// let inf = f64::INFINITY;
190 /// let neg_inf = f64::NEG_INFINITY;
191 /// let nan = f64::NAN;
193 /// assert!(!f.is_infinite());
194 /// assert!(!nan.is_infinite());
196 /// assert!(inf.is_infinite());
197 /// assert!(neg_inf.is_infinite());
199 #[stable(feature = "rust1", since = "1.0.0")]
201 pub fn is_infinite(self) -> bool {
202 self.abs_private() == INFINITY
205 /// Returns `true` if this number is neither infinite nor `NaN`.
211 /// let inf: f64 = f64::INFINITY;
212 /// let neg_inf: f64 = f64::NEG_INFINITY;
213 /// let nan: f64 = f64::NAN;
215 /// assert!(f.is_finite());
217 /// assert!(!nan.is_finite());
218 /// assert!(!inf.is_finite());
219 /// assert!(!neg_inf.is_finite());
221 #[stable(feature = "rust1", since = "1.0.0")]
223 pub fn is_finite(self) -> bool {
224 // There's no need to handle NaN separately: if self is NaN,
225 // the comparison is not true, exactly as desired.
226 self.abs_private() < INFINITY
229 /// Returns `true` if the number is neither zero, infinite,
230 /// [subnormal][subnormal], or `NaN`.
235 /// let min = f64::MIN_POSITIVE; // 2.2250738585072014e-308f64
236 /// let max = f64::MAX;
237 /// let lower_than_min = 1.0e-308_f64;
238 /// let zero = 0.0f64;
240 /// assert!(min.is_normal());
241 /// assert!(max.is_normal());
243 /// assert!(!zero.is_normal());
244 /// assert!(!f64::NAN.is_normal());
245 /// assert!(!f64::INFINITY.is_normal());
246 /// // Values between `0` and `min` are Subnormal.
247 /// assert!(!lower_than_min.is_normal());
249 /// [subnormal]: https://en.wikipedia.org/wiki/Denormal_number
250 #[stable(feature = "rust1", since = "1.0.0")]
252 pub fn is_normal(self) -> bool {
253 self.classify() == FpCategory::Normal
256 /// Returns the floating point category of the number. If only one property
257 /// is going to be tested, it is generally faster to use the specific
258 /// predicate instead.
261 /// use std::num::FpCategory;
264 /// let num = 12.4_f64;
265 /// let inf = f64::INFINITY;
267 /// assert_eq!(num.classify(), FpCategory::Normal);
268 /// assert_eq!(inf.classify(), FpCategory::Infinite);
270 #[stable(feature = "rust1", since = "1.0.0")]
271 pub fn classify(self) -> FpCategory {
272 const EXP_MASK: u64 = 0x7ff0000000000000;
273 const MAN_MASK: u64 = 0x000fffffffffffff;
275 let bits = self.to_bits();
276 match (bits & MAN_MASK, bits & EXP_MASK) {
277 (0, 0) => FpCategory::Zero,
278 (_, 0) => FpCategory::Subnormal,
279 (0, EXP_MASK) => FpCategory::Infinite,
280 (_, EXP_MASK) => FpCategory::Nan,
281 _ => FpCategory::Normal,
285 /// Returns `true` if `self` has a positive sign, including `+0.0`, `NaN`s with
286 /// positive sign bit and positive infinity.
290 /// let g = -7.0_f64;
292 /// assert!(f.is_sign_positive());
293 /// assert!(!g.is_sign_positive());
295 #[stable(feature = "rust1", since = "1.0.0")]
297 pub fn is_sign_positive(self) -> bool {
298 !self.is_sign_negative()
301 #[stable(feature = "rust1", since = "1.0.0")]
302 #[rustc_deprecated(since = "1.0.0", reason = "renamed to is_sign_positive")]
305 pub fn is_positive(self) -> bool {
306 self.is_sign_positive()
309 /// Returns `true` if `self` has a negative sign, including `-0.0`, `NaN`s with
310 /// negative sign bit and negative infinity.
314 /// let g = -7.0_f64;
316 /// assert!(!f.is_sign_negative());
317 /// assert!(g.is_sign_negative());
319 #[stable(feature = "rust1", since = "1.0.0")]
321 pub fn is_sign_negative(self) -> bool {
322 self.to_bits() & 0x8000_0000_0000_0000 != 0
325 #[stable(feature = "rust1", since = "1.0.0")]
326 #[rustc_deprecated(since = "1.0.0", reason = "renamed to is_sign_negative")]
329 pub fn is_negative(self) -> bool {
330 self.is_sign_negative()
333 /// Takes the reciprocal (inverse) of a number, `1/x`.
337 /// let abs_difference = (x.recip() - (1.0 / x)).abs();
339 /// assert!(abs_difference < 1e-10);
341 #[stable(feature = "rust1", since = "1.0.0")]
343 pub fn recip(self) -> f64 {
347 /// Converts radians to degrees.
350 /// use std::f64::consts;
352 /// let angle = consts::PI;
354 /// let abs_difference = (angle.to_degrees() - 180.0).abs();
356 /// assert!(abs_difference < 1e-10);
358 #[stable(feature = "rust1", since = "1.0.0")]
360 pub fn to_degrees(self) -> f64 {
361 // The division here is correctly rounded with respect to the true
362 // value of 180/π. (This differs from f32, where a constant must be
363 // used to ensure a correctly rounded result.)
364 self * (180.0f64 / consts::PI)
367 /// Converts degrees to radians.
370 /// use std::f64::consts;
372 /// let angle = 180.0_f64;
374 /// let abs_difference = (angle.to_radians() - consts::PI).abs();
376 /// assert!(abs_difference < 1e-10);
378 #[stable(feature = "rust1", since = "1.0.0")]
380 pub fn to_radians(self) -> f64 {
381 let value: f64 = consts::PI;
382 self * (value / 180.0)
385 /// Returns the maximum of the two numbers.
391 /// assert_eq!(x.max(y), y);
394 /// If one of the arguments is NaN, then the other argument is returned.
395 #[stable(feature = "rust1", since = "1.0.0")]
397 pub fn max(self, other: f64) -> f64 {
398 intrinsics::maxnumf64(self, other)
401 /// Returns the minimum of the two numbers.
407 /// assert_eq!(x.min(y), x);
410 /// If one of the arguments is NaN, then the other argument is returned.
411 #[stable(feature = "rust1", since = "1.0.0")]
413 pub fn min(self, other: f64) -> f64 {
414 intrinsics::minnumf64(self, other)
417 /// Rounds toward zero and converts to any primitive integer type,
418 /// assuming that the value is finite and fits in that type.
421 /// #![feature(float_approx_unchecked_to)]
423 /// let value = 4.6_f32;
424 /// let rounded = unsafe { value.approx_unchecked_to::<u16>() };
425 /// assert_eq!(rounded, 4);
427 /// let value = -128.9_f32;
428 /// let rounded = unsafe { value.approx_unchecked_to::<i8>() };
429 /// assert_eq!(rounded, std::i8::MIN);
437 /// * Not be infinite
438 /// * Be representable in the return type `Int`, after truncating off its fractional part
439 #[cfg(not(bootstrap))]
440 #[unstable(feature = "float_approx_unchecked_to", issue = "67058")]
442 pub unsafe fn approx_unchecked_to<Int>(self) -> Int where Self: FloatToInt<Int> {
443 FloatToInt::<Int>::approx_unchecked(self)
446 /// Raw transmutation to `u64`.
448 /// This is currently identical to `transmute::<f64, u64>(self)` on all platforms.
450 /// See `from_bits` for some discussion of the portability of this operation
451 /// (there are almost no issues).
453 /// Note that this function is distinct from `as` casting, which attempts to
454 /// preserve the *numeric* value, and not the bitwise value.
459 /// assert!((1f64).to_bits() != 1f64 as u64); // to_bits() is not casting!
460 /// assert_eq!((12.5f64).to_bits(), 0x4029000000000000);
463 #[stable(feature = "float_bits_conv", since = "1.20.0")]
465 pub fn to_bits(self) -> u64 {
466 // SAFETY: `u64` is a plain old datatype so we can always transmute to it
467 unsafe { mem::transmute(self) }
470 /// Raw transmutation from `u64`.
472 /// This is currently identical to `transmute::<u64, f64>(v)` on all platforms.
473 /// It turns out this is incredibly portable, for two reasons:
475 /// * Floats and Ints have the same endianness on all supported platforms.
476 /// * IEEE-754 very precisely specifies the bit layout of floats.
478 /// However there is one caveat: prior to the 2008 version of IEEE-754, how
479 /// to interpret the NaN signaling bit wasn't actually specified. Most platforms
480 /// (notably x86 and ARM) picked the interpretation that was ultimately
481 /// standardized in 2008, but some didn't (notably MIPS). As a result, all
482 /// signaling NaNs on MIPS are quiet NaNs on x86, and vice-versa.
484 /// Rather than trying to preserve signaling-ness cross-platform, this
485 /// implementation favours preserving the exact bits. This means that
486 /// any payloads encoded in NaNs will be preserved even if the result of
487 /// this method is sent over the network from an x86 machine to a MIPS one.
489 /// If the results of this method are only manipulated by the same
490 /// architecture that produced them, then there is no portability concern.
492 /// If the input isn't NaN, then there is no portability concern.
494 /// If you don't care about signalingness (very likely), then there is no
495 /// portability concern.
497 /// Note that this function is distinct from `as` casting, which attempts to
498 /// preserve the *numeric* value, and not the bitwise value.
503 /// let v = f64::from_bits(0x4029000000000000);
504 /// assert_eq!(v, 12.5);
506 #[stable(feature = "float_bits_conv", since = "1.20.0")]
508 pub fn from_bits(v: u64) -> Self {
509 // SAFETY: `u64` is a plain old datatype so we can always transmute from it
510 // It turns out the safety issues with sNaN were overblown! Hooray!
511 unsafe { mem::transmute(v) }
514 /// Return the memory representation of this floating point number as a byte array in
515 /// big-endian (network) byte order.
520 /// let bytes = 12.5f64.to_be_bytes();
521 /// assert_eq!(bytes, [0x40, 0x29, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
523 #[stable(feature = "float_to_from_bytes", since = "1.40.0")]
525 pub fn to_be_bytes(self) -> [u8; 8] {
526 self.to_bits().to_be_bytes()
529 /// Return the memory representation of this floating point number as a byte array in
530 /// little-endian byte order.
535 /// let bytes = 12.5f64.to_le_bytes();
536 /// assert_eq!(bytes, [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x29, 0x40]);
538 #[stable(feature = "float_to_from_bytes", since = "1.40.0")]
540 pub fn to_le_bytes(self) -> [u8; 8] {
541 self.to_bits().to_le_bytes()
544 /// Return the memory representation of this floating point number as a byte array in
545 /// native byte order.
547 /// As the target platform's native endianness is used, portable code
548 /// should use [`to_be_bytes`] or [`to_le_bytes`], as appropriate, instead.
550 /// [`to_be_bytes`]: #method.to_be_bytes
551 /// [`to_le_bytes`]: #method.to_le_bytes
556 /// let bytes = 12.5f64.to_ne_bytes();
559 /// if cfg!(target_endian = "big") {
560 /// [0x40, 0x29, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
562 /// [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x29, 0x40]
566 #[stable(feature = "float_to_from_bytes", since = "1.40.0")]
568 pub fn to_ne_bytes(self) -> [u8; 8] {
569 self.to_bits().to_ne_bytes()
572 /// Create a floating point value from its representation as a byte array in big endian.
577 /// let value = f64::from_be_bytes([0x40, 0x29, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
578 /// assert_eq!(value, 12.5);
580 #[stable(feature = "float_to_from_bytes", since = "1.40.0")]
582 pub fn from_be_bytes(bytes: [u8; 8]) -> Self {
583 Self::from_bits(u64::from_be_bytes(bytes))
586 /// Create a floating point value from its representation as a byte array in little endian.
591 /// let value = f64::from_le_bytes([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x29, 0x40]);
592 /// assert_eq!(value, 12.5);
594 #[stable(feature = "float_to_from_bytes", since = "1.40.0")]
596 pub fn from_le_bytes(bytes: [u8; 8]) -> Self {
597 Self::from_bits(u64::from_le_bytes(bytes))
600 /// Create a floating point value from its representation as a byte array in native endian.
602 /// As the target platform's native endianness is used, portable code
603 /// likely wants to use [`from_be_bytes`] or [`from_le_bytes`], as
604 /// appropriate instead.
606 /// [`from_be_bytes`]: #method.from_be_bytes
607 /// [`from_le_bytes`]: #method.from_le_bytes
612 /// let value = f64::from_ne_bytes(if cfg!(target_endian = "big") {
613 /// [0x40, 0x29, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
615 /// [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x29, 0x40]
617 /// assert_eq!(value, 12.5);
619 #[stable(feature = "float_to_from_bytes", since = "1.40.0")]
621 pub fn from_ne_bytes(bytes: [u8; 8]) -> Self {
622 Self::from_bits(u64::from_ne_bytes(bytes))