]>
Commit | Line | Data |
---|---|---|
c34b1796 | 1 | // Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT |
970d7e83 LB |
2 | // file at the top-level directory of this distribution and at |
3 | // http://rust-lang.org/COPYRIGHT. | |
4 | // | |
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. | |
10 | ||
c1a9b12d SL |
11 | //! The 64-bit floating point type. |
12 | //! | |
13 | //! *[See also the `f64` primitive type](../primitive.f64.html).* | |
970d7e83 | 14 | |
85aaf69f | 15 | #![stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc | 16 | #![allow(missing_docs)] |
970d7e83 | 17 | |
9cc50fc6 | 18 | #[cfg(not(test))] |
9346a6ac | 19 | use core::num; |
9cc50fc6 | 20 | #[cfg(not(test))] |
1a4d82fc | 21 | use intrinsics; |
9cc50fc6 | 22 | #[cfg(not(test))] |
1a4d82fc | 23 | use libc::c_int; |
9cc50fc6 SL |
24 | #[cfg(not(test))] |
25 | use num::FpCategory; | |
1a4d82fc | 26 | |
92a42be0 | 27 | #[stable(feature = "rust1", since = "1.0.0")] |
9346a6ac | 28 | pub use core::f64::{RADIX, MANTISSA_DIGITS, DIGITS, EPSILON}; |
92a42be0 | 29 | #[stable(feature = "rust1", since = "1.0.0")] |
9346a6ac | 30 | pub use core::f64::{MIN_EXP, MAX_EXP, MIN_10_EXP}; |
92a42be0 | 31 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc | 32 | pub use core::f64::{MAX_10_EXP, NAN, INFINITY, NEG_INFINITY}; |
92a42be0 | 33 | #[stable(feature = "rust1", since = "1.0.0")] |
85aaf69f | 34 | pub use core::f64::{MIN, MIN_POSITIVE, MAX}; |
92a42be0 | 35 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
36 | pub use core::f64::consts; |
37 | ||
38 | #[allow(dead_code)] | |
39 | mod cmath { | |
40 | use libc::{c_double, c_int}; | |
41 | ||
42 | #[link_name = "m"] | |
43 | extern { | |
44 | pub fn acos(n: c_double) -> c_double; | |
45 | pub fn asin(n: c_double) -> c_double; | |
46 | pub fn atan(n: c_double) -> c_double; | |
47 | pub fn atan2(a: c_double, b: c_double) -> c_double; | |
48 | pub fn cbrt(n: c_double) -> c_double; | |
49 | pub fn cosh(n: c_double) -> c_double; | |
50 | pub fn erf(n: c_double) -> c_double; | |
51 | pub fn erfc(n: c_double) -> c_double; | |
52 | pub fn expm1(n: c_double) -> c_double; | |
53 | pub fn fdim(a: c_double, b: c_double) -> c_double; | |
54 | pub fn fmax(a: c_double, b: c_double) -> c_double; | |
55 | pub fn fmin(a: c_double, b: c_double) -> c_double; | |
56 | pub fn fmod(a: c_double, b: c_double) -> c_double; | |
1a4d82fc | 57 | pub fn frexp(n: c_double, value: &mut c_int) -> c_double; |
e9174d1e | 58 | pub fn ilogb(n: c_double) -> c_int; |
1a4d82fc JJ |
59 | pub fn ldexp(x: c_double, n: c_int) -> c_double; |
60 | pub fn logb(n: c_double) -> c_double; | |
61 | pub fn log1p(n: c_double) -> c_double; | |
e9174d1e | 62 | pub fn nextafter(x: c_double, y: c_double) -> c_double; |
1a4d82fc JJ |
63 | pub fn modf(n: c_double, iptr: &mut c_double) -> c_double; |
64 | pub fn sinh(n: c_double) -> c_double; | |
65 | pub fn tan(n: c_double) -> c_double; | |
66 | pub fn tanh(n: c_double) -> c_double; | |
67 | pub fn tgamma(n: c_double) -> c_double; | |
68 | ||
69 | // These are commonly only available for doubles | |
70 | ||
71 | pub fn j0(n: c_double) -> c_double; | |
72 | pub fn j1(n: c_double) -> c_double; | |
73 | pub fn jn(i: c_int, n: c_double) -> c_double; | |
74 | ||
75 | pub fn y0(n: c_double) -> c_double; | |
76 | pub fn y1(n: c_double) -> c_double; | |
77 | pub fn yn(i: c_int, n: c_double) -> c_double; | |
78 | ||
62682a34 | 79 | #[cfg_attr(all(windows, target_env = "msvc"), link_name = "__lgamma_r")] |
1a4d82fc | 80 | pub fn lgamma_r(n: c_double, sign: &mut c_int) -> c_double; |
62682a34 SL |
81 | |
82 | #[cfg_attr(all(windows, target_env = "msvc"), link_name = "_hypot")] | |
83 | pub fn hypot(x: c_double, y: c_double) -> c_double; | |
1a4d82fc JJ |
84 | } |
85 | } | |
86 | ||
c34b1796 AL |
87 | #[cfg(not(test))] |
88 | #[lang = "f64"] | |
c34b1796 AL |
89 | impl f64 { |
90 | /// Returns `true` if this value is `NaN` and false otherwise. | |
91 | /// | |
92 | /// ``` | |
93 | /// use std::f64; | |
94 | /// | |
95 | /// let nan = f64::NAN; | |
96 | /// let f = 7.0_f64; | |
97 | /// | |
98 | /// assert!(nan.is_nan()); | |
99 | /// assert!(!f.is_nan()); | |
100 | /// ``` | |
101 | #[stable(feature = "rust1", since = "1.0.0")] | |
102 | #[inline] | |
103 | pub fn is_nan(self) -> bool { num::Float::is_nan(self) } | |
970d7e83 | 104 | |
c34b1796 AL |
105 | /// Returns `true` if this value is positive infinity or negative infinity and |
106 | /// false otherwise. | |
107 | /// | |
108 | /// ``` | |
109 | /// use std::f64; | |
110 | /// | |
111 | /// let f = 7.0f64; | |
112 | /// let inf = f64::INFINITY; | |
113 | /// let neg_inf = f64::NEG_INFINITY; | |
114 | /// let nan = f64::NAN; | |
115 | /// | |
116 | /// assert!(!f.is_infinite()); | |
117 | /// assert!(!nan.is_infinite()); | |
118 | /// | |
119 | /// assert!(inf.is_infinite()); | |
120 | /// assert!(neg_inf.is_infinite()); | |
121 | /// ``` | |
122 | #[stable(feature = "rust1", since = "1.0.0")] | |
123 | #[inline] | |
124 | pub fn is_infinite(self) -> bool { num::Float::is_infinite(self) } | |
970d7e83 | 125 | |
c34b1796 AL |
126 | /// Returns `true` if this number is neither infinite nor `NaN`. |
127 | /// | |
128 | /// ``` | |
129 | /// use std::f64; | |
130 | /// | |
131 | /// let f = 7.0f64; | |
132 | /// let inf: f64 = f64::INFINITY; | |
133 | /// let neg_inf: f64 = f64::NEG_INFINITY; | |
134 | /// let nan: f64 = f64::NAN; | |
135 | /// | |
136 | /// assert!(f.is_finite()); | |
137 | /// | |
138 | /// assert!(!nan.is_finite()); | |
139 | /// assert!(!inf.is_finite()); | |
140 | /// assert!(!neg_inf.is_finite()); | |
141 | /// ``` | |
142 | #[stable(feature = "rust1", since = "1.0.0")] | |
143 | #[inline] | |
144 | pub fn is_finite(self) -> bool { num::Float::is_finite(self) } | |
970d7e83 | 145 | |
c34b1796 AL |
146 | /// Returns `true` if the number is neither zero, infinite, |
147 | /// [subnormal][subnormal], or `NaN`. | |
148 | /// | |
149 | /// ``` | |
3157f602 | 150 | /// use std::f64; |
c34b1796 | 151 | /// |
3157f602 XL |
152 | /// let min = f64::MIN_POSITIVE; // 2.2250738585072014e-308f64 |
153 | /// let max = f64::MAX; | |
154 | /// let lower_than_min = 1.0e-308_f64; | |
155 | /// let zero = 0.0f64; | |
c34b1796 AL |
156 | /// |
157 | /// assert!(min.is_normal()); | |
158 | /// assert!(max.is_normal()); | |
159 | /// | |
160 | /// assert!(!zero.is_normal()); | |
3157f602 XL |
161 | /// assert!(!f64::NAN.is_normal()); |
162 | /// assert!(!f64::INFINITY.is_normal()); | |
c34b1796 AL |
163 | /// // Values between `0` and `min` are Subnormal. |
164 | /// assert!(!lower_than_min.is_normal()); | |
165 | /// ``` | |
3157f602 | 166 | /// [subnormal]: https://en.wikipedia.org/wiki/Denormal_number |
c34b1796 AL |
167 | #[stable(feature = "rust1", since = "1.0.0")] |
168 | #[inline] | |
169 | pub fn is_normal(self) -> bool { num::Float::is_normal(self) } | |
170 | ||
171 | /// Returns the floating point category of the number. If only one property | |
172 | /// is going to be tested, it is generally faster to use the specific | |
173 | /// predicate instead. | |
174 | /// | |
175 | /// ``` | |
176 | /// use std::num::FpCategory; | |
177 | /// use std::f64; | |
178 | /// | |
179 | /// let num = 12.4_f64; | |
180 | /// let inf = f64::INFINITY; | |
181 | /// | |
182 | /// assert_eq!(num.classify(), FpCategory::Normal); | |
183 | /// assert_eq!(inf.classify(), FpCategory::Infinite); | |
184 | /// ``` | |
185 | #[stable(feature = "rust1", since = "1.0.0")] | |
186 | #[inline] | |
187 | pub fn classify(self) -> FpCategory { num::Float::classify(self) } | |
970d7e83 | 188 | |
c34b1796 AL |
189 | /// Returns the mantissa, base 2 exponent, and sign as integers, respectively. |
190 | /// The original number can be recovered by `sign * mantissa * 2 ^ exponent`. | |
191 | /// The floating point encoding is documented in the [Reference][floating-point]. | |
192 | /// | |
193 | /// ``` | |
c1a9b12d SL |
194 | /// #![feature(float_extras)] |
195 | /// | |
c34b1796 AL |
196 | /// let num = 2.0f64; |
197 | /// | |
198 | /// // (8388608, -22, 1) | |
199 | /// let (mantissa, exponent, sign) = num.integer_decode(); | |
200 | /// let sign_f = sign as f64; | |
201 | /// let mantissa_f = mantissa as f64; | |
202 | /// let exponent_f = num.powf(exponent as f64); | |
203 | /// | |
204 | /// // 1 * 8388608 * 2^(-22) == 2 | |
205 | /// let abs_difference = (sign_f * mantissa_f * exponent_f - num).abs(); | |
206 | /// | |
207 | /// assert!(abs_difference < 1e-10); | |
208 | /// ``` | |
8bb4bdeb | 209 | /// [floating-point]: ../reference/types.html#machine-types |
e9174d1e SL |
210 | #[unstable(feature = "float_extras", reason = "signature is undecided", |
211 | issue = "27752")] | |
3157f602 XL |
212 | #[rustc_deprecated(since = "1.11.0", |
213 | reason = "never really came to fruition and easily \ | |
214 | implementable outside the standard library")] | |
c34b1796 | 215 | #[inline] |
3157f602 | 216 | #[allow(deprecated)] |
c34b1796 | 217 | pub fn integer_decode(self) -> (u64, i16, i8) { num::Float::integer_decode(self) } |
970d7e83 | 218 | |
c34b1796 AL |
219 | /// Returns the largest integer less than or equal to a number. |
220 | /// | |
221 | /// ``` | |
222 | /// let f = 3.99_f64; | |
223 | /// let g = 3.0_f64; | |
224 | /// | |
225 | /// assert_eq!(f.floor(), 3.0); | |
226 | /// assert_eq!(g.floor(), 3.0); | |
227 | /// ``` | |
228 | #[stable(feature = "rust1", since = "1.0.0")] | |
229 | #[inline] | |
e9174d1e SL |
230 | pub fn floor(self) -> f64 { |
231 | unsafe { intrinsics::floorf64(self) } | |
232 | } | |
970d7e83 | 233 | |
c34b1796 AL |
234 | /// Returns the smallest integer greater than or equal to a number. |
235 | /// | |
236 | /// ``` | |
237 | /// let f = 3.01_f64; | |
238 | /// let g = 4.0_f64; | |
239 | /// | |
240 | /// assert_eq!(f.ceil(), 4.0); | |
241 | /// assert_eq!(g.ceil(), 4.0); | |
242 | /// ``` | |
243 | #[stable(feature = "rust1", since = "1.0.0")] | |
244 | #[inline] | |
e9174d1e SL |
245 | pub fn ceil(self) -> f64 { |
246 | unsafe { intrinsics::ceilf64(self) } | |
247 | } | |
970d7e83 | 248 | |
c34b1796 AL |
249 | /// Returns the nearest integer to a number. Round half-way cases away from |
250 | /// `0.0`. | |
251 | /// | |
252 | /// ``` | |
253 | /// let f = 3.3_f64; | |
254 | /// let g = -3.3_f64; | |
255 | /// | |
256 | /// assert_eq!(f.round(), 3.0); | |
257 | /// assert_eq!(g.round(), -3.0); | |
258 | /// ``` | |
259 | #[stable(feature = "rust1", since = "1.0.0")] | |
260 | #[inline] | |
e9174d1e SL |
261 | pub fn round(self) -> f64 { |
262 | unsafe { intrinsics::roundf64(self) } | |
263 | } | |
970d7e83 | 264 | |
9346a6ac | 265 | /// Returns the integer part of a number. |
c34b1796 AL |
266 | /// |
267 | /// ``` | |
268 | /// let f = 3.3_f64; | |
269 | /// let g = -3.7_f64; | |
270 | /// | |
271 | /// assert_eq!(f.trunc(), 3.0); | |
272 | /// assert_eq!(g.trunc(), -3.0); | |
273 | /// ``` | |
274 | #[stable(feature = "rust1", since = "1.0.0")] | |
275 | #[inline] | |
e9174d1e SL |
276 | pub fn trunc(self) -> f64 { |
277 | unsafe { intrinsics::truncf64(self) } | |
278 | } | |
970d7e83 | 279 | |
c34b1796 AL |
280 | /// Returns the fractional part of a number. |
281 | /// | |
282 | /// ``` | |
283 | /// let x = 3.5_f64; | |
284 | /// let y = -3.5_f64; | |
285 | /// let abs_difference_x = (x.fract() - 0.5).abs(); | |
286 | /// let abs_difference_y = (y.fract() - (-0.5)).abs(); | |
287 | /// | |
288 | /// assert!(abs_difference_x < 1e-10); | |
289 | /// assert!(abs_difference_y < 1e-10); | |
290 | /// ``` | |
291 | #[stable(feature = "rust1", since = "1.0.0")] | |
292 | #[inline] | |
e9174d1e | 293 | pub fn fract(self) -> f64 { self - self.trunc() } |
970d7e83 | 294 | |
c34b1796 AL |
295 | /// Computes the absolute value of `self`. Returns `NAN` if the |
296 | /// number is `NAN`. | |
297 | /// | |
298 | /// ``` | |
299 | /// use std::f64; | |
300 | /// | |
301 | /// let x = 3.5_f64; | |
302 | /// let y = -3.5_f64; | |
303 | /// | |
304 | /// let abs_difference_x = (x.abs() - x).abs(); | |
305 | /// let abs_difference_y = (y.abs() - (-y)).abs(); | |
306 | /// | |
307 | /// assert!(abs_difference_x < 1e-10); | |
308 | /// assert!(abs_difference_y < 1e-10); | |
309 | /// | |
310 | /// assert!(f64::NAN.abs().is_nan()); | |
311 | /// ``` | |
312 | #[stable(feature = "rust1", since = "1.0.0")] | |
313 | #[inline] | |
314 | pub fn abs(self) -> f64 { num::Float::abs(self) } | |
970d7e83 | 315 | |
c34b1796 AL |
316 | /// Returns a number that represents the sign of `self`. |
317 | /// | |
318 | /// - `1.0` if the number is positive, `+0.0` or `INFINITY` | |
319 | /// - `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY` | |
320 | /// - `NAN` if the number is `NAN` | |
321 | /// | |
322 | /// ``` | |
323 | /// use std::f64; | |
324 | /// | |
325 | /// let f = 3.5_f64; | |
326 | /// | |
327 | /// assert_eq!(f.signum(), 1.0); | |
328 | /// assert_eq!(f64::NEG_INFINITY.signum(), -1.0); | |
329 | /// | |
330 | /// assert!(f64::NAN.signum().is_nan()); | |
331 | /// ``` | |
332 | #[stable(feature = "rust1", since = "1.0.0")] | |
333 | #[inline] | |
334 | pub fn signum(self) -> f64 { num::Float::signum(self) } | |
970d7e83 | 335 | |
c34b1796 AL |
336 | /// Returns `true` if `self`'s sign bit is positive, including |
337 | /// `+0.0` and `INFINITY`. | |
338 | /// | |
339 | /// ``` | |
340 | /// use std::f64; | |
341 | /// | |
342 | /// let nan: f64 = f64::NAN; | |
343 | /// | |
344 | /// let f = 7.0_f64; | |
345 | /// let g = -7.0_f64; | |
346 | /// | |
347 | /// assert!(f.is_sign_positive()); | |
348 | /// assert!(!g.is_sign_positive()); | |
349 | /// // Requires both tests to determine if is `NaN` | |
350 | /// assert!(!nan.is_sign_positive() && !nan.is_sign_negative()); | |
351 | /// ``` | |
352 | #[stable(feature = "rust1", since = "1.0.0")] | |
353 | #[inline] | |
92a42be0 | 354 | pub fn is_sign_positive(self) -> bool { num::Float::is_sign_positive(self) } |
970d7e83 | 355 | |
c34b1796 | 356 | #[stable(feature = "rust1", since = "1.0.0")] |
92a42be0 | 357 | #[rustc_deprecated(since = "1.0.0", reason = "renamed to is_sign_positive")] |
c34b1796 | 358 | #[inline] |
92a42be0 | 359 | pub fn is_positive(self) -> bool { num::Float::is_sign_positive(self) } |
970d7e83 | 360 | |
c34b1796 AL |
361 | /// Returns `true` if `self`'s sign is negative, including `-0.0` |
362 | /// and `NEG_INFINITY`. | |
363 | /// | |
364 | /// ``` | |
365 | /// use std::f64; | |
366 | /// | |
367 | /// let nan = f64::NAN; | |
368 | /// | |
369 | /// let f = 7.0_f64; | |
370 | /// let g = -7.0_f64; | |
371 | /// | |
372 | /// assert!(!f.is_sign_negative()); | |
373 | /// assert!(g.is_sign_negative()); | |
374 | /// // Requires both tests to determine if is `NaN`. | |
375 | /// assert!(!nan.is_sign_positive() && !nan.is_sign_negative()); | |
376 | /// ``` | |
377 | #[stable(feature = "rust1", since = "1.0.0")] | |
378 | #[inline] | |
92a42be0 | 379 | pub fn is_sign_negative(self) -> bool { num::Float::is_sign_negative(self) } |
970d7e83 | 380 | |
c34b1796 | 381 | #[stable(feature = "rust1", since = "1.0.0")] |
92a42be0 | 382 | #[rustc_deprecated(since = "1.0.0", reason = "renamed to is_sign_negative")] |
c34b1796 | 383 | #[inline] |
92a42be0 | 384 | pub fn is_negative(self) -> bool { num::Float::is_sign_negative(self) } |
970d7e83 | 385 | |
c34b1796 AL |
386 | /// Fused multiply-add. Computes `(self * a) + b` with only one rounding |
387 | /// error. This produces a more accurate result with better performance than | |
388 | /// a separate multiplication operation followed by an add. | |
389 | /// | |
390 | /// ``` | |
391 | /// let m = 10.0_f64; | |
392 | /// let x = 4.0_f64; | |
393 | /// let b = 60.0_f64; | |
394 | /// | |
395 | /// // 100.0 | |
396 | /// let abs_difference = (m.mul_add(x, b) - (m*x + b)).abs(); | |
397 | /// | |
398 | /// assert!(abs_difference < 1e-10); | |
399 | /// ``` | |
400 | #[stable(feature = "rust1", since = "1.0.0")] | |
401 | #[inline] | |
e9174d1e SL |
402 | pub fn mul_add(self, a: f64, b: f64) -> f64 { |
403 | unsafe { intrinsics::fmaf64(self, a, b) } | |
404 | } | |
85aaf69f | 405 | |
9346a6ac | 406 | /// Takes the reciprocal (inverse) of a number, `1/x`. |
c34b1796 AL |
407 | /// |
408 | /// ``` | |
409 | /// let x = 2.0_f64; | |
410 | /// let abs_difference = (x.recip() - (1.0/x)).abs(); | |
411 | /// | |
412 | /// assert!(abs_difference < 1e-10); | |
413 | /// ``` | |
414 | #[stable(feature = "rust1", since = "1.0.0")] | |
415 | #[inline] | |
416 | pub fn recip(self) -> f64 { num::Float::recip(self) } | |
417 | ||
9346a6ac | 418 | /// Raises a number to an integer power. |
c34b1796 AL |
419 | /// |
420 | /// Using this function is generally faster than using `powf` | |
421 | /// | |
422 | /// ``` | |
423 | /// let x = 2.0_f64; | |
424 | /// let abs_difference = (x.powi(2) - x*x).abs(); | |
425 | /// | |
426 | /// assert!(abs_difference < 1e-10); | |
427 | /// ``` | |
428 | #[stable(feature = "rust1", since = "1.0.0")] | |
429 | #[inline] | |
430 | pub fn powi(self, n: i32) -> f64 { num::Float::powi(self, n) } | |
431 | ||
9346a6ac | 432 | /// Raises a number to a floating point power. |
c34b1796 AL |
433 | /// |
434 | /// ``` | |
435 | /// let x = 2.0_f64; | |
436 | /// let abs_difference = (x.powf(2.0) - x*x).abs(); | |
437 | /// | |
438 | /// assert!(abs_difference < 1e-10); | |
439 | /// ``` | |
440 | #[stable(feature = "rust1", since = "1.0.0")] | |
441 | #[inline] | |
e9174d1e SL |
442 | pub fn powf(self, n: f64) -> f64 { |
443 | unsafe { intrinsics::powf64(self, n) } | |
444 | } | |
c34b1796 | 445 | |
9346a6ac | 446 | /// Takes the square root of a number. |
c34b1796 AL |
447 | /// |
448 | /// Returns NaN if `self` is a negative number. | |
449 | /// | |
450 | /// ``` | |
451 | /// let positive = 4.0_f64; | |
452 | /// let negative = -4.0_f64; | |
453 | /// | |
454 | /// let abs_difference = (positive.sqrt() - 2.0).abs(); | |
455 | /// | |
456 | /// assert!(abs_difference < 1e-10); | |
457 | /// assert!(negative.sqrt().is_nan()); | |
458 | /// ``` | |
459 | #[stable(feature = "rust1", since = "1.0.0")] | |
460 | #[inline] | |
e9174d1e SL |
461 | pub fn sqrt(self) -> f64 { |
462 | if self < 0.0 { | |
463 | NAN | |
464 | } else { | |
465 | unsafe { intrinsics::sqrtf64(self) } | |
466 | } | |
467 | } | |
c34b1796 | 468 | |
c34b1796 AL |
469 | /// Returns `e^(self)`, (the exponential function). |
470 | /// | |
471 | /// ``` | |
472 | /// let one = 1.0_f64; | |
473 | /// // e^1 | |
474 | /// let e = one.exp(); | |
475 | /// | |
476 | /// // ln(e) - 1 == 0 | |
477 | /// let abs_difference = (e.ln() - 1.0).abs(); | |
478 | /// | |
479 | /// assert!(abs_difference < 1e-10); | |
480 | /// ``` | |
481 | #[stable(feature = "rust1", since = "1.0.0")] | |
482 | #[inline] | |
e9174d1e SL |
483 | pub fn exp(self) -> f64 { |
484 | unsafe { intrinsics::expf64(self) } | |
485 | } | |
c34b1796 AL |
486 | |
487 | /// Returns `2^(self)`. | |
488 | /// | |
489 | /// ``` | |
490 | /// let f = 2.0_f64; | |
491 | /// | |
492 | /// // 2^2 - 4 == 0 | |
493 | /// let abs_difference = (f.exp2() - 4.0).abs(); | |
494 | /// | |
495 | /// assert!(abs_difference < 1e-10); | |
496 | /// ``` | |
497 | #[stable(feature = "rust1", since = "1.0.0")] | |
498 | #[inline] | |
e9174d1e SL |
499 | pub fn exp2(self) -> f64 { |
500 | unsafe { intrinsics::exp2f64(self) } | |
501 | } | |
c34b1796 AL |
502 | |
503 | /// Returns the natural logarithm of the number. | |
504 | /// | |
505 | /// ``` | |
506 | /// let one = 1.0_f64; | |
507 | /// // e^1 | |
508 | /// let e = one.exp(); | |
509 | /// | |
510 | /// // ln(e) - 1 == 0 | |
511 | /// let abs_difference = (e.ln() - 1.0).abs(); | |
512 | /// | |
513 | /// assert!(abs_difference < 1e-10); | |
514 | /// ``` | |
515 | #[stable(feature = "rust1", since = "1.0.0")] | |
516 | #[inline] | |
e9174d1e | 517 | pub fn ln(self) -> f64 { |
7453a54e | 518 | self.log_wrapper(|n| { unsafe { intrinsics::logf64(n) } }) |
e9174d1e | 519 | } |
c34b1796 AL |
520 | |
521 | /// Returns the logarithm of the number with respect to an arbitrary base. | |
522 | /// | |
523 | /// ``` | |
524 | /// let ten = 10.0_f64; | |
525 | /// let two = 2.0_f64; | |
526 | /// | |
527 | /// // log10(10) - 1 == 0 | |
528 | /// let abs_difference_10 = (ten.log(10.0) - 1.0).abs(); | |
529 | /// | |
530 | /// // log2(2) - 1 == 0 | |
531 | /// let abs_difference_2 = (two.log(2.0) - 1.0).abs(); | |
532 | /// | |
533 | /// assert!(abs_difference_10 < 1e-10); | |
534 | /// assert!(abs_difference_2 < 1e-10); | |
535 | /// ``` | |
536 | #[stable(feature = "rust1", since = "1.0.0")] | |
537 | #[inline] | |
e9174d1e | 538 | pub fn log(self, base: f64) -> f64 { self.ln() / base.ln() } |
c34b1796 AL |
539 | |
540 | /// Returns the base 2 logarithm of the number. | |
541 | /// | |
542 | /// ``` | |
543 | /// let two = 2.0_f64; | |
544 | /// | |
545 | /// // log2(2) - 1 == 0 | |
546 | /// let abs_difference = (two.log2() - 1.0).abs(); | |
547 | /// | |
548 | /// assert!(abs_difference < 1e-10); | |
549 | /// ``` | |
550 | #[stable(feature = "rust1", since = "1.0.0")] | |
551 | #[inline] | |
e9174d1e | 552 | pub fn log2(self) -> f64 { |
a7813a04 XL |
553 | self.log_wrapper(|n| { |
554 | #[cfg(target_os = "android")] | |
555 | return ::sys::android::log2f64(n); | |
556 | #[cfg(not(target_os = "android"))] | |
557 | return unsafe { intrinsics::log2f64(n) }; | |
558 | }) | |
e9174d1e | 559 | } |
c34b1796 AL |
560 | |
561 | /// Returns the base 10 logarithm of the number. | |
562 | /// | |
563 | /// ``` | |
564 | /// let ten = 10.0_f64; | |
565 | /// | |
566 | /// // log10(10) - 1 == 0 | |
567 | /// let abs_difference = (ten.log10() - 1.0).abs(); | |
568 | /// | |
569 | /// assert!(abs_difference < 1e-10); | |
570 | /// ``` | |
571 | #[stable(feature = "rust1", since = "1.0.0")] | |
572 | #[inline] | |
e9174d1e | 573 | pub fn log10(self) -> f64 { |
7453a54e | 574 | self.log_wrapper(|n| { unsafe { intrinsics::log10f64(n) } }) |
e9174d1e | 575 | } |
c34b1796 | 576 | |
9346a6ac | 577 | /// Converts radians to degrees. |
c34b1796 AL |
578 | /// |
579 | /// ``` | |
580 | /// use std::f64::consts; | |
581 | /// | |
582 | /// let angle = consts::PI; | |
583 | /// | |
584 | /// let abs_difference = (angle.to_degrees() - 180.0).abs(); | |
585 | /// | |
586 | /// assert!(abs_difference < 1e-10); | |
587 | /// ``` | |
588 | #[stable(feature = "rust1", since = "1.0.0")] | |
589 | #[inline] | |
590 | pub fn to_degrees(self) -> f64 { num::Float::to_degrees(self) } | |
591 | ||
9346a6ac | 592 | /// Converts degrees to radians. |
c34b1796 AL |
593 | /// |
594 | /// ``` | |
595 | /// use std::f64::consts; | |
596 | /// | |
597 | /// let angle = 180.0_f64; | |
598 | /// | |
599 | /// let abs_difference = (angle.to_radians() - consts::PI).abs(); | |
600 | /// | |
601 | /// assert!(abs_difference < 1e-10); | |
602 | /// ``` | |
603 | #[stable(feature = "rust1", since = "1.0.0")] | |
604 | #[inline] | |
605 | pub fn to_radians(self) -> f64 { num::Float::to_radians(self) } | |
606 | ||
607 | /// Constructs a floating point number of `x*2^exp`. | |
608 | /// | |
609 | /// ``` | |
c1a9b12d SL |
610 | /// #![feature(float_extras)] |
611 | /// | |
c34b1796 AL |
612 | /// // 3*2^2 - 12 == 0 |
613 | /// let abs_difference = (f64::ldexp(3.0, 2) - 12.0).abs(); | |
614 | /// | |
615 | /// assert!(abs_difference < 1e-10); | |
616 | /// ``` | |
62682a34 | 617 | #[unstable(feature = "float_extras", |
e9174d1e SL |
618 | reason = "pending integer conventions", |
619 | issue = "27752")] | |
3157f602 XL |
620 | #[rustc_deprecated(since = "1.11.0", |
621 | reason = "never really came to fruition and easily \ | |
622 | implementable outside the standard library")] | |
c34b1796 AL |
623 | #[inline] |
624 | pub fn ldexp(x: f64, exp: isize) -> f64 { | |
625 | unsafe { cmath::ldexp(x, exp as c_int) } | |
626 | } | |
627 | ||
628 | /// Breaks the number into a normalized fraction and a base-2 exponent, | |
629 | /// satisfying: | |
630 | /// | |
631 | /// * `self = x * 2^exp` | |
632 | /// * `0.5 <= abs(x) < 1.0` | |
633 | /// | |
634 | /// ``` | |
c1a9b12d SL |
635 | /// #![feature(float_extras)] |
636 | /// | |
c34b1796 AL |
637 | /// let x = 4.0_f64; |
638 | /// | |
639 | /// // (1/2)*2^3 -> 1 * 8/2 -> 4.0 | |
640 | /// let f = x.frexp(); | |
641 | /// let abs_difference_0 = (f.0 - 0.5).abs(); | |
642 | /// let abs_difference_1 = (f.1 as f64 - 3.0).abs(); | |
643 | /// | |
644 | /// assert!(abs_difference_0 < 1e-10); | |
645 | /// assert!(abs_difference_1 < 1e-10); | |
646 | /// ``` | |
62682a34 | 647 | #[unstable(feature = "float_extras", |
e9174d1e SL |
648 | reason = "pending integer conventions", |
649 | issue = "27752")] | |
3157f602 XL |
650 | #[rustc_deprecated(since = "1.11.0", |
651 | reason = "never really came to fruition and easily \ | |
652 | implementable outside the standard library")] | |
c34b1796 AL |
653 | #[inline] |
654 | pub fn frexp(self) -> (f64, isize) { | |
655 | unsafe { | |
656 | let mut exp = 0; | |
657 | let x = cmath::frexp(self, &mut exp); | |
658 | (x, exp as isize) | |
659 | } | |
660 | } | |
661 | ||
662 | /// Returns the next representable floating-point value in the direction of | |
663 | /// `other`. | |
664 | /// | |
665 | /// ``` | |
c1a9b12d | 666 | /// #![feature(float_extras)] |
c34b1796 | 667 | /// |
3157f602 | 668 | /// let x = 1.0f64; |
c34b1796 | 669 | /// |
3157f602 | 670 | /// let abs_diff = (x.next_after(2.0) - 1.0000000000000002220446049250313_f64).abs(); |
c34b1796 AL |
671 | /// |
672 | /// assert!(abs_diff < 1e-10); | |
673 | /// ``` | |
62682a34 | 674 | #[unstable(feature = "float_extras", |
e9174d1e SL |
675 | reason = "unsure about its place in the world", |
676 | issue = "27752")] | |
3157f602 XL |
677 | #[rustc_deprecated(since = "1.11.0", |
678 | reason = "never really came to fruition and easily \ | |
679 | implementable outside the standard library")] | |
c34b1796 AL |
680 | #[inline] |
681 | pub fn next_after(self, other: f64) -> f64 { | |
682 | unsafe { cmath::nextafter(self, other) } | |
683 | } | |
684 | ||
685 | /// Returns the maximum of the two numbers. | |
686 | /// | |
687 | /// ``` | |
688 | /// let x = 1.0_f64; | |
689 | /// let y = 2.0_f64; | |
690 | /// | |
691 | /// assert_eq!(x.max(y), y); | |
692 | /// ``` | |
62682a34 SL |
693 | /// |
694 | /// If one of the arguments is NaN, then the other argument is returned. | |
c34b1796 AL |
695 | #[stable(feature = "rust1", since = "1.0.0")] |
696 | #[inline] | |
697 | pub fn max(self, other: f64) -> f64 { | |
698 | unsafe { cmath::fmax(self, other) } | |
699 | } | |
700 | ||
701 | /// Returns the minimum of the two numbers. | |
702 | /// | |
703 | /// ``` | |
704 | /// let x = 1.0_f64; | |
705 | /// let y = 2.0_f64; | |
706 | /// | |
707 | /// assert_eq!(x.min(y), x); | |
708 | /// ``` | |
62682a34 SL |
709 | /// |
710 | /// If one of the arguments is NaN, then the other argument is returned. | |
c34b1796 AL |
711 | #[stable(feature = "rust1", since = "1.0.0")] |
712 | #[inline] | |
713 | pub fn min(self, other: f64) -> f64 { | |
714 | unsafe { cmath::fmin(self, other) } | |
715 | } | |
716 | ||
717 | /// The positive difference of two numbers. | |
718 | /// | |
719 | /// * If `self <= other`: `0:0` | |
720 | /// * Else: `self - other` | |
721 | /// | |
722 | /// ``` | |
723 | /// let x = 3.0_f64; | |
724 | /// let y = -3.0_f64; | |
725 | /// | |
726 | /// let abs_difference_x = (x.abs_sub(1.0) - 2.0).abs(); | |
727 | /// let abs_difference_y = (y.abs_sub(1.0) - 0.0).abs(); | |
728 | /// | |
729 | /// assert!(abs_difference_x < 1e-10); | |
730 | /// assert!(abs_difference_y < 1e-10); | |
731 | /// ``` | |
732 | #[stable(feature = "rust1", since = "1.0.0")] | |
733 | #[inline] | |
3157f602 XL |
734 | #[rustc_deprecated(since = "1.10.0", |
735 | reason = "you probably meant `(self - other).abs()`: \ | |
736 | this operation is `(self - other).max(0.0)` (also \ | |
737 | known as `fdim` in C). If you truly need the positive \ | |
738 | difference, consider using that expression or the C function \ | |
739 | `fdim`, depending on how you wish to handle NaN (please consider \ | |
740 | filing an issue describing your use-case too).")] | |
741 | pub fn abs_sub(self, other: f64) -> f64 { | |
742 | unsafe { cmath::fdim(self, other) } | |
743 | } | |
c34b1796 | 744 | |
9346a6ac | 745 | /// Takes the cubic root of a number. |
c34b1796 AL |
746 | /// |
747 | /// ``` | |
748 | /// let x = 8.0_f64; | |
749 | /// | |
750 | /// // x^(1/3) - 2 == 0 | |
751 | /// let abs_difference = (x.cbrt() - 2.0).abs(); | |
752 | /// | |
753 | /// assert!(abs_difference < 1e-10); | |
754 | /// ``` | |
755 | #[stable(feature = "rust1", since = "1.0.0")] | |
756 | #[inline] | |
757 | pub fn cbrt(self) -> f64 { | |
758 | unsafe { cmath::cbrt(self) } | |
759 | } | |
760 | ||
9346a6ac | 761 | /// Calculates the length of the hypotenuse of a right-angle triangle given |
c34b1796 AL |
762 | /// legs of length `x` and `y`. |
763 | /// | |
764 | /// ``` | |
765 | /// let x = 2.0_f64; | |
766 | /// let y = 3.0_f64; | |
767 | /// | |
768 | /// // sqrt(x^2 + y^2) | |
769 | /// let abs_difference = (x.hypot(y) - (x.powi(2) + y.powi(2)).sqrt()).abs(); | |
770 | /// | |
771 | /// assert!(abs_difference < 1e-10); | |
772 | /// ``` | |
773 | #[stable(feature = "rust1", since = "1.0.0")] | |
774 | #[inline] | |
775 | pub fn hypot(self, other: f64) -> f64 { | |
776 | unsafe { cmath::hypot(self, other) } | |
777 | } | |
778 | ||
779 | /// Computes the sine of a number (in radians). | |
780 | /// | |
781 | /// ``` | |
782 | /// use std::f64; | |
783 | /// | |
784 | /// let x = f64::consts::PI/2.0; | |
785 | /// | |
786 | /// let abs_difference = (x.sin() - 1.0).abs(); | |
787 | /// | |
788 | /// assert!(abs_difference < 1e-10); | |
789 | /// ``` | |
790 | #[stable(feature = "rust1", since = "1.0.0")] | |
791 | #[inline] | |
792 | pub fn sin(self) -> f64 { | |
793 | unsafe { intrinsics::sinf64(self) } | |
794 | } | |
795 | ||
796 | /// Computes the cosine of a number (in radians). | |
797 | /// | |
798 | /// ``` | |
799 | /// use std::f64; | |
800 | /// | |
801 | /// let x = 2.0*f64::consts::PI; | |
802 | /// | |
803 | /// let abs_difference = (x.cos() - 1.0).abs(); | |
804 | /// | |
805 | /// assert!(abs_difference < 1e-10); | |
806 | /// ``` | |
807 | #[stable(feature = "rust1", since = "1.0.0")] | |
808 | #[inline] | |
809 | pub fn cos(self) -> f64 { | |
810 | unsafe { intrinsics::cosf64(self) } | |
811 | } | |
812 | ||
813 | /// Computes the tangent of a number (in radians). | |
814 | /// | |
815 | /// ``` | |
816 | /// use std::f64; | |
817 | /// | |
818 | /// let x = f64::consts::PI/4.0; | |
819 | /// let abs_difference = (x.tan() - 1.0).abs(); | |
820 | /// | |
821 | /// assert!(abs_difference < 1e-14); | |
822 | /// ``` | |
823 | #[stable(feature = "rust1", since = "1.0.0")] | |
824 | #[inline] | |
825 | pub fn tan(self) -> f64 { | |
826 | unsafe { cmath::tan(self) } | |
827 | } | |
828 | ||
829 | /// Computes the arcsine of a number. Return value is in radians in | |
830 | /// the range [-pi/2, pi/2] or NaN if the number is outside the range | |
831 | /// [-1, 1]. | |
832 | /// | |
833 | /// ``` | |
834 | /// use std::f64; | |
835 | /// | |
836 | /// let f = f64::consts::PI / 2.0; | |
837 | /// | |
838 | /// // asin(sin(pi/2)) | |
839 | /// let abs_difference = (f.sin().asin() - f64::consts::PI / 2.0).abs(); | |
840 | /// | |
841 | /// assert!(abs_difference < 1e-10); | |
842 | /// ``` | |
843 | #[stable(feature = "rust1", since = "1.0.0")] | |
844 | #[inline] | |
845 | pub fn asin(self) -> f64 { | |
846 | unsafe { cmath::asin(self) } | |
847 | } | |
848 | ||
849 | /// Computes the arccosine of a number. Return value is in radians in | |
850 | /// the range [0, pi] or NaN if the number is outside the range | |
851 | /// [-1, 1]. | |
852 | /// | |
853 | /// ``` | |
854 | /// use std::f64; | |
855 | /// | |
856 | /// let f = f64::consts::PI / 4.0; | |
857 | /// | |
858 | /// // acos(cos(pi/4)) | |
859 | /// let abs_difference = (f.cos().acos() - f64::consts::PI / 4.0).abs(); | |
860 | /// | |
861 | /// assert!(abs_difference < 1e-10); | |
862 | /// ``` | |
863 | #[stable(feature = "rust1", since = "1.0.0")] | |
864 | #[inline] | |
865 | pub fn acos(self) -> f64 { | |
866 | unsafe { cmath::acos(self) } | |
867 | } | |
868 | ||
869 | /// Computes the arctangent of a number. Return value is in radians in the | |
870 | /// range [-pi/2, pi/2]; | |
871 | /// | |
872 | /// ``` | |
873 | /// let f = 1.0_f64; | |
874 | /// | |
875 | /// // atan(tan(1)) | |
876 | /// let abs_difference = (f.tan().atan() - 1.0).abs(); | |
877 | /// | |
878 | /// assert!(abs_difference < 1e-10); | |
879 | /// ``` | |
880 | #[stable(feature = "rust1", since = "1.0.0")] | |
881 | #[inline] | |
882 | pub fn atan(self) -> f64 { | |
883 | unsafe { cmath::atan(self) } | |
884 | } | |
885 | ||
886 | /// Computes the four quadrant arctangent of `self` (`y`) and `other` (`x`). | |
887 | /// | |
888 | /// * `x = 0`, `y = 0`: `0` | |
889 | /// * `x >= 0`: `arctan(y/x)` -> `[-pi/2, pi/2]` | |
890 | /// * `y >= 0`: `arctan(y/x) + pi` -> `(pi/2, pi]` | |
891 | /// * `y < 0`: `arctan(y/x) - pi` -> `(-pi, -pi/2)` | |
892 | /// | |
893 | /// ``` | |
894 | /// use std::f64; | |
895 | /// | |
896 | /// let pi = f64::consts::PI; | |
897 | /// // All angles from horizontal right (+x) | |
898 | /// // 45 deg counter-clockwise | |
899 | /// let x1 = 3.0_f64; | |
900 | /// let y1 = -3.0_f64; | |
901 | /// | |
902 | /// // 135 deg clockwise | |
903 | /// let x2 = -3.0_f64; | |
904 | /// let y2 = 3.0_f64; | |
905 | /// | |
906 | /// let abs_difference_1 = (y1.atan2(x1) - (-pi/4.0)).abs(); | |
907 | /// let abs_difference_2 = (y2.atan2(x2) - 3.0*pi/4.0).abs(); | |
908 | /// | |
909 | /// assert!(abs_difference_1 < 1e-10); | |
910 | /// assert!(abs_difference_2 < 1e-10); | |
911 | /// ``` | |
912 | #[stable(feature = "rust1", since = "1.0.0")] | |
913 | #[inline] | |
914 | pub fn atan2(self, other: f64) -> f64 { | |
915 | unsafe { cmath::atan2(self, other) } | |
916 | } | |
917 | ||
918 | /// Simultaneously computes the sine and cosine of the number, `x`. Returns | |
919 | /// `(sin(x), cos(x))`. | |
920 | /// | |
921 | /// ``` | |
922 | /// use std::f64; | |
923 | /// | |
924 | /// let x = f64::consts::PI/4.0; | |
925 | /// let f = x.sin_cos(); | |
926 | /// | |
927 | /// let abs_difference_0 = (f.0 - x.sin()).abs(); | |
928 | /// let abs_difference_1 = (f.1 - x.cos()).abs(); | |
929 | /// | |
930 | /// assert!(abs_difference_0 < 1e-10); | |
a7813a04 | 931 | /// assert!(abs_difference_1 < 1e-10); |
c34b1796 AL |
932 | /// ``` |
933 | #[stable(feature = "rust1", since = "1.0.0")] | |
934 | #[inline] | |
935 | pub fn sin_cos(self) -> (f64, f64) { | |
936 | (self.sin(), self.cos()) | |
937 | } | |
938 | ||
939 | /// Returns `e^(self) - 1` in a way that is accurate even if the | |
940 | /// number is close to zero. | |
941 | /// | |
942 | /// ``` | |
943 | /// let x = 7.0_f64; | |
944 | /// | |
945 | /// // e^(ln(7)) - 1 | |
946 | /// let abs_difference = (x.ln().exp_m1() - 6.0).abs(); | |
947 | /// | |
948 | /// assert!(abs_difference < 1e-10); | |
949 | /// ``` | |
950 | #[stable(feature = "rust1", since = "1.0.0")] | |
951 | #[inline] | |
952 | pub fn exp_m1(self) -> f64 { | |
953 | unsafe { cmath::expm1(self) } | |
954 | } | |
955 | ||
956 | /// Returns `ln(1+n)` (natural logarithm) more accurately than if | |
957 | /// the operations were performed separately. | |
958 | /// | |
959 | /// ``` | |
960 | /// use std::f64; | |
961 | /// | |
962 | /// let x = f64::consts::E - 1.0; | |
963 | /// | |
964 | /// // ln(1 + (e - 1)) == ln(e) == 1 | |
965 | /// let abs_difference = (x.ln_1p() - 1.0).abs(); | |
966 | /// | |
967 | /// assert!(abs_difference < 1e-10); | |
968 | /// ``` | |
969 | #[stable(feature = "rust1", since = "1.0.0")] | |
970 | #[inline] | |
971 | pub fn ln_1p(self) -> f64 { | |
972 | unsafe { cmath::log1p(self) } | |
973 | } | |
974 | ||
975 | /// Hyperbolic sine function. | |
976 | /// | |
977 | /// ``` | |
978 | /// use std::f64; | |
979 | /// | |
980 | /// let e = f64::consts::E; | |
981 | /// let x = 1.0_f64; | |
982 | /// | |
983 | /// let f = x.sinh(); | |
984 | /// // Solving sinh() at 1 gives `(e^2-1)/(2e)` | |
985 | /// let g = (e*e - 1.0)/(2.0*e); | |
986 | /// let abs_difference = (f - g).abs(); | |
987 | /// | |
988 | /// assert!(abs_difference < 1e-10); | |
989 | /// ``` | |
990 | #[stable(feature = "rust1", since = "1.0.0")] | |
991 | #[inline] | |
992 | pub fn sinh(self) -> f64 { | |
993 | unsafe { cmath::sinh(self) } | |
994 | } | |
995 | ||
996 | /// Hyperbolic cosine function. | |
997 | /// | |
998 | /// ``` | |
999 | /// use std::f64; | |
1000 | /// | |
1001 | /// let e = f64::consts::E; | |
1002 | /// let x = 1.0_f64; | |
1003 | /// let f = x.cosh(); | |
1004 | /// // Solving cosh() at 1 gives this result | |
1005 | /// let g = (e*e + 1.0)/(2.0*e); | |
1006 | /// let abs_difference = (f - g).abs(); | |
1007 | /// | |
1008 | /// // Same result | |
1009 | /// assert!(abs_difference < 1.0e-10); | |
1010 | /// ``` | |
1011 | #[stable(feature = "rust1", since = "1.0.0")] | |
1012 | #[inline] | |
1013 | pub fn cosh(self) -> f64 { | |
1014 | unsafe { cmath::cosh(self) } | |
1015 | } | |
1016 | ||
1017 | /// Hyperbolic tangent function. | |
1018 | /// | |
1019 | /// ``` | |
1020 | /// use std::f64; | |
1021 | /// | |
1022 | /// let e = f64::consts::E; | |
1023 | /// let x = 1.0_f64; | |
1024 | /// | |
1025 | /// let f = x.tanh(); | |
1026 | /// // Solving tanh() at 1 gives `(1 - e^(-2))/(1 + e^(-2))` | |
1027 | /// let g = (1.0 - e.powi(-2))/(1.0 + e.powi(-2)); | |
1028 | /// let abs_difference = (f - g).abs(); | |
1029 | /// | |
1030 | /// assert!(abs_difference < 1.0e-10); | |
1031 | /// ``` | |
1032 | #[stable(feature = "rust1", since = "1.0.0")] | |
1033 | #[inline] | |
1034 | pub fn tanh(self) -> f64 { | |
1035 | unsafe { cmath::tanh(self) } | |
1036 | } | |
1037 | ||
1038 | /// Inverse hyperbolic sine function. | |
1039 | /// | |
1040 | /// ``` | |
1041 | /// let x = 1.0_f64; | |
1042 | /// let f = x.sinh().asinh(); | |
1043 | /// | |
1044 | /// let abs_difference = (f - x).abs(); | |
1045 | /// | |
1046 | /// assert!(abs_difference < 1.0e-10); | |
1047 | /// ``` | |
1048 | #[stable(feature = "rust1", since = "1.0.0")] | |
1049 | #[inline] | |
1050 | pub fn asinh(self) -> f64 { | |
54a0048b SL |
1051 | if self == NEG_INFINITY { |
1052 | NEG_INFINITY | |
1053 | } else { | |
1054 | (self + ((self * self) + 1.0).sqrt()).ln() | |
c34b1796 AL |
1055 | } |
1056 | } | |
1057 | ||
1058 | /// Inverse hyperbolic cosine function. | |
1059 | /// | |
1060 | /// ``` | |
1061 | /// let x = 1.0_f64; | |
1062 | /// let f = x.cosh().acosh(); | |
1063 | /// | |
1064 | /// let abs_difference = (f - x).abs(); | |
1065 | /// | |
1066 | /// assert!(abs_difference < 1.0e-10); | |
1067 | /// ``` | |
1068 | #[stable(feature = "rust1", since = "1.0.0")] | |
1069 | #[inline] | |
1070 | pub fn acosh(self) -> f64 { | |
1071 | match self { | |
9346a6ac | 1072 | x if x < 1.0 => NAN, |
c34b1796 AL |
1073 | x => (x + ((x * x) - 1.0).sqrt()).ln(), |
1074 | } | |
1075 | } | |
1076 | ||
1077 | /// Inverse hyperbolic tangent function. | |
1078 | /// | |
1079 | /// ``` | |
1080 | /// use std::f64; | |
1081 | /// | |
1082 | /// let e = f64::consts::E; | |
1083 | /// let f = e.tanh().atanh(); | |
1084 | /// | |
1085 | /// let abs_difference = (f - e).abs(); | |
1086 | /// | |
1087 | /// assert!(abs_difference < 1.0e-10); | |
1088 | /// ``` | |
1089 | #[stable(feature = "rust1", since = "1.0.0")] | |
1090 | #[inline] | |
1091 | pub fn atanh(self) -> f64 { | |
1092 | 0.5 * ((2.0 * self) / (1.0 - self)).ln_1p() | |
1093 | } | |
7453a54e SL |
1094 | |
1095 | // Solaris/Illumos requires a wrapper around log, log2, and log10 functions | |
1096 | // because of their non-standard behavior (e.g. log(-n) returns -Inf instead | |
1097 | // of expected NaN). | |
1098 | fn log_wrapper<F: Fn(f64) -> f64>(self, log_fn: F) -> f64 { | |
1099 | if !cfg!(target_os = "solaris") { | |
1100 | log_fn(self) | |
1101 | } else { | |
1102 | if self.is_finite() { | |
1103 | if self > 0.0 { | |
1104 | log_fn(self) | |
1105 | } else if self == 0.0 { | |
1106 | NEG_INFINITY // log(0) = -Inf | |
1107 | } else { | |
1108 | NAN // log(-n) = NaN | |
1109 | } | |
1110 | } else if self.is_nan() { | |
1111 | self // log(NaN) = NaN | |
1112 | } else if self > 0.0 { | |
1113 | self // log(Inf) = Inf | |
1114 | } else { | |
1115 | NAN // log(-Inf) = NaN | |
1116 | } | |
1117 | } | |
1118 | } | |
c34b1796 AL |
1119 | } |
1120 | ||
c34b1796 AL |
1121 | #[cfg(test)] |
1122 | mod tests { | |
9346a6ac | 1123 | use f64; |
c34b1796 AL |
1124 | use f64::*; |
1125 | use num::*; | |
1126 | use num::FpCategory as Fp; | |
1127 | ||
1128 | #[test] | |
1129 | fn test_num_f64() { | |
1130 | test_num(10f64, 2f64); | |
1131 | } | |
1132 | ||
1133 | #[test] | |
1134 | fn test_min_nan() { | |
1135 | assert_eq!(NAN.min(2.0), 2.0); | |
1136 | assert_eq!(2.0f64.min(NAN), 2.0); | |
1137 | } | |
1138 | ||
1139 | #[test] | |
1140 | fn test_max_nan() { | |
1141 | assert_eq!(NAN.max(2.0), 2.0); | |
1142 | assert_eq!(2.0f64.max(NAN), 2.0); | |
1143 | } | |
1144 | ||
1145 | #[test] | |
1146 | fn test_nan() { | |
9346a6ac | 1147 | let nan: f64 = NAN; |
c34b1796 AL |
1148 | assert!(nan.is_nan()); |
1149 | assert!(!nan.is_infinite()); | |
1150 | assert!(!nan.is_finite()); | |
1151 | assert!(!nan.is_normal()); | |
1152 | assert!(!nan.is_sign_positive()); | |
1153 | assert!(!nan.is_sign_negative()); | |
1154 | assert_eq!(Fp::Nan, nan.classify()); | |
1155 | } | |
1156 | ||
1157 | #[test] | |
1158 | fn test_infinity() { | |
9346a6ac | 1159 | let inf: f64 = INFINITY; |
c34b1796 AL |
1160 | assert!(inf.is_infinite()); |
1161 | assert!(!inf.is_finite()); | |
1162 | assert!(inf.is_sign_positive()); | |
1163 | assert!(!inf.is_sign_negative()); | |
1164 | assert!(!inf.is_nan()); | |
1165 | assert!(!inf.is_normal()); | |
1166 | assert_eq!(Fp::Infinite, inf.classify()); | |
1167 | } | |
1168 | ||
1169 | #[test] | |
1170 | fn test_neg_infinity() { | |
9346a6ac | 1171 | let neg_inf: f64 = NEG_INFINITY; |
c34b1796 AL |
1172 | assert!(neg_inf.is_infinite()); |
1173 | assert!(!neg_inf.is_finite()); | |
1174 | assert!(!neg_inf.is_sign_positive()); | |
1175 | assert!(neg_inf.is_sign_negative()); | |
1176 | assert!(!neg_inf.is_nan()); | |
1177 | assert!(!neg_inf.is_normal()); | |
1178 | assert_eq!(Fp::Infinite, neg_inf.classify()); | |
1179 | } | |
1180 | ||
1181 | #[test] | |
1182 | fn test_zero() { | |
9346a6ac | 1183 | let zero: f64 = 0.0f64; |
c34b1796 AL |
1184 | assert_eq!(0.0, zero); |
1185 | assert!(!zero.is_infinite()); | |
1186 | assert!(zero.is_finite()); | |
1187 | assert!(zero.is_sign_positive()); | |
1188 | assert!(!zero.is_sign_negative()); | |
1189 | assert!(!zero.is_nan()); | |
1190 | assert!(!zero.is_normal()); | |
1191 | assert_eq!(Fp::Zero, zero.classify()); | |
1192 | } | |
1193 | ||
1194 | #[test] | |
1195 | fn test_neg_zero() { | |
9346a6ac | 1196 | let neg_zero: f64 = -0.0; |
c34b1796 AL |
1197 | assert_eq!(0.0, neg_zero); |
1198 | assert!(!neg_zero.is_infinite()); | |
1199 | assert!(neg_zero.is_finite()); | |
1200 | assert!(!neg_zero.is_sign_positive()); | |
1201 | assert!(neg_zero.is_sign_negative()); | |
1202 | assert!(!neg_zero.is_nan()); | |
1203 | assert!(!neg_zero.is_normal()); | |
1204 | assert_eq!(Fp::Zero, neg_zero.classify()); | |
1205 | } | |
1206 | ||
1207 | #[test] | |
1208 | fn test_one() { | |
9346a6ac | 1209 | let one: f64 = 1.0f64; |
c34b1796 AL |
1210 | assert_eq!(1.0, one); |
1211 | assert!(!one.is_infinite()); | |
1212 | assert!(one.is_finite()); | |
1213 | assert!(one.is_sign_positive()); | |
1214 | assert!(!one.is_sign_negative()); | |
1215 | assert!(!one.is_nan()); | |
1216 | assert!(one.is_normal()); | |
1217 | assert_eq!(Fp::Normal, one.classify()); | |
1218 | } | |
1219 | ||
1220 | #[test] | |
1221 | fn test_is_nan() { | |
9346a6ac AL |
1222 | let nan: f64 = NAN; |
1223 | let inf: f64 = INFINITY; | |
1224 | let neg_inf: f64 = NEG_INFINITY; | |
c34b1796 AL |
1225 | assert!(nan.is_nan()); |
1226 | assert!(!0.0f64.is_nan()); | |
1227 | assert!(!5.3f64.is_nan()); | |
1228 | assert!(!(-10.732f64).is_nan()); | |
1229 | assert!(!inf.is_nan()); | |
1230 | assert!(!neg_inf.is_nan()); | |
1231 | } | |
1232 | ||
1233 | #[test] | |
1234 | fn test_is_infinite() { | |
9346a6ac AL |
1235 | let nan: f64 = NAN; |
1236 | let inf: f64 = INFINITY; | |
1237 | let neg_inf: f64 = NEG_INFINITY; | |
c34b1796 AL |
1238 | assert!(!nan.is_infinite()); |
1239 | assert!(inf.is_infinite()); | |
1240 | assert!(neg_inf.is_infinite()); | |
1241 | assert!(!0.0f64.is_infinite()); | |
1242 | assert!(!42.8f64.is_infinite()); | |
1243 | assert!(!(-109.2f64).is_infinite()); | |
1244 | } | |
1245 | ||
1246 | #[test] | |
1247 | fn test_is_finite() { | |
9346a6ac AL |
1248 | let nan: f64 = NAN; |
1249 | let inf: f64 = INFINITY; | |
1250 | let neg_inf: f64 = NEG_INFINITY; | |
c34b1796 AL |
1251 | assert!(!nan.is_finite()); |
1252 | assert!(!inf.is_finite()); | |
1253 | assert!(!neg_inf.is_finite()); | |
1254 | assert!(0.0f64.is_finite()); | |
1255 | assert!(42.8f64.is_finite()); | |
1256 | assert!((-109.2f64).is_finite()); | |
1257 | } | |
1258 | ||
1259 | #[test] | |
1260 | fn test_is_normal() { | |
9346a6ac AL |
1261 | let nan: f64 = NAN; |
1262 | let inf: f64 = INFINITY; | |
1263 | let neg_inf: f64 = NEG_INFINITY; | |
1264 | let zero: f64 = 0.0f64; | |
1265 | let neg_zero: f64 = -0.0; | |
c34b1796 AL |
1266 | assert!(!nan.is_normal()); |
1267 | assert!(!inf.is_normal()); | |
1268 | assert!(!neg_inf.is_normal()); | |
1269 | assert!(!zero.is_normal()); | |
1270 | assert!(!neg_zero.is_normal()); | |
1271 | assert!(1f64.is_normal()); | |
1272 | assert!(1e-307f64.is_normal()); | |
1273 | assert!(!1e-308f64.is_normal()); | |
1274 | } | |
1275 | ||
1276 | #[test] | |
1277 | fn test_classify() { | |
9346a6ac AL |
1278 | let nan: f64 = NAN; |
1279 | let inf: f64 = INFINITY; | |
1280 | let neg_inf: f64 = NEG_INFINITY; | |
1281 | let zero: f64 = 0.0f64; | |
1282 | let neg_zero: f64 = -0.0; | |
c34b1796 AL |
1283 | assert_eq!(nan.classify(), Fp::Nan); |
1284 | assert_eq!(inf.classify(), Fp::Infinite); | |
1285 | assert_eq!(neg_inf.classify(), Fp::Infinite); | |
1286 | assert_eq!(zero.classify(), Fp::Zero); | |
1287 | assert_eq!(neg_zero.classify(), Fp::Zero); | |
1288 | assert_eq!(1e-307f64.classify(), Fp::Normal); | |
1289 | assert_eq!(1e-308f64.classify(), Fp::Subnormal); | |
1290 | } | |
1291 | ||
1292 | #[test] | |
3157f602 | 1293 | #[allow(deprecated)] |
c34b1796 AL |
1294 | fn test_integer_decode() { |
1295 | assert_eq!(3.14159265359f64.integer_decode(), (7074237752028906, -51, 1)); | |
1296 | assert_eq!((-8573.5918555f64).integer_decode(), (4713381968463931, -39, -1)); | |
1297 | assert_eq!(2f64.powf(100.0).integer_decode(), (4503599627370496, 48, 1)); | |
1298 | assert_eq!(0f64.integer_decode(), (0, -1075, 1)); | |
1299 | assert_eq!((-0f64).integer_decode(), (0, -1075, -1)); | |
1300 | assert_eq!(INFINITY.integer_decode(), (4503599627370496, 972, 1)); | |
1301 | assert_eq!(NEG_INFINITY.integer_decode(), (4503599627370496, 972, -1)); | |
3157f602 XL |
1302 | |
1303 | // Ignore the "sign" (quiet / signalling flag) of NAN. | |
1304 | // It can vary between runtime operations and LLVM folding. | |
1305 | let (nan_m, nan_e, _nan_s) = NAN.integer_decode(); | |
1306 | assert_eq!((nan_m, nan_e), (6755399441055744, 972)); | |
c34b1796 AL |
1307 | } |
1308 | ||
1309 | #[test] | |
1310 | fn test_floor() { | |
1311 | assert_approx_eq!(1.0f64.floor(), 1.0f64); | |
1312 | assert_approx_eq!(1.3f64.floor(), 1.0f64); | |
1313 | assert_approx_eq!(1.5f64.floor(), 1.0f64); | |
1314 | assert_approx_eq!(1.7f64.floor(), 1.0f64); | |
1315 | assert_approx_eq!(0.0f64.floor(), 0.0f64); | |
1316 | assert_approx_eq!((-0.0f64).floor(), -0.0f64); | |
1317 | assert_approx_eq!((-1.0f64).floor(), -1.0f64); | |
1318 | assert_approx_eq!((-1.3f64).floor(), -2.0f64); | |
1319 | assert_approx_eq!((-1.5f64).floor(), -2.0f64); | |
1320 | assert_approx_eq!((-1.7f64).floor(), -2.0f64); | |
1321 | } | |
1322 | ||
1323 | #[test] | |
1324 | fn test_ceil() { | |
1325 | assert_approx_eq!(1.0f64.ceil(), 1.0f64); | |
1326 | assert_approx_eq!(1.3f64.ceil(), 2.0f64); | |
1327 | assert_approx_eq!(1.5f64.ceil(), 2.0f64); | |
1328 | assert_approx_eq!(1.7f64.ceil(), 2.0f64); | |
1329 | assert_approx_eq!(0.0f64.ceil(), 0.0f64); | |
1330 | assert_approx_eq!((-0.0f64).ceil(), -0.0f64); | |
1331 | assert_approx_eq!((-1.0f64).ceil(), -1.0f64); | |
1332 | assert_approx_eq!((-1.3f64).ceil(), -1.0f64); | |
1333 | assert_approx_eq!((-1.5f64).ceil(), -1.0f64); | |
1334 | assert_approx_eq!((-1.7f64).ceil(), -1.0f64); | |
1335 | } | |
1336 | ||
1337 | #[test] | |
1338 | fn test_round() { | |
1339 | assert_approx_eq!(1.0f64.round(), 1.0f64); | |
1340 | assert_approx_eq!(1.3f64.round(), 1.0f64); | |
1341 | assert_approx_eq!(1.5f64.round(), 2.0f64); | |
1342 | assert_approx_eq!(1.7f64.round(), 2.0f64); | |
1343 | assert_approx_eq!(0.0f64.round(), 0.0f64); | |
1344 | assert_approx_eq!((-0.0f64).round(), -0.0f64); | |
1345 | assert_approx_eq!((-1.0f64).round(), -1.0f64); | |
1346 | assert_approx_eq!((-1.3f64).round(), -1.0f64); | |
1347 | assert_approx_eq!((-1.5f64).round(), -2.0f64); | |
1348 | assert_approx_eq!((-1.7f64).round(), -2.0f64); | |
1349 | } | |
1350 | ||
1351 | #[test] | |
1352 | fn test_trunc() { | |
1353 | assert_approx_eq!(1.0f64.trunc(), 1.0f64); | |
1354 | assert_approx_eq!(1.3f64.trunc(), 1.0f64); | |
1355 | assert_approx_eq!(1.5f64.trunc(), 1.0f64); | |
1356 | assert_approx_eq!(1.7f64.trunc(), 1.0f64); | |
1357 | assert_approx_eq!(0.0f64.trunc(), 0.0f64); | |
1358 | assert_approx_eq!((-0.0f64).trunc(), -0.0f64); | |
1359 | assert_approx_eq!((-1.0f64).trunc(), -1.0f64); | |
1360 | assert_approx_eq!((-1.3f64).trunc(), -1.0f64); | |
1361 | assert_approx_eq!((-1.5f64).trunc(), -1.0f64); | |
1362 | assert_approx_eq!((-1.7f64).trunc(), -1.0f64); | |
1363 | } | |
1364 | ||
1365 | #[test] | |
1366 | fn test_fract() { | |
1367 | assert_approx_eq!(1.0f64.fract(), 0.0f64); | |
1368 | assert_approx_eq!(1.3f64.fract(), 0.3f64); | |
1369 | assert_approx_eq!(1.5f64.fract(), 0.5f64); | |
1370 | assert_approx_eq!(1.7f64.fract(), 0.7f64); | |
1371 | assert_approx_eq!(0.0f64.fract(), 0.0f64); | |
1372 | assert_approx_eq!((-0.0f64).fract(), -0.0f64); | |
1373 | assert_approx_eq!((-1.0f64).fract(), -0.0f64); | |
1374 | assert_approx_eq!((-1.3f64).fract(), -0.3f64); | |
1375 | assert_approx_eq!((-1.5f64).fract(), -0.5f64); | |
1376 | assert_approx_eq!((-1.7f64).fract(), -0.7f64); | |
1377 | } | |
1378 | ||
1379 | #[test] | |
1380 | fn test_abs() { | |
1381 | assert_eq!(INFINITY.abs(), INFINITY); | |
1382 | assert_eq!(1f64.abs(), 1f64); | |
1383 | assert_eq!(0f64.abs(), 0f64); | |
1384 | assert_eq!((-0f64).abs(), 0f64); | |
1385 | assert_eq!((-1f64).abs(), 1f64); | |
1386 | assert_eq!(NEG_INFINITY.abs(), INFINITY); | |
1387 | assert_eq!((1f64/NEG_INFINITY).abs(), 0f64); | |
1388 | assert!(NAN.abs().is_nan()); | |
1389 | } | |
1390 | ||
1391 | #[test] | |
1392 | fn test_signum() { | |
1393 | assert_eq!(INFINITY.signum(), 1f64); | |
1394 | assert_eq!(1f64.signum(), 1f64); | |
1395 | assert_eq!(0f64.signum(), 1f64); | |
1396 | assert_eq!((-0f64).signum(), -1f64); | |
1397 | assert_eq!((-1f64).signum(), -1f64); | |
1398 | assert_eq!(NEG_INFINITY.signum(), -1f64); | |
1399 | assert_eq!((1f64/NEG_INFINITY).signum(), -1f64); | |
1400 | assert!(NAN.signum().is_nan()); | |
1401 | } | |
1402 | ||
1403 | #[test] | |
1404 | fn test_is_sign_positive() { | |
1405 | assert!(INFINITY.is_sign_positive()); | |
1406 | assert!(1f64.is_sign_positive()); | |
1407 | assert!(0f64.is_sign_positive()); | |
1408 | assert!(!(-0f64).is_sign_positive()); | |
1409 | assert!(!(-1f64).is_sign_positive()); | |
1410 | assert!(!NEG_INFINITY.is_sign_positive()); | |
1411 | assert!(!(1f64/NEG_INFINITY).is_sign_positive()); | |
1412 | assert!(!NAN.is_sign_positive()); | |
1413 | } | |
1414 | ||
1415 | #[test] | |
1416 | fn test_is_sign_negative() { | |
1417 | assert!(!INFINITY.is_sign_negative()); | |
1418 | assert!(!1f64.is_sign_negative()); | |
1419 | assert!(!0f64.is_sign_negative()); | |
1420 | assert!((-0f64).is_sign_negative()); | |
1421 | assert!((-1f64).is_sign_negative()); | |
1422 | assert!(NEG_INFINITY.is_sign_negative()); | |
1423 | assert!((1f64/NEG_INFINITY).is_sign_negative()); | |
1424 | assert!(!NAN.is_sign_negative()); | |
1425 | } | |
1426 | ||
1427 | #[test] | |
1428 | fn test_mul_add() { | |
9346a6ac AL |
1429 | let nan: f64 = NAN; |
1430 | let inf: f64 = INFINITY; | |
1431 | let neg_inf: f64 = NEG_INFINITY; | |
c34b1796 AL |
1432 | assert_approx_eq!(12.3f64.mul_add(4.5, 6.7), 62.05); |
1433 | assert_approx_eq!((-12.3f64).mul_add(-4.5, -6.7), 48.65); | |
1434 | assert_approx_eq!(0.0f64.mul_add(8.9, 1.2), 1.2); | |
1435 | assert_approx_eq!(3.4f64.mul_add(-0.0, 5.6), 5.6); | |
1436 | assert!(nan.mul_add(7.8, 9.0).is_nan()); | |
1437 | assert_eq!(inf.mul_add(7.8, 9.0), inf); | |
1438 | assert_eq!(neg_inf.mul_add(7.8, 9.0), neg_inf); | |
1439 | assert_eq!(8.9f64.mul_add(inf, 3.2), inf); | |
1440 | assert_eq!((-3.2f64).mul_add(2.4, neg_inf), neg_inf); | |
1441 | } | |
1442 | ||
1443 | #[test] | |
1444 | fn test_recip() { | |
9346a6ac AL |
1445 | let nan: f64 = NAN; |
1446 | let inf: f64 = INFINITY; | |
1447 | let neg_inf: f64 = NEG_INFINITY; | |
c34b1796 AL |
1448 | assert_eq!(1.0f64.recip(), 1.0); |
1449 | assert_eq!(2.0f64.recip(), 0.5); | |
1450 | assert_eq!((-0.4f64).recip(), -2.5); | |
1451 | assert_eq!(0.0f64.recip(), inf); | |
1452 | assert!(nan.recip().is_nan()); | |
1453 | assert_eq!(inf.recip(), 0.0); | |
1454 | assert_eq!(neg_inf.recip(), 0.0); | |
1455 | } | |
1456 | ||
1457 | #[test] | |
1458 | fn test_powi() { | |
9346a6ac AL |
1459 | let nan: f64 = NAN; |
1460 | let inf: f64 = INFINITY; | |
1461 | let neg_inf: f64 = NEG_INFINITY; | |
c34b1796 AL |
1462 | assert_eq!(1.0f64.powi(1), 1.0); |
1463 | assert_approx_eq!((-3.1f64).powi(2), 9.61); | |
1464 | assert_approx_eq!(5.9f64.powi(-2), 0.028727); | |
1465 | assert_eq!(8.3f64.powi(0), 1.0); | |
1466 | assert!(nan.powi(2).is_nan()); | |
1467 | assert_eq!(inf.powi(3), inf); | |
1468 | assert_eq!(neg_inf.powi(2), inf); | |
1469 | } | |
1470 | ||
1471 | #[test] | |
1472 | fn test_powf() { | |
9346a6ac AL |
1473 | let nan: f64 = NAN; |
1474 | let inf: f64 = INFINITY; | |
1475 | let neg_inf: f64 = NEG_INFINITY; | |
c34b1796 AL |
1476 | assert_eq!(1.0f64.powf(1.0), 1.0); |
1477 | assert_approx_eq!(3.4f64.powf(4.5), 246.408183); | |
1478 | assert_approx_eq!(2.7f64.powf(-3.2), 0.041652); | |
1479 | assert_approx_eq!((-3.1f64).powf(2.0), 9.61); | |
1480 | assert_approx_eq!(5.9f64.powf(-2.0), 0.028727); | |
1481 | assert_eq!(8.3f64.powf(0.0), 1.0); | |
1482 | assert!(nan.powf(2.0).is_nan()); | |
1483 | assert_eq!(inf.powf(2.0), inf); | |
1484 | assert_eq!(neg_inf.powf(3.0), neg_inf); | |
1485 | } | |
1486 | ||
1487 | #[test] | |
1488 | fn test_sqrt_domain() { | |
1489 | assert!(NAN.sqrt().is_nan()); | |
1490 | assert!(NEG_INFINITY.sqrt().is_nan()); | |
1491 | assert!((-1.0f64).sqrt().is_nan()); | |
1492 | assert_eq!((-0.0f64).sqrt(), -0.0); | |
1493 | assert_eq!(0.0f64.sqrt(), 0.0); | |
1494 | assert_eq!(1.0f64.sqrt(), 1.0); | |
1495 | assert_eq!(INFINITY.sqrt(), INFINITY); | |
1496 | } | |
1497 | ||
c34b1796 AL |
1498 | #[test] |
1499 | fn test_exp() { | |
1500 | assert_eq!(1.0, 0.0f64.exp()); | |
1501 | assert_approx_eq!(2.718282, 1.0f64.exp()); | |
1502 | assert_approx_eq!(148.413159, 5.0f64.exp()); | |
1503 | ||
9346a6ac AL |
1504 | let inf: f64 = INFINITY; |
1505 | let neg_inf: f64 = NEG_INFINITY; | |
1506 | let nan: f64 = NAN; | |
85aaf69f SL |
1507 | assert_eq!(inf, inf.exp()); |
1508 | assert_eq!(0.0, neg_inf.exp()); | |
1509 | assert!(nan.exp().is_nan()); | |
1510 | } | |
1511 | ||
1512 | #[test] | |
1513 | fn test_exp2() { | |
1514 | assert_eq!(32.0, 5.0f64.exp2()); | |
1515 | assert_eq!(1.0, 0.0f64.exp2()); | |
1516 | ||
9346a6ac AL |
1517 | let inf: f64 = INFINITY; |
1518 | let neg_inf: f64 = NEG_INFINITY; | |
1519 | let nan: f64 = NAN; | |
85aaf69f SL |
1520 | assert_eq!(inf, inf.exp2()); |
1521 | assert_eq!(0.0, neg_inf.exp2()); | |
1522 | assert!(nan.exp2().is_nan()); | |
1523 | } | |
1524 | ||
c34b1796 AL |
1525 | #[test] |
1526 | fn test_ln() { | |
9346a6ac AL |
1527 | let nan: f64 = NAN; |
1528 | let inf: f64 = INFINITY; | |
1529 | let neg_inf: f64 = NEG_INFINITY; | |
c34b1796 AL |
1530 | assert_approx_eq!(1.0f64.exp().ln(), 1.0); |
1531 | assert!(nan.ln().is_nan()); | |
1532 | assert_eq!(inf.ln(), inf); | |
1533 | assert!(neg_inf.ln().is_nan()); | |
1534 | assert!((-2.3f64).ln().is_nan()); | |
1535 | assert_eq!((-0.0f64).ln(), neg_inf); | |
1536 | assert_eq!(0.0f64.ln(), neg_inf); | |
1537 | assert_approx_eq!(4.0f64.ln(), 1.386294); | |
1538 | } | |
1539 | ||
1540 | #[test] | |
1541 | fn test_log() { | |
9346a6ac AL |
1542 | let nan: f64 = NAN; |
1543 | let inf: f64 = INFINITY; | |
1544 | let neg_inf: f64 = NEG_INFINITY; | |
c34b1796 AL |
1545 | assert_eq!(10.0f64.log(10.0), 1.0); |
1546 | assert_approx_eq!(2.3f64.log(3.5), 0.664858); | |
9346a6ac | 1547 | assert_eq!(1.0f64.exp().log(1.0f64.exp()), 1.0); |
c34b1796 AL |
1548 | assert!(1.0f64.log(1.0).is_nan()); |
1549 | assert!(1.0f64.log(-13.9).is_nan()); | |
1550 | assert!(nan.log(2.3).is_nan()); | |
1551 | assert_eq!(inf.log(10.0), inf); | |
1552 | assert!(neg_inf.log(8.8).is_nan()); | |
1553 | assert!((-2.3f64).log(0.1).is_nan()); | |
1554 | assert_eq!((-0.0f64).log(2.0), neg_inf); | |
1555 | assert_eq!(0.0f64.log(7.0), neg_inf); | |
1556 | } | |
1557 | ||
1558 | #[test] | |
1559 | fn test_log2() { | |
9346a6ac AL |
1560 | let nan: f64 = NAN; |
1561 | let inf: f64 = INFINITY; | |
1562 | let neg_inf: f64 = NEG_INFINITY; | |
c34b1796 AL |
1563 | assert_approx_eq!(10.0f64.log2(), 3.321928); |
1564 | assert_approx_eq!(2.3f64.log2(), 1.201634); | |
1565 | assert_approx_eq!(1.0f64.exp().log2(), 1.442695); | |
1566 | assert!(nan.log2().is_nan()); | |
1567 | assert_eq!(inf.log2(), inf); | |
1568 | assert!(neg_inf.log2().is_nan()); | |
1569 | assert!((-2.3f64).log2().is_nan()); | |
1570 | assert_eq!((-0.0f64).log2(), neg_inf); | |
1571 | assert_eq!(0.0f64.log2(), neg_inf); | |
1572 | } | |
1573 | ||
1574 | #[test] | |
1575 | fn test_log10() { | |
9346a6ac AL |
1576 | let nan: f64 = NAN; |
1577 | let inf: f64 = INFINITY; | |
1578 | let neg_inf: f64 = NEG_INFINITY; | |
c34b1796 AL |
1579 | assert_eq!(10.0f64.log10(), 1.0); |
1580 | assert_approx_eq!(2.3f64.log10(), 0.361728); | |
1581 | assert_approx_eq!(1.0f64.exp().log10(), 0.434294); | |
1582 | assert_eq!(1.0f64.log10(), 0.0); | |
1583 | assert!(nan.log10().is_nan()); | |
1584 | assert_eq!(inf.log10(), inf); | |
1585 | assert!(neg_inf.log10().is_nan()); | |
1586 | assert!((-2.3f64).log10().is_nan()); | |
1587 | assert_eq!((-0.0f64).log10(), neg_inf); | |
1588 | assert_eq!(0.0f64.log10(), neg_inf); | |
1589 | } | |
1590 | ||
1591 | #[test] | |
1592 | fn test_to_degrees() { | |
1593 | let pi: f64 = consts::PI; | |
9346a6ac AL |
1594 | let nan: f64 = NAN; |
1595 | let inf: f64 = INFINITY; | |
1596 | let neg_inf: f64 = NEG_INFINITY; | |
c34b1796 AL |
1597 | assert_eq!(0.0f64.to_degrees(), 0.0); |
1598 | assert_approx_eq!((-5.8f64).to_degrees(), -332.315521); | |
1599 | assert_eq!(pi.to_degrees(), 180.0); | |
1600 | assert!(nan.to_degrees().is_nan()); | |
1601 | assert_eq!(inf.to_degrees(), inf); | |
1602 | assert_eq!(neg_inf.to_degrees(), neg_inf); | |
1603 | } | |
1604 | ||
1605 | #[test] | |
1606 | fn test_to_radians() { | |
1607 | let pi: f64 = consts::PI; | |
9346a6ac AL |
1608 | let nan: f64 = NAN; |
1609 | let inf: f64 = INFINITY; | |
1610 | let neg_inf: f64 = NEG_INFINITY; | |
c34b1796 AL |
1611 | assert_eq!(0.0f64.to_radians(), 0.0); |
1612 | assert_approx_eq!(154.6f64.to_radians(), 2.698279); | |
1613 | assert_approx_eq!((-332.31f64).to_radians(), -5.799903); | |
1614 | assert_eq!(180.0f64.to_radians(), pi); | |
1615 | assert!(nan.to_radians().is_nan()); | |
1616 | assert_eq!(inf.to_radians(), inf); | |
1617 | assert_eq!(neg_inf.to_radians(), neg_inf); | |
1618 | } | |
1619 | ||
1620 | #[test] | |
3157f602 | 1621 | #[allow(deprecated)] |
c34b1796 | 1622 | fn test_ldexp() { |
9cc50fc6 SL |
1623 | let f1 = 2.0f64.powi(-123); |
1624 | let f2 = 2.0f64.powi(-111); | |
1625 | let f3 = 1.75 * 2.0f64.powi(-12); | |
9346a6ac AL |
1626 | assert_eq!(f64::ldexp(1f64, -123), f1); |
1627 | assert_eq!(f64::ldexp(1f64, -111), f2); | |
1628 | assert_eq!(f64::ldexp(1.75f64, -12), f3); | |
c34b1796 | 1629 | |
9346a6ac AL |
1630 | assert_eq!(f64::ldexp(0f64, -123), 0f64); |
1631 | assert_eq!(f64::ldexp(-0f64, -123), -0f64); | |
c34b1796 | 1632 | |
9346a6ac AL |
1633 | let inf: f64 = INFINITY; |
1634 | let neg_inf: f64 = NEG_INFINITY; | |
1635 | let nan: f64 = NAN; | |
1636 | assert_eq!(f64::ldexp(inf, -123), inf); | |
1637 | assert_eq!(f64::ldexp(neg_inf, -123), neg_inf); | |
1638 | assert!(f64::ldexp(nan, -123).is_nan()); | |
c34b1796 AL |
1639 | } |
1640 | ||
1641 | #[test] | |
3157f602 | 1642 | #[allow(deprecated)] |
c34b1796 | 1643 | fn test_frexp() { |
9cc50fc6 SL |
1644 | let f1 = 2.0f64.powi(-123); |
1645 | let f2 = 2.0f64.powi(-111); | |
1646 | let f3 = 1.75 * 2.0f64.powi(-123); | |
c34b1796 AL |
1647 | let (x1, exp1) = f1.frexp(); |
1648 | let (x2, exp2) = f2.frexp(); | |
1649 | let (x3, exp3) = f3.frexp(); | |
1650 | assert_eq!((x1, exp1), (0.5f64, -122)); | |
1651 | assert_eq!((x2, exp2), (0.5f64, -110)); | |
1652 | assert_eq!((x3, exp3), (0.875f64, -122)); | |
9346a6ac AL |
1653 | assert_eq!(f64::ldexp(x1, exp1), f1); |
1654 | assert_eq!(f64::ldexp(x2, exp2), f2); | |
1655 | assert_eq!(f64::ldexp(x3, exp3), f3); | |
c34b1796 AL |
1656 | |
1657 | assert_eq!(0f64.frexp(), (0f64, 0)); | |
1658 | assert_eq!((-0f64).frexp(), (-0f64, 0)); | |
1659 | } | |
1660 | ||
1661 | #[test] #[cfg_attr(windows, ignore)] // FIXME #8755 | |
3157f602 | 1662 | #[allow(deprecated)] |
c34b1796 | 1663 | fn test_frexp_nowin() { |
9346a6ac AL |
1664 | let inf: f64 = INFINITY; |
1665 | let neg_inf: f64 = NEG_INFINITY; | |
1666 | let nan: f64 = NAN; | |
c34b1796 AL |
1667 | assert_eq!(match inf.frexp() { (x, _) => x }, inf); |
1668 | assert_eq!(match neg_inf.frexp() { (x, _) => x }, neg_inf); | |
1669 | assert!(match nan.frexp() { (x, _) => x.is_nan() }) | |
1670 | } | |
1671 | ||
970d7e83 LB |
1672 | #[test] |
1673 | fn test_asinh() { | |
1674 | assert_eq!(0.0f64.asinh(), 0.0f64); | |
1675 | assert_eq!((-0.0f64).asinh(), -0.0f64); | |
1a4d82fc | 1676 | |
9346a6ac AL |
1677 | let inf: f64 = INFINITY; |
1678 | let neg_inf: f64 = NEG_INFINITY; | |
1679 | let nan: f64 = NAN; | |
1a4d82fc JJ |
1680 | assert_eq!(inf.asinh(), inf); |
1681 | assert_eq!(neg_inf.asinh(), neg_inf); | |
1682 | assert!(nan.asinh().is_nan()); | |
970d7e83 LB |
1683 | assert_approx_eq!(2.0f64.asinh(), 1.443635475178810342493276740273105f64); |
1684 | assert_approx_eq!((-2.0f64).asinh(), -1.443635475178810342493276740273105f64); | |
1685 | } | |
1686 | ||
1687 | #[test] | |
1688 | fn test_acosh() { | |
1689 | assert_eq!(1.0f64.acosh(), 0.0f64); | |
1a4d82fc JJ |
1690 | assert!(0.999f64.acosh().is_nan()); |
1691 | ||
9346a6ac AL |
1692 | let inf: f64 = INFINITY; |
1693 | let neg_inf: f64 = NEG_INFINITY; | |
1694 | let nan: f64 = NAN; | |
1a4d82fc JJ |
1695 | assert_eq!(inf.acosh(), inf); |
1696 | assert!(neg_inf.acosh().is_nan()); | |
1697 | assert!(nan.acosh().is_nan()); | |
970d7e83 LB |
1698 | assert_approx_eq!(2.0f64.acosh(), 1.31695789692481670862504634730796844f64); |
1699 | assert_approx_eq!(3.0f64.acosh(), 1.76274717403908605046521864995958461f64); | |
1700 | } | |
1701 | ||
1702 | #[test] | |
1703 | fn test_atanh() { | |
1704 | assert_eq!(0.0f64.atanh(), 0.0f64); | |
1705 | assert_eq!((-0.0f64).atanh(), -0.0f64); | |
1a4d82fc | 1706 | |
9346a6ac AL |
1707 | let inf: f64 = INFINITY; |
1708 | let neg_inf: f64 = NEG_INFINITY; | |
1709 | let nan: f64 = NAN; | |
1a4d82fc JJ |
1710 | assert_eq!(1.0f64.atanh(), inf); |
1711 | assert_eq!((-1.0f64).atanh(), neg_inf); | |
1712 | assert!(2f64.atanh().atanh().is_nan()); | |
1713 | assert!((-2f64).atanh().atanh().is_nan()); | |
1714 | assert!(inf.atanh().is_nan()); | |
1715 | assert!(neg_inf.atanh().is_nan()); | |
1716 | assert!(nan.atanh().is_nan()); | |
970d7e83 LB |
1717 | assert_approx_eq!(0.5f64.atanh(), 0.54930614433405484569762261846126285f64); |
1718 | assert_approx_eq!((-0.5f64).atanh(), -0.54930614433405484569762261846126285f64); | |
1719 | } | |
1720 | ||
1721 | #[test] | |
1722 | fn test_real_consts() { | |
1a4d82fc JJ |
1723 | use super::consts; |
1724 | let pi: f64 = consts::PI; | |
1a4d82fc JJ |
1725 | let frac_pi_2: f64 = consts::FRAC_PI_2; |
1726 | let frac_pi_3: f64 = consts::FRAC_PI_3; | |
1727 | let frac_pi_4: f64 = consts::FRAC_PI_4; | |
1728 | let frac_pi_6: f64 = consts::FRAC_PI_6; | |
1729 | let frac_pi_8: f64 = consts::FRAC_PI_8; | |
1730 | let frac_1_pi: f64 = consts::FRAC_1_PI; | |
1731 | let frac_2_pi: f64 = consts::FRAC_2_PI; | |
9346a6ac AL |
1732 | let frac_2_sqrtpi: f64 = consts::FRAC_2_SQRT_PI; |
1733 | let sqrt2: f64 = consts::SQRT_2; | |
1734 | let frac_1_sqrt2: f64 = consts::FRAC_1_SQRT_2; | |
1a4d82fc JJ |
1735 | let e: f64 = consts::E; |
1736 | let log2_e: f64 = consts::LOG2_E; | |
1737 | let log10_e: f64 = consts::LOG10_E; | |
1738 | let ln_2: f64 = consts::LN_2; | |
1739 | let ln_10: f64 = consts::LN_10; | |
1740 | ||
1a4d82fc JJ |
1741 | assert_approx_eq!(frac_pi_2, pi / 2f64); |
1742 | assert_approx_eq!(frac_pi_3, pi / 3f64); | |
1743 | assert_approx_eq!(frac_pi_4, pi / 4f64); | |
1744 | assert_approx_eq!(frac_pi_6, pi / 6f64); | |
1745 | assert_approx_eq!(frac_pi_8, pi / 8f64); | |
1746 | assert_approx_eq!(frac_1_pi, 1f64 / pi); | |
1747 | assert_approx_eq!(frac_2_pi, 2f64 / pi); | |
1748 | assert_approx_eq!(frac_2_sqrtpi, 2f64 / pi.sqrt()); | |
1749 | assert_approx_eq!(sqrt2, 2f64.sqrt()); | |
1750 | assert_approx_eq!(frac_1_sqrt2, 1f64 / 2f64.sqrt()); | |
1751 | assert_approx_eq!(log2_e, e.log2()); | |
1752 | assert_approx_eq!(log10_e, e.log10()); | |
1753 | assert_approx_eq!(ln_2, 2f64.ln()); | |
1754 | assert_approx_eq!(ln_10, 10f64.ln()); | |
970d7e83 | 1755 | } |
970d7e83 | 1756 | } |