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