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