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")]
13 /// The radix or base of the internal representation of `f64`.
14 #[stable(feature = "rust1", since = "1.0.0")]
15 pub const RADIX: u32 = 2;
17 /// Number of significant digits in base 2.
18 #[stable(feature = "rust1", since = "1.0.0")]
19 pub const MANTISSA_DIGITS: u32 = 53;
20 /// Approximate number of significant digits in base 10.
21 #[stable(feature = "rust1", since = "1.0.0")]
22 pub const DIGITS: u32 = 15;
24 /// [Machine epsilon] value for `f64`.
26 /// This is the difference between `1.0` and the next largest representable number.
28 /// [Machine epsilon]: https://en.wikipedia.org/wiki/Machine_epsilon
29 #[stable(feature = "rust1", since = "1.0.0")]
30 pub const EPSILON: f64 = 2.2204460492503131e-16_f64;
32 /// Smallest finite `f64` value.
33 #[stable(feature = "rust1", since = "1.0.0")]
34 pub const MIN: f64 = -1.7976931348623157e+308_f64;
35 /// Smallest positive normal `f64` value.
36 #[stable(feature = "rust1", since = "1.0.0")]
37 pub const MIN_POSITIVE: f64 = 2.2250738585072014e-308_f64;
38 /// Largest finite `f64` value.
39 #[stable(feature = "rust1", since = "1.0.0")]
40 pub const MAX: f64 = 1.7976931348623157e+308_f64;
42 /// One greater than the minimum possible normal power of 2 exponent.
43 #[stable(feature = "rust1", since = "1.0.0")]
44 pub const MIN_EXP: i32 = -1021;
45 /// Maximum possible power of 2 exponent.
46 #[stable(feature = "rust1", since = "1.0.0")]
47 pub const MAX_EXP: i32 = 1024;
49 /// Minimum possible normal power of 10 exponent.
50 #[stable(feature = "rust1", since = "1.0.0")]
51 pub const MIN_10_EXP: i32 = -307;
52 /// Maximum possible power of 10 exponent.
53 #[stable(feature = "rust1", since = "1.0.0")]
54 pub const MAX_10_EXP: i32 = 308;
56 /// Not a Number (NaN).
57 #[stable(feature = "rust1", since = "1.0.0")]
58 pub const NAN: f64 = 0.0_f64 / 0.0_f64;
60 #[stable(feature = "rust1", since = "1.0.0")]
61 pub const INFINITY: f64 = 1.0_f64 / 0.0_f64;
62 /// Negative infinity (-∞).
63 #[stable(feature = "rust1", since = "1.0.0")]
64 pub const NEG_INFINITY: f64 = -1.0_f64 / 0.0_f64;
66 /// Basic mathematical constants.
67 #[stable(feature = "rust1", since = "1.0.0")]
69 // FIXME: replace with mathematical constants from cmath.
71 /// Archimedes' constant (π)
72 #[stable(feature = "rust1", since = "1.0.0")]
73 pub const PI: f64 = 3.14159265358979323846264338327950288_f64;
76 #[stable(feature = "rust1", since = "1.0.0")]
77 pub const FRAC_PI_2: f64 = 1.57079632679489661923132169163975144_f64;
80 #[stable(feature = "rust1", since = "1.0.0")]
81 pub const FRAC_PI_3: f64 = 1.04719755119659774615421446109316763_f64;
84 #[stable(feature = "rust1", since = "1.0.0")]
85 pub const FRAC_PI_4: f64 = 0.785398163397448309615660845819875721_f64;
88 #[stable(feature = "rust1", since = "1.0.0")]
89 pub const FRAC_PI_6: f64 = 0.52359877559829887307710723054658381_f64;
92 #[stable(feature = "rust1", since = "1.0.0")]
93 pub const FRAC_PI_8: f64 = 0.39269908169872415480783042290993786_f64;
96 #[stable(feature = "rust1", since = "1.0.0")]
97 pub const FRAC_1_PI: f64 = 0.318309886183790671537767526745028724_f64;
100 #[stable(feature = "rust1", since = "1.0.0")]
101 pub const FRAC_2_PI: f64 = 0.636619772367581343075535053490057448_f64;
104 #[stable(feature = "rust1", since = "1.0.0")]
105 pub const FRAC_2_SQRT_PI: f64 = 1.12837916709551257389615890312154517_f64;
108 #[stable(feature = "rust1", since = "1.0.0")]
109 pub const SQRT_2: f64 = 1.41421356237309504880168872420969808_f64;
112 #[stable(feature = "rust1", since = "1.0.0")]
113 pub const FRAC_1_SQRT_2: f64 = 0.707106781186547524400844362104849039_f64;
115 /// Euler's number (e)
116 #[stable(feature = "rust1", since = "1.0.0")]
117 pub const E: f64 = 2.71828182845904523536028747135266250_f64;
119 /// log<sub>2</sub>(10)
120 #[unstable(feature = "extra_log_consts", issue = "50540")]
121 pub const LOG2_10: f64 = 3.32192809488736234787031942948939018_f64;
123 /// log<sub>2</sub>(e)
124 #[stable(feature = "rust1", since = "1.0.0")]
125 pub const LOG2_E: f64 = 1.44269504088896340735992468100189214_f64;
127 /// log<sub>10</sub>(2)
128 #[unstable(feature = "extra_log_consts", issue = "50540")]
129 pub const LOG10_2: f64 = 0.301029995663981195213738894724493027_f64;
131 /// log<sub>10</sub>(e)
132 #[stable(feature = "rust1", since = "1.0.0")]
133 pub const LOG10_E: f64 = 0.434294481903251827651128918916605082_f64;
136 #[stable(feature = "rust1", since = "1.0.0")]
137 pub const LN_2: f64 = 0.693147180559945309417232121458176568_f64;
140 #[stable(feature = "rust1", since = "1.0.0")]
141 pub const LN_10: f64 = 2.30258509299404568401799145468436421_f64;
147 /// Returns `true` if this value is `NaN`.
152 /// let nan = f64::NAN;
155 /// assert!(nan.is_nan());
156 /// assert!(!f.is_nan());
158 #[stable(feature = "rust1", since = "1.0.0")]
160 pub fn is_nan(self) -> bool {
164 // FIXME(#50145): `abs` is publicly unavailable in libcore due to
165 // concerns about portability, so this implementation is for
166 // private use internally.
168 fn abs_private(self) -> f64 {
169 f64::from_bits(self.to_bits() & 0x7fff_ffff_ffff_ffff)
172 /// Returns `true` if this value is positive infinity or negative infinity, and
173 /// `false` otherwise.
179 /// let inf = f64::INFINITY;
180 /// let neg_inf = f64::NEG_INFINITY;
181 /// let nan = f64::NAN;
183 /// assert!(!f.is_infinite());
184 /// assert!(!nan.is_infinite());
186 /// assert!(inf.is_infinite());
187 /// assert!(neg_inf.is_infinite());
189 #[stable(feature = "rust1", since = "1.0.0")]
191 pub fn is_infinite(self) -> bool {
192 self.abs_private() == INFINITY
195 /// Returns `true` if this number is neither infinite nor `NaN`.
201 /// let inf: f64 = f64::INFINITY;
202 /// let neg_inf: f64 = f64::NEG_INFINITY;
203 /// let nan: f64 = f64::NAN;
205 /// assert!(f.is_finite());
207 /// assert!(!nan.is_finite());
208 /// assert!(!inf.is_finite());
209 /// assert!(!neg_inf.is_finite());
211 #[stable(feature = "rust1", since = "1.0.0")]
213 pub fn is_finite(self) -> bool {
214 // There's no need to handle NaN separately: if self is NaN,
215 // the comparison is not true, exactly as desired.
216 self.abs_private() < INFINITY
219 /// Returns `true` if the number is neither zero, infinite,
220 /// [subnormal][subnormal], or `NaN`.
225 /// let min = f64::MIN_POSITIVE; // 2.2250738585072014e-308f64
226 /// let max = f64::MAX;
227 /// let lower_than_min = 1.0e-308_f64;
228 /// let zero = 0.0f64;
230 /// assert!(min.is_normal());
231 /// assert!(max.is_normal());
233 /// assert!(!zero.is_normal());
234 /// assert!(!f64::NAN.is_normal());
235 /// assert!(!f64::INFINITY.is_normal());
236 /// // Values between `0` and `min` are Subnormal.
237 /// assert!(!lower_than_min.is_normal());
239 /// [subnormal]: https://en.wikipedia.org/wiki/Denormal_number
240 #[stable(feature = "rust1", since = "1.0.0")]
242 pub fn is_normal(self) -> bool {
243 self.classify() == FpCategory::Normal
246 /// Returns the floating point category of the number. If only one property
247 /// is going to be tested, it is generally faster to use the specific
248 /// predicate instead.
251 /// use std::num::FpCategory;
254 /// let num = 12.4_f64;
255 /// let inf = f64::INFINITY;
257 /// assert_eq!(num.classify(), FpCategory::Normal);
258 /// assert_eq!(inf.classify(), FpCategory::Infinite);
260 #[stable(feature = "rust1", since = "1.0.0")]
261 pub fn classify(self) -> FpCategory {
262 const EXP_MASK: u64 = 0x7ff0000000000000;
263 const MAN_MASK: u64 = 0x000fffffffffffff;
265 let bits = self.to_bits();
266 match (bits & MAN_MASK, bits & EXP_MASK) {
267 (0, 0) => FpCategory::Zero,
268 (_, 0) => FpCategory::Subnormal,
269 (0, EXP_MASK) => FpCategory::Infinite,
270 (_, EXP_MASK) => FpCategory::Nan,
271 _ => FpCategory::Normal,
275 /// Returns `true` if `self` has a positive sign, including `+0.0`, `NaN`s with
276 /// positive sign bit and positive infinity.
280 /// let g = -7.0_f64;
282 /// assert!(f.is_sign_positive());
283 /// assert!(!g.is_sign_positive());
285 #[stable(feature = "rust1", since = "1.0.0")]
287 pub fn is_sign_positive(self) -> bool {
288 !self.is_sign_negative()
291 #[stable(feature = "rust1", since = "1.0.0")]
292 #[rustc_deprecated(since = "1.0.0", reason = "renamed to is_sign_positive")]
295 pub fn is_positive(self) -> bool {
296 self.is_sign_positive()
299 /// Returns `true` if `self` has a negative sign, including `-0.0`, `NaN`s with
300 /// negative sign bit and negative infinity.
304 /// let g = -7.0_f64;
306 /// assert!(!f.is_sign_negative());
307 /// assert!(g.is_sign_negative());
309 #[stable(feature = "rust1", since = "1.0.0")]
311 pub fn is_sign_negative(self) -> bool {
312 self.to_bits() & 0x8000_0000_0000_0000 != 0
315 #[stable(feature = "rust1", since = "1.0.0")]
316 #[rustc_deprecated(since = "1.0.0", reason = "renamed to is_sign_negative")]
319 pub fn is_negative(self) -> bool {
320 self.is_sign_negative()
323 /// Takes the reciprocal (inverse) of a number, `1/x`.
327 /// let abs_difference = (x.recip() - (1.0/x)).abs();
329 /// assert!(abs_difference < 1e-10);
331 #[stable(feature = "rust1", since = "1.0.0")]
333 pub fn recip(self) -> f64 {
337 /// Converts radians to degrees.
340 /// use std::f64::consts;
342 /// let angle = consts::PI;
344 /// let abs_difference = (angle.to_degrees() - 180.0).abs();
346 /// assert!(abs_difference < 1e-10);
348 #[stable(feature = "rust1", since = "1.0.0")]
350 pub fn to_degrees(self) -> f64 {
351 // The division here is correctly rounded with respect to the true
352 // value of 180/π. (This differs from f32, where a constant must be
353 // used to ensure a correctly rounded result.)
354 self * (180.0f64 / consts::PI)
357 /// Converts degrees to radians.
360 /// use std::f64::consts;
362 /// let angle = 180.0_f64;
364 /// let abs_difference = (angle.to_radians() - consts::PI).abs();
366 /// assert!(abs_difference < 1e-10);
368 #[stable(feature = "rust1", since = "1.0.0")]
370 pub fn to_radians(self) -> f64 {
371 let value: f64 = consts::PI;
372 self * (value / 180.0)
375 /// Returns the maximum of the two numbers.
381 /// assert_eq!(x.max(y), y);
384 /// If one of the arguments is NaN, then the other argument is returned.
385 #[stable(feature = "rust1", since = "1.0.0")]
387 pub fn max(self, other: f64) -> f64 {
388 // IEEE754 says: maxNum(x, y) is the canonicalized number y if x < y, x if y < x, the
389 // canonicalized number if one operand is a number and the other a quiet NaN. Otherwise it
390 // is either x or y, canonicalized (this means results might differ among implementations).
391 // When either x or y is a signalingNaN, then the result is according to 6.2.
393 // Since we do not support sNaN in Rust yet, we do not need to handle them.
394 // FIXME(nagisa): due to https://bugs.llvm.org/show_bug.cgi?id=33303 we canonicalize by
395 // multiplying by 1.0. Should switch to the `canonicalize` when it works.
396 (if self.is_nan() || self < other { other } else { self }) * 1.0
399 /// Returns the minimum of the two numbers.
405 /// assert_eq!(x.min(y), x);
408 /// If one of the arguments is NaN, then the other argument is returned.
409 #[stable(feature = "rust1", since = "1.0.0")]
411 pub fn min(self, other: f64) -> f64 {
412 // IEEE754 says: minNum(x, y) is the canonicalized number x if x < y, y if y < x, the
413 // canonicalized number if one operand is a number and the other a quiet NaN. Otherwise it
414 // is either x or y, canonicalized (this means results might differ among implementations).
415 // When either x or y is a signalingNaN, then the result is according to 6.2.
417 // Since we do not support sNaN in Rust yet, we do not need to handle them.
418 // FIXME(nagisa): due to https://bugs.llvm.org/show_bug.cgi?id=33303 we canonicalize by
419 // multiplying by 1.0. Should switch to the `canonicalize` when it works.
420 (if other.is_nan() || self < other { self } else { other }) * 1.0
423 /// Raw transmutation to `u64`.
425 /// This is currently identical to `transmute::<f64, u64>(self)` on all platforms.
427 /// See `from_bits` for some discussion of the portability of this operation
428 /// (there are almost no issues).
430 /// Note that this function is distinct from `as` casting, which attempts to
431 /// preserve the *numeric* value, and not the bitwise value.
436 /// assert!((1f64).to_bits() != 1f64 as u64); // to_bits() is not casting!
437 /// assert_eq!((12.5f64).to_bits(), 0x4029000000000000);
440 #[stable(feature = "float_bits_conv", since = "1.20.0")]
442 pub fn to_bits(self) -> u64 {
443 unsafe { mem::transmute(self) }
446 /// Raw transmutation from `u64`.
448 /// This is currently identical to `transmute::<u64, f64>(v)` on all platforms.
449 /// It turns out this is incredibly portable, for two reasons:
451 /// * Floats and Ints have the same endianness on all supported platforms.
452 /// * IEEE-754 very precisely specifies the bit layout of floats.
454 /// However there is one caveat: prior to the 2008 version of IEEE-754, how
455 /// to interpret the NaN signaling bit wasn't actually specified. Most platforms
456 /// (notably x86 and ARM) picked the interpretation that was ultimately
457 /// standardized in 2008, but some didn't (notably MIPS). As a result, all
458 /// signaling NaNs on MIPS are quiet NaNs on x86, and vice-versa.
460 /// Rather than trying to preserve signaling-ness cross-platform, this
461 /// implementation favours preserving the exact bits. This means that
462 /// any payloads encoded in NaNs will be preserved even if the result of
463 /// this method is sent over the network from an x86 machine to a MIPS one.
465 /// If the results of this method are only manipulated by the same
466 /// architecture that produced them, then there is no portability concern.
468 /// If the input isn't NaN, then there is no portability concern.
470 /// If you don't care about signalingness (very likely), then there is no
471 /// portability concern.
473 /// Note that this function is distinct from `as` casting, which attempts to
474 /// preserve the *numeric* value, and not the bitwise value.
480 /// let v = f64::from_bits(0x4029000000000000);
481 /// let difference = (v - 12.5).abs();
482 /// assert!(difference <= 1e-5);
484 #[stable(feature = "float_bits_conv", since = "1.20.0")]
486 pub fn from_bits(v: u64) -> Self {
487 // It turns out the safety issues with sNaN were overblown! Hooray!
488 unsafe { mem::transmute(v) }