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