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