1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! Operations and constants for 64-bits floats (`f64` type)
13 #![doc(primitive = "f64")]
14 // FIXME: MIN_VALUE and MAX_VALUE literals are parsed as -inf and inf #14353
15 #![allow(overflowing_literals)]
17 #![stable(feature = "rust1", since = "1.0.0")]
23 use num::FpCategory as Fp;
24 use num::{Float, ParseFloatError};
26 #[stable(feature = "rust1", since = "1.0.0")]
27 pub const RADIX: u32 = 2;
29 #[stable(feature = "rust1", since = "1.0.0")]
30 pub const MANTISSA_DIGITS: u32 = 53;
31 #[stable(feature = "rust1", since = "1.0.0")]
32 pub const DIGITS: u32 = 15;
34 #[stable(feature = "rust1", since = "1.0.0")]
35 pub const EPSILON: f64 = 2.2204460492503131e-16_f64;
37 /// Smallest finite f64 value
38 #[stable(feature = "rust1", since = "1.0.0")]
39 pub const MIN: f64 = -1.7976931348623157e+308_f64;
40 /// Smallest positive, normalized f64 value
41 #[stable(feature = "rust1", since = "1.0.0")]
42 pub const MIN_POSITIVE: f64 = 2.2250738585072014e-308_f64;
43 /// Largest finite f64 value
44 #[stable(feature = "rust1", since = "1.0.0")]
45 pub const MAX: f64 = 1.7976931348623157e+308_f64;
47 #[stable(feature = "rust1", since = "1.0.0")]
48 pub const MIN_EXP: i32 = -1021;
49 #[stable(feature = "rust1", since = "1.0.0")]
50 pub const MAX_EXP: i32 = 1024;
52 #[stable(feature = "rust1", since = "1.0.0")]
53 pub const MIN_10_EXP: i32 = -307;
54 #[stable(feature = "rust1", since = "1.0.0")]
55 pub const MAX_10_EXP: i32 = 308;
57 #[stable(feature = "rust1", since = "1.0.0")]
58 pub const NAN: f64 = 0.0_f64/0.0_f64;
59 #[stable(feature = "rust1", since = "1.0.0")]
60 pub const INFINITY: f64 = 1.0_f64/0.0_f64;
61 #[stable(feature = "rust1", since = "1.0.0")]
62 pub const NEG_INFINITY: f64 = -1.0_f64/0.0_f64;
64 /// Basic mathematial constants.
65 #[stable(feature = "rust1", since = "1.0.0")]
67 // FIXME: replace with mathematical constants from cmath.
69 /// Archimedes' constant
70 #[stable(feature = "rust1", since = "1.0.0")]
71 pub const PI: f64 = 3.14159265358979323846264338327950288_f64;
74 #[unstable(feature = "float_consts",
75 reason = "unclear naming convention/usefulness")]
76 pub const PI_2: f64 = 6.28318530717958647692528676655900576_f64;
79 #[stable(feature = "rust1", since = "1.0.0")]
80 pub const FRAC_PI_2: f64 = 1.57079632679489661923132169163975144_f64;
83 #[stable(feature = "rust1", since = "1.0.0")]
84 pub const FRAC_PI_3: f64 = 1.04719755119659774615421446109316763_f64;
87 #[stable(feature = "rust1", since = "1.0.0")]
88 pub const FRAC_PI_4: f64 = 0.785398163397448309615660845819875721_f64;
91 #[stable(feature = "rust1", since = "1.0.0")]
92 pub const FRAC_PI_6: f64 = 0.52359877559829887307710723054658381_f64;
95 #[stable(feature = "rust1", since = "1.0.0")]
96 pub const FRAC_PI_8: f64 = 0.39269908169872415480783042290993786_f64;
99 #[stable(feature = "rust1", since = "1.0.0")]
100 pub const FRAC_1_PI: f64 = 0.318309886183790671537767526745028724_f64;
103 #[stable(feature = "rust1", since = "1.0.0")]
104 pub const FRAC_2_PI: f64 = 0.636619772367581343075535053490057448_f64;
107 #[stable(feature = "rust1", since = "1.0.0")]
108 pub const FRAC_2_SQRT_PI: f64 = 1.12837916709551257389615890312154517_f64;
111 #[stable(feature = "rust1", since = "1.0.0")]
112 pub const SQRT_2: f64 = 1.41421356237309504880168872420969808_f64;
115 #[stable(feature = "rust1", since = "1.0.0")]
116 pub const FRAC_1_SQRT_2: f64 = 0.707106781186547524400844362104849039_f64;
119 #[stable(feature = "rust1", since = "1.0.0")]
120 pub const E: f64 = 2.71828182845904523536028747135266250_f64;
123 #[stable(feature = "rust1", since = "1.0.0")]
124 pub const LOG2_E: f64 = 1.44269504088896340735992468100189214_f64;
127 #[stable(feature = "rust1", since = "1.0.0")]
128 pub const LOG10_E: f64 = 0.434294481903251827651128918916605082_f64;
131 #[stable(feature = "rust1", since = "1.0.0")]
132 pub const LN_2: f64 = 0.693147180559945309417232121458176568_f64;
135 #[stable(feature = "rust1", since = "1.0.0")]
136 pub const LN_10: f64 = 2.30258509299404568401799145468436421_f64;
141 fn nan() -> f64 { NAN }
144 fn infinity() -> f64 { INFINITY }
147 fn neg_infinity() -> f64 { NEG_INFINITY }
150 fn zero() -> f64 { 0.0 }
153 fn neg_zero() -> f64 { -0.0 }
156 fn one() -> f64 { 1.0 }
158 from_str_radix_float_impl! { f64 }
160 /// Returns `true` if the number is NaN.
162 fn is_nan(self) -> bool { self != self }
164 /// Returns `true` if the number is infinite.
166 fn is_infinite(self) -> bool {
167 self == Float::infinity() || self == Float::neg_infinity()
170 /// Returns `true` if the number is neither infinite or NaN.
172 fn is_finite(self) -> bool {
173 !(self.is_nan() || self.is_infinite())
176 /// Returns `true` if the number is neither zero, infinite, subnormal or NaN.
178 fn is_normal(self) -> bool {
179 self.classify() == Fp::Normal
182 /// Returns the floating point category of the number. If only one property
183 /// is going to be tested, it is generally faster to use the specific
184 /// predicate instead.
185 fn classify(self) -> Fp {
186 const EXP_MASK: u64 = 0x7ff0000000000000;
187 const MAN_MASK: u64 = 0x000fffffffffffff;
189 let bits: u64 = unsafe { mem::transmute(self) };
190 match (bits & MAN_MASK, bits & EXP_MASK) {
192 (_, 0) => Fp::Subnormal,
193 (0, EXP_MASK) => Fp::Infinite,
194 (_, EXP_MASK) => Fp::Nan,
199 /// Returns the mantissa, exponent and sign as integers.
200 fn integer_decode(self) -> (u64, i16, i8) {
201 let bits: u64 = unsafe { mem::transmute(self) };
202 let sign: i8 = if bits >> 63 == 0 { 1 } else { -1 };
203 let mut exponent: i16 = ((bits >> 52) & 0x7ff) as i16;
204 let mantissa = if exponent == 0 {
205 (bits & 0xfffffffffffff) << 1
207 (bits & 0xfffffffffffff) | 0x10000000000000
209 // Exponent bias + mantissa shift
210 exponent -= 1023 + 52;
211 (mantissa, exponent, sign)
214 /// Rounds towards minus infinity.
216 fn floor(self) -> f64 {
217 unsafe { intrinsics::floorf64(self) }
220 /// Rounds towards plus infinity.
222 fn ceil(self) -> f64 {
223 unsafe { intrinsics::ceilf64(self) }
226 /// Rounds to nearest integer. Rounds half-way cases away from zero.
228 fn round(self) -> f64 {
229 unsafe { intrinsics::roundf64(self) }
232 /// Returns the integer part of the number (rounds towards zero).
234 fn trunc(self) -> f64 {
235 unsafe { intrinsics::truncf64(self) }
238 /// The fractional part of the number, satisfying:
242 /// assert!(x == x.trunc() + x.fract())
245 fn fract(self) -> f64 { self - self.trunc() }
247 /// Computes the absolute value of `self`. Returns `Float::nan()` if the
248 /// number is `Float::nan()`.
250 fn abs(self) -> f64 {
251 unsafe { intrinsics::fabsf64(self) }
254 /// Returns a number that represents the sign of `self`.
256 /// - `1.0` if the number is positive, `+0.0` or `Float::infinity()`
257 /// - `-1.0` if the number is negative, `-0.0` or `Float::neg_infinity()`
258 /// - `Float::nan()` if the number is `Float::nan()`
260 fn signum(self) -> f64 {
264 unsafe { intrinsics::copysignf64(1.0, self) }
268 /// Returns `true` if `self` is positive, including `+0.0` and
269 /// `Float::infinity()`.
271 fn is_positive(self) -> bool {
272 self > 0.0 || (1.0 / self) == Float::infinity()
275 /// Returns `true` if `self` is negative, including `-0.0` and
276 /// `Float::neg_infinity()`.
278 fn is_negative(self) -> bool {
279 self < 0.0 || (1.0 / self) == Float::neg_infinity()
282 /// Fused multiply-add. Computes `(self * a) + b` with only one rounding
283 /// error. This produces a more accurate result with better performance than
284 /// a separate multiplication operation followed by an add.
286 fn mul_add(self, a: f64, b: f64) -> f64 {
287 unsafe { intrinsics::fmaf64(self, a, b) }
290 /// Returns the reciprocal (multiplicative inverse) of the number.
292 fn recip(self) -> f64 { 1.0 / self }
295 fn powf(self, n: f64) -> f64 {
296 unsafe { intrinsics::powf64(self, n) }
300 fn powi(self, n: i32) -> f64 {
301 unsafe { intrinsics::powif64(self, n) }
305 fn sqrt(self) -> f64 {
309 unsafe { intrinsics::sqrtf64(self) }
314 fn rsqrt(self) -> f64 { self.sqrt().recip() }
316 /// Returns the exponential of the number.
318 fn exp(self) -> f64 {
319 unsafe { intrinsics::expf64(self) }
322 /// Returns 2 raised to the power of the number.
324 fn exp2(self) -> f64 {
325 unsafe { intrinsics::exp2f64(self) }
328 /// Returns the natural logarithm of the number.
331 unsafe { intrinsics::logf64(self) }
334 /// Returns the logarithm of the number with respect to an arbitrary base.
336 fn log(self, base: f64) -> f64 { self.ln() / base.ln() }
338 /// Returns the base 2 logarithm of the number.
340 fn log2(self) -> f64 {
341 unsafe { intrinsics::log2f64(self) }
344 /// Returns the base 10 logarithm of the number.
346 fn log10(self) -> f64 {
347 unsafe { intrinsics::log10f64(self) }
350 /// Converts to degrees, assuming the number is in radians.
352 fn to_degrees(self) -> f64 { self * (180.0f64 / consts::PI) }
354 /// Converts to radians, assuming the number is in degrees.
356 fn to_radians(self) -> f64 {
357 let value: f64 = consts::PI;
358 self * (value / 180.0)