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