1 | //! Constants for the `f16` half-precision floating point type. |
2 | //! |
3 | //! *[See also the `f16` primitive type](primitive@f16).* |
4 | //! |
5 | //! Mathematically significant numbers are provided in the `consts` sub-module. |
6 | |
7 | #[unstable (feature = "f16" , issue = "116909" )] |
8 | pub use core::f16::consts; |
9 | |
10 | #[cfg (not(test))] |
11 | use crate::intrinsics; |
12 | #[cfg (not(test))] |
13 | use crate::sys::cmath; |
14 | |
15 | #[cfg (not(test))] |
16 | impl f16 { |
17 | /// Returns the largest integer less than or equal to `self`. |
18 | /// |
19 | /// This function always returns the precise result. |
20 | /// |
21 | /// # Examples |
22 | /// |
23 | /// ``` |
24 | /// #![feature(f16)] |
25 | /// # #[cfg (reliable_f16_math)] { |
26 | /// |
27 | /// let f = 3.7_f16; |
28 | /// let g = 3.0_f16; |
29 | /// let h = -3.7_f16; |
30 | /// |
31 | /// assert_eq!(f.floor(), 3.0); |
32 | /// assert_eq!(g.floor(), 3.0); |
33 | /// assert_eq!(h.floor(), -4.0); |
34 | /// # } |
35 | /// ``` |
36 | #[inline ] |
37 | #[rustc_allow_incoherent_impl ] |
38 | #[unstable (feature = "f16" , issue = "116909" )] |
39 | #[must_use = "method returns a new number and does not mutate the original value" ] |
40 | pub fn floor(self) -> f16 { |
41 | unsafe { intrinsics::floorf16(self) } |
42 | } |
43 | |
44 | /// Returns the smallest integer greater than or equal to `self`. |
45 | /// |
46 | /// This function always returns the precise result. |
47 | /// |
48 | /// # Examples |
49 | /// |
50 | /// ``` |
51 | /// #![feature(f16)] |
52 | /// # #[cfg (reliable_f16_math)] { |
53 | /// |
54 | /// let f = 3.01_f16; |
55 | /// let g = 4.0_f16; |
56 | /// |
57 | /// assert_eq!(f.ceil(), 4.0); |
58 | /// assert_eq!(g.ceil(), 4.0); |
59 | /// # } |
60 | /// ``` |
61 | #[inline ] |
62 | #[doc (alias = "ceiling" )] |
63 | #[rustc_allow_incoherent_impl ] |
64 | #[unstable (feature = "f16" , issue = "116909" )] |
65 | #[must_use = "method returns a new number and does not mutate the original value" ] |
66 | pub fn ceil(self) -> f16 { |
67 | unsafe { intrinsics::ceilf16(self) } |
68 | } |
69 | |
70 | /// Returns the nearest integer to `self`. If a value is half-way between two |
71 | /// integers, round away from `0.0`. |
72 | /// |
73 | /// This function always returns the precise result. |
74 | /// |
75 | /// # Examples |
76 | /// |
77 | /// ``` |
78 | /// #![feature(f16)] |
79 | /// # #[cfg (reliable_f16_math)] { |
80 | /// |
81 | /// let f = 3.3_f16; |
82 | /// let g = -3.3_f16; |
83 | /// let h = -3.7_f16; |
84 | /// let i = 3.5_f16; |
85 | /// let j = 4.5_f16; |
86 | /// |
87 | /// assert_eq!(f.round(), 3.0); |
88 | /// assert_eq!(g.round(), -3.0); |
89 | /// assert_eq!(h.round(), -4.0); |
90 | /// assert_eq!(i.round(), 4.0); |
91 | /// assert_eq!(j.round(), 5.0); |
92 | /// # } |
93 | /// ``` |
94 | #[inline ] |
95 | #[rustc_allow_incoherent_impl ] |
96 | #[unstable (feature = "f16" , issue = "116909" )] |
97 | #[must_use = "method returns a new number and does not mutate the original value" ] |
98 | pub fn round(self) -> f16 { |
99 | unsafe { intrinsics::roundf16(self) } |
100 | } |
101 | |
102 | /// Returns the nearest integer to a number. Rounds half-way cases to the number |
103 | /// with an even least significant digit. |
104 | /// |
105 | /// This function always returns the precise result. |
106 | /// |
107 | /// # Examples |
108 | /// |
109 | /// ``` |
110 | /// #![feature(f16)] |
111 | /// # #[cfg (reliable_f16_math)] { |
112 | /// |
113 | /// let f = 3.3_f16; |
114 | /// let g = -3.3_f16; |
115 | /// let h = 3.5_f16; |
116 | /// let i = 4.5_f16; |
117 | /// |
118 | /// assert_eq!(f.round_ties_even(), 3.0); |
119 | /// assert_eq!(g.round_ties_even(), -3.0); |
120 | /// assert_eq!(h.round_ties_even(), 4.0); |
121 | /// assert_eq!(i.round_ties_even(), 4.0); |
122 | /// # } |
123 | /// ``` |
124 | #[inline ] |
125 | #[rustc_allow_incoherent_impl ] |
126 | #[unstable (feature = "f16" , issue = "116909" )] |
127 | #[must_use = "method returns a new number and does not mutate the original value" ] |
128 | pub fn round_ties_even(self) -> f16 { |
129 | intrinsics::round_ties_even_f16(self) |
130 | } |
131 | |
132 | /// Returns the integer part of `self`. |
133 | /// This means that non-integer numbers are always truncated towards zero. |
134 | /// |
135 | /// This function always returns the precise result. |
136 | /// |
137 | /// # Examples |
138 | /// |
139 | /// ``` |
140 | /// #![feature(f16)] |
141 | /// # #[cfg (reliable_f16_math)] { |
142 | /// |
143 | /// let f = 3.7_f16; |
144 | /// let g = 3.0_f16; |
145 | /// let h = -3.7_f16; |
146 | /// |
147 | /// assert_eq!(f.trunc(), 3.0); |
148 | /// assert_eq!(g.trunc(), 3.0); |
149 | /// assert_eq!(h.trunc(), -3.0); |
150 | /// # } |
151 | /// ``` |
152 | #[inline ] |
153 | #[doc (alias = "truncate" )] |
154 | #[rustc_allow_incoherent_impl ] |
155 | #[unstable (feature = "f16" , issue = "116909" )] |
156 | #[must_use = "method returns a new number and does not mutate the original value" ] |
157 | pub fn trunc(self) -> f16 { |
158 | unsafe { intrinsics::truncf16(self) } |
159 | } |
160 | |
161 | /// Returns the fractional part of `self`. |
162 | /// |
163 | /// This function always returns the precise result. |
164 | /// |
165 | /// # Examples |
166 | /// |
167 | /// ``` |
168 | /// #![feature(f16)] |
169 | /// # #[cfg (reliable_f16_math)] { |
170 | /// |
171 | /// let x = 3.6_f16; |
172 | /// let y = -3.6_f16; |
173 | /// let abs_difference_x = (x.fract() - 0.6).abs(); |
174 | /// let abs_difference_y = (y.fract() - (-0.6)).abs(); |
175 | /// |
176 | /// assert!(abs_difference_x <= f16::EPSILON); |
177 | /// assert!(abs_difference_y <= f16::EPSILON); |
178 | /// # } |
179 | /// ``` |
180 | #[inline ] |
181 | #[rustc_allow_incoherent_impl ] |
182 | #[unstable (feature = "f16" , issue = "116909" )] |
183 | #[must_use = "method returns a new number and does not mutate the original value" ] |
184 | pub fn fract(self) -> f16 { |
185 | self - self.trunc() |
186 | } |
187 | |
188 | /// Fused multiply-add. Computes `(self * a) + b` with only one rounding |
189 | /// error, yielding a more accurate result than an unfused multiply-add. |
190 | /// |
191 | /// Using `mul_add` *may* be more performant than an unfused multiply-add if |
192 | /// the target architecture has a dedicated `fma` CPU instruction. However, |
193 | /// this is not always true, and will be heavily dependant on designing |
194 | /// algorithms with specific target hardware in mind. |
195 | /// |
196 | /// # Precision |
197 | /// |
198 | /// The result of this operation is guaranteed to be the rounded |
199 | /// infinite-precision result. It is specified by IEEE 754 as |
200 | /// `fusedMultiplyAdd` and guaranteed not to change. |
201 | /// |
202 | /// # Examples |
203 | /// |
204 | /// ``` |
205 | /// #![feature(f16)] |
206 | /// # #[cfg (reliable_f16_math)] { |
207 | /// |
208 | /// let m = 10.0_f16; |
209 | /// let x = 4.0_f16; |
210 | /// let b = 60.0_f16; |
211 | /// |
212 | /// assert_eq!(m.mul_add(x, b), 100.0); |
213 | /// assert_eq!(m * x + b, 100.0); |
214 | /// |
215 | /// let one_plus_eps = 1.0_f16 + f16::EPSILON; |
216 | /// let one_minus_eps = 1.0_f16 - f16::EPSILON; |
217 | /// let minus_one = -1.0_f16; |
218 | /// |
219 | /// // The exact result (1 + eps) * (1 - eps) = 1 - eps * eps. |
220 | /// assert_eq!(one_plus_eps.mul_add(one_minus_eps, minus_one), -f16::EPSILON * f16::EPSILON); |
221 | /// // Different rounding with the non-fused multiply and add. |
222 | /// assert_eq!(one_plus_eps * one_minus_eps + minus_one, 0.0); |
223 | /// # } |
224 | /// ``` |
225 | #[inline ] |
226 | #[rustc_allow_incoherent_impl ] |
227 | #[unstable (feature = "f16" , issue = "116909" )] |
228 | #[doc (alias = "fmaf16" , alias = "fusedMultiplyAdd" )] |
229 | #[must_use = "method returns a new number and does not mutate the original value" ] |
230 | pub fn mul_add(self, a: f16, b: f16) -> f16 { |
231 | unsafe { intrinsics::fmaf16(self, a, b) } |
232 | } |
233 | |
234 | /// Calculates Euclidean division, the matching method for `rem_euclid`. |
235 | /// |
236 | /// This computes the integer `n` such that |
237 | /// `self = n * rhs + self.rem_euclid(rhs)`. |
238 | /// In other words, the result is `self / rhs` rounded to the integer `n` |
239 | /// such that `self >= n * rhs`. |
240 | /// |
241 | /// # Precision |
242 | /// |
243 | /// The result of this operation is guaranteed to be the rounded |
244 | /// infinite-precision result. |
245 | /// |
246 | /// # Examples |
247 | /// |
248 | /// ``` |
249 | /// #![feature(f16)] |
250 | /// # #[cfg (reliable_f16_math)] { |
251 | /// |
252 | /// let a: f16 = 7.0; |
253 | /// let b = 4.0; |
254 | /// assert_eq!(a.div_euclid(b), 1.0); // 7.0 > 4.0 * 1.0 |
255 | /// assert_eq!((-a).div_euclid(b), -2.0); // -7.0 >= 4.0 * -2.0 |
256 | /// assert_eq!(a.div_euclid(-b), -1.0); // 7.0 >= -4.0 * -1.0 |
257 | /// assert_eq!((-a).div_euclid(-b), 2.0); // -7.0 >= -4.0 * 2.0 |
258 | /// # } |
259 | /// ``` |
260 | #[inline ] |
261 | #[rustc_allow_incoherent_impl ] |
262 | #[unstable (feature = "f16" , issue = "116909" )] |
263 | #[must_use = "method returns a new number and does not mutate the original value" ] |
264 | pub fn div_euclid(self, rhs: f16) -> f16 { |
265 | let q = (self / rhs).trunc(); |
266 | if self % rhs < 0.0 { |
267 | return if rhs > 0.0 { q - 1.0 } else { q + 1.0 }; |
268 | } |
269 | q |
270 | } |
271 | |
272 | /// Calculates the least nonnegative remainder of `self (mod rhs)`. |
273 | /// |
274 | /// In particular, the return value `r` satisfies `0.0 <= r < rhs.abs()` in |
275 | /// most cases. However, due to a floating point round-off error it can |
276 | /// result in `r == rhs.abs()`, violating the mathematical definition, if |
277 | /// `self` is much smaller than `rhs.abs()` in magnitude and `self < 0.0`. |
278 | /// This result is not an element of the function's codomain, but it is the |
279 | /// closest floating point number in the real numbers and thus fulfills the |
280 | /// property `self == self.div_euclid(rhs) * rhs + self.rem_euclid(rhs)` |
281 | /// approximately. |
282 | /// |
283 | /// # Precision |
284 | /// |
285 | /// The result of this operation is guaranteed to be the rounded |
286 | /// infinite-precision result. |
287 | /// |
288 | /// # Examples |
289 | /// |
290 | /// ``` |
291 | /// #![feature(f16)] |
292 | /// # #[cfg (reliable_f16_math)] { |
293 | /// |
294 | /// let a: f16 = 7.0; |
295 | /// let b = 4.0; |
296 | /// assert_eq!(a.rem_euclid(b), 3.0); |
297 | /// assert_eq!((-a).rem_euclid(b), 1.0); |
298 | /// assert_eq!(a.rem_euclid(-b), 3.0); |
299 | /// assert_eq!((-a).rem_euclid(-b), 1.0); |
300 | /// // limitation due to round-off error |
301 | /// assert!((-f16::EPSILON).rem_euclid(3.0) != 0.0); |
302 | /// # } |
303 | /// ``` |
304 | #[inline ] |
305 | #[rustc_allow_incoherent_impl ] |
306 | #[doc (alias = "modulo" , alias = "mod" )] |
307 | #[unstable (feature = "f16" , issue = "116909" )] |
308 | #[must_use = "method returns a new number and does not mutate the original value" ] |
309 | pub fn rem_euclid(self, rhs: f16) -> f16 { |
310 | let r = self % rhs; |
311 | if r < 0.0 { r + rhs.abs() } else { r } |
312 | } |
313 | |
314 | /// Raises a number to an integer power. |
315 | /// |
316 | /// Using this function is generally faster than using `powf`. |
317 | /// It might have a different sequence of rounding operations than `powf`, |
318 | /// so the results are not guaranteed to agree. |
319 | /// |
320 | /// # Unspecified precision |
321 | /// |
322 | /// The precision of this function is non-deterministic. This means it varies by platform, |
323 | /// Rust version, and can even differ within the same execution from one invocation to the next. |
324 | /// |
325 | /// # Examples |
326 | /// |
327 | /// ``` |
328 | /// #![feature(f16)] |
329 | /// # #[cfg (reliable_f16_math)] { |
330 | /// |
331 | /// let x = 2.0_f16; |
332 | /// let abs_difference = (x.powi(2) - (x * x)).abs(); |
333 | /// assert!(abs_difference <= f16::EPSILON); |
334 | /// |
335 | /// assert_eq!(f16::powi(f16::NAN, 0), 1.0); |
336 | /// # } |
337 | /// ``` |
338 | #[inline ] |
339 | #[rustc_allow_incoherent_impl ] |
340 | #[unstable (feature = "f16" , issue = "116909" )] |
341 | #[must_use = "method returns a new number and does not mutate the original value" ] |
342 | pub fn powi(self, n: i32) -> f16 { |
343 | unsafe { intrinsics::powif16(self, n) } |
344 | } |
345 | |
346 | /// Raises a number to a floating point power. |
347 | /// |
348 | /// # Unspecified precision |
349 | /// |
350 | /// The precision of this function is non-deterministic. This means it varies by platform, |
351 | /// Rust version, and can even differ within the same execution from one invocation to the next. |
352 | /// |
353 | /// # Examples |
354 | /// |
355 | /// ``` |
356 | /// #![feature(f16)] |
357 | /// # #[cfg (reliable_f16_math)] { |
358 | /// |
359 | /// let x = 2.0_f16; |
360 | /// let abs_difference = (x.powf(2.0) - (x * x)).abs(); |
361 | /// assert!(abs_difference <= f16::EPSILON); |
362 | /// |
363 | /// assert_eq!(f16::powf(1.0, f16::NAN), 1.0); |
364 | /// assert_eq!(f16::powf(f16::NAN, 0.0), 1.0); |
365 | /// # } |
366 | /// ``` |
367 | #[inline ] |
368 | #[rustc_allow_incoherent_impl ] |
369 | #[unstable (feature = "f16" , issue = "116909" )] |
370 | #[must_use = "method returns a new number and does not mutate the original value" ] |
371 | pub fn powf(self, n: f16) -> f16 { |
372 | unsafe { intrinsics::powf16(self, n) } |
373 | } |
374 | |
375 | /// Returns the square root of a number. |
376 | /// |
377 | /// Returns NaN if `self` is a negative number other than `-0.0`. |
378 | /// |
379 | /// # Precision |
380 | /// |
381 | /// The result of this operation is guaranteed to be the rounded |
382 | /// infinite-precision result. It is specified by IEEE 754 as `squareRoot` |
383 | /// and guaranteed not to change. |
384 | /// |
385 | /// # Examples |
386 | /// |
387 | /// ``` |
388 | /// #![feature(f16)] |
389 | /// # #[cfg (reliable_f16_math)] { |
390 | /// |
391 | /// let positive = 4.0_f16; |
392 | /// let negative = -4.0_f16; |
393 | /// let negative_zero = -0.0_f16; |
394 | /// |
395 | /// assert_eq!(positive.sqrt(), 2.0); |
396 | /// assert!(negative.sqrt().is_nan()); |
397 | /// assert!(negative_zero.sqrt() == negative_zero); |
398 | /// # } |
399 | /// ``` |
400 | #[inline ] |
401 | #[doc (alias = "squareRoot" )] |
402 | #[rustc_allow_incoherent_impl ] |
403 | #[unstable (feature = "f16" , issue = "116909" )] |
404 | #[must_use = "method returns a new number and does not mutate the original value" ] |
405 | pub fn sqrt(self) -> f16 { |
406 | unsafe { intrinsics::sqrtf16(self) } |
407 | } |
408 | |
409 | /// Returns `e^(self)`, (the exponential function). |
410 | /// |
411 | /// # Unspecified precision |
412 | /// |
413 | /// The precision of this function is non-deterministic. This means it varies by platform, |
414 | /// Rust version, and can even differ within the same execution from one invocation to the next. |
415 | /// |
416 | /// # Examples |
417 | /// |
418 | /// ``` |
419 | /// #![feature(f16)] |
420 | /// # #[cfg (reliable_f16_math)] { |
421 | /// |
422 | /// let one = 1.0f16; |
423 | /// // e^1 |
424 | /// let e = one.exp(); |
425 | /// |
426 | /// // ln(e) - 1 == 0 |
427 | /// let abs_difference = (e.ln() - 1.0).abs(); |
428 | /// |
429 | /// assert!(abs_difference <= f16::EPSILON); |
430 | /// # } |
431 | /// ``` |
432 | #[inline ] |
433 | #[rustc_allow_incoherent_impl ] |
434 | #[unstable (feature = "f16" , issue = "116909" )] |
435 | #[must_use = "method returns a new number and does not mutate the original value" ] |
436 | pub fn exp(self) -> f16 { |
437 | unsafe { intrinsics::expf16(self) } |
438 | } |
439 | |
440 | /// Returns `2^(self)`. |
441 | /// |
442 | /// # Unspecified precision |
443 | /// |
444 | /// The precision of this function is non-deterministic. This means it varies by platform, |
445 | /// Rust version, and can even differ within the same execution from one invocation to the next. |
446 | /// |
447 | /// # Examples |
448 | /// |
449 | /// ``` |
450 | /// #![feature(f16)] |
451 | /// # #[cfg (reliable_f16_math)] { |
452 | /// |
453 | /// let f = 2.0f16; |
454 | /// |
455 | /// // 2^2 - 4 == 0 |
456 | /// let abs_difference = (f.exp2() - 4.0).abs(); |
457 | /// |
458 | /// assert!(abs_difference <= f16::EPSILON); |
459 | /// # } |
460 | /// ``` |
461 | #[inline ] |
462 | #[rustc_allow_incoherent_impl ] |
463 | #[unstable (feature = "f16" , issue = "116909" )] |
464 | #[must_use = "method returns a new number and does not mutate the original value" ] |
465 | pub fn exp2(self) -> f16 { |
466 | unsafe { intrinsics::exp2f16(self) } |
467 | } |
468 | |
469 | /// Returns the natural logarithm of the number. |
470 | /// |
471 | /// This returns NaN when the number is negative, and negative infinity when number is zero. |
472 | /// |
473 | /// # Unspecified precision |
474 | /// |
475 | /// The precision of this function is non-deterministic. This means it varies by platform, |
476 | /// Rust version, and can even differ within the same execution from one invocation to the next. |
477 | /// |
478 | /// # Examples |
479 | /// |
480 | /// ``` |
481 | /// #![feature(f16)] |
482 | /// # #[cfg (reliable_f16_math)] { |
483 | /// |
484 | /// let one = 1.0f16; |
485 | /// // e^1 |
486 | /// let e = one.exp(); |
487 | /// |
488 | /// // ln(e) - 1 == 0 |
489 | /// let abs_difference = (e.ln() - 1.0).abs(); |
490 | /// |
491 | /// assert!(abs_difference <= f16::EPSILON); |
492 | /// # } |
493 | /// ``` |
494 | /// |
495 | /// Non-positive values: |
496 | /// ``` |
497 | /// #![feature(f16)] |
498 | /// # #[cfg (reliable_f16_math)] { |
499 | /// |
500 | /// assert_eq!(0_f16.ln(), f16::NEG_INFINITY); |
501 | /// assert!((-42_f16).ln().is_nan()); |
502 | /// # } |
503 | /// ``` |
504 | #[inline ] |
505 | #[rustc_allow_incoherent_impl ] |
506 | #[unstable (feature = "f16" , issue = "116909" )] |
507 | #[must_use = "method returns a new number and does not mutate the original value" ] |
508 | pub fn ln(self) -> f16 { |
509 | unsafe { intrinsics::logf16(self) } |
510 | } |
511 | |
512 | /// Returns the logarithm of the number with respect to an arbitrary base. |
513 | /// |
514 | /// This returns NaN when the number is negative, and negative infinity when number is zero. |
515 | /// |
516 | /// The result might not be correctly rounded owing to implementation details; |
517 | /// `self.log2()` can produce more accurate results for base 2, and |
518 | /// `self.log10()` can produce more accurate results for base 10. |
519 | /// |
520 | /// # Unspecified precision |
521 | /// |
522 | /// The precision of this function is non-deterministic. This means it varies by platform, |
523 | /// Rust version, and can even differ within the same execution from one invocation to the next. |
524 | /// |
525 | /// # Examples |
526 | /// |
527 | /// ``` |
528 | /// #![feature(f16)] |
529 | /// # #[cfg (reliable_f16_math)] { |
530 | /// |
531 | /// let five = 5.0f16; |
532 | /// |
533 | /// // log5(5) - 1 == 0 |
534 | /// let abs_difference = (five.log(5.0) - 1.0).abs(); |
535 | /// |
536 | /// assert!(abs_difference <= f16::EPSILON); |
537 | /// # } |
538 | /// ``` |
539 | /// |
540 | /// Non-positive values: |
541 | /// ``` |
542 | /// #![feature(f16)] |
543 | /// # #[cfg (reliable_f16_math)] { |
544 | /// |
545 | /// assert_eq!(0_f16.log(10.0), f16::NEG_INFINITY); |
546 | /// assert!((-42_f16).log(10.0).is_nan()); |
547 | /// # } |
548 | /// ``` |
549 | #[inline ] |
550 | #[rustc_allow_incoherent_impl ] |
551 | #[unstable (feature = "f16" , issue = "116909" )] |
552 | #[must_use = "method returns a new number and does not mutate the original value" ] |
553 | pub fn log(self, base: f16) -> f16 { |
554 | self.ln() / base.ln() |
555 | } |
556 | |
557 | /// Returns the base 2 logarithm of the number. |
558 | /// |
559 | /// This returns NaN when the number is negative, and negative infinity when number is zero. |
560 | /// |
561 | /// # Unspecified precision |
562 | /// |
563 | /// The precision of this function is non-deterministic. This means it varies by platform, |
564 | /// Rust version, and can even differ within the same execution from one invocation to the next. |
565 | /// |
566 | /// # Examples |
567 | /// |
568 | /// ``` |
569 | /// #![feature(f16)] |
570 | /// # #[cfg (reliable_f16_math)] { |
571 | /// |
572 | /// let two = 2.0f16; |
573 | /// |
574 | /// // log2(2) - 1 == 0 |
575 | /// let abs_difference = (two.log2() - 1.0).abs(); |
576 | /// |
577 | /// assert!(abs_difference <= f16::EPSILON); |
578 | /// # } |
579 | /// ``` |
580 | /// |
581 | /// Non-positive values: |
582 | /// ``` |
583 | /// #![feature(f16)] |
584 | /// # #[cfg (reliable_f16_math)] { |
585 | /// |
586 | /// assert_eq!(0_f16.log2(), f16::NEG_INFINITY); |
587 | /// assert!((-42_f16).log2().is_nan()); |
588 | /// # } |
589 | /// ``` |
590 | #[inline ] |
591 | #[rustc_allow_incoherent_impl ] |
592 | #[unstable (feature = "f16" , issue = "116909" )] |
593 | #[must_use = "method returns a new number and does not mutate the original value" ] |
594 | pub fn log2(self) -> f16 { |
595 | unsafe { intrinsics::log2f16(self) } |
596 | } |
597 | |
598 | /// Returns the base 10 logarithm of the number. |
599 | /// |
600 | /// This returns NaN when the number is negative, and negative infinity when number is zero. |
601 | /// |
602 | /// # Unspecified precision |
603 | /// |
604 | /// The precision of this function is non-deterministic. This means it varies by platform, |
605 | /// Rust version, and can even differ within the same execution from one invocation to the next. |
606 | /// |
607 | /// # Examples |
608 | /// |
609 | /// ``` |
610 | /// #![feature(f16)] |
611 | /// # #[cfg (reliable_f16_math)] { |
612 | /// |
613 | /// let ten = 10.0f16; |
614 | /// |
615 | /// // log10(10) - 1 == 0 |
616 | /// let abs_difference = (ten.log10() - 1.0).abs(); |
617 | /// |
618 | /// assert!(abs_difference <= f16::EPSILON); |
619 | /// # } |
620 | /// ``` |
621 | /// |
622 | /// Non-positive values: |
623 | /// ``` |
624 | /// #![feature(f16)] |
625 | /// # #[cfg (reliable_f16_math)] { |
626 | /// |
627 | /// assert_eq!(0_f16.log10(), f16::NEG_INFINITY); |
628 | /// assert!((-42_f16).log10().is_nan()); |
629 | /// # } |
630 | /// ``` |
631 | #[inline ] |
632 | #[rustc_allow_incoherent_impl ] |
633 | #[unstable (feature = "f16" , issue = "116909" )] |
634 | #[must_use = "method returns a new number and does not mutate the original value" ] |
635 | pub fn log10(self) -> f16 { |
636 | unsafe { intrinsics::log10f16(self) } |
637 | } |
638 | |
639 | /// Returns the cube root of a number. |
640 | /// |
641 | /// # Unspecified precision |
642 | /// |
643 | /// The precision of this function is non-deterministic. This means it varies by platform, |
644 | /// Rust version, and can even differ within the same execution from one invocation to the next. |
645 | /// |
646 | /// This function currently corresponds to the `cbrtf` from libc on Unix |
647 | /// and Windows. Note that this might change in the future. |
648 | /// |
649 | /// # Examples |
650 | /// |
651 | /// ``` |
652 | /// #![feature(f16)] |
653 | /// # #[cfg (reliable_f16_math)] { |
654 | /// |
655 | /// let x = 8.0f16; |
656 | /// |
657 | /// // x^(1/3) - 2 == 0 |
658 | /// let abs_difference = (x.cbrt() - 2.0).abs(); |
659 | /// |
660 | /// assert!(abs_difference <= f16::EPSILON); |
661 | /// # } |
662 | /// ``` |
663 | #[inline ] |
664 | #[rustc_allow_incoherent_impl ] |
665 | #[unstable (feature = "f16" , issue = "116909" )] |
666 | #[must_use = "method returns a new number and does not mutate the original value" ] |
667 | pub fn cbrt(self) -> f16 { |
668 | (unsafe { cmath::cbrtf(self as f32) }) as f16 |
669 | } |
670 | |
671 | /// Compute the distance between the origin and a point (`x`, `y`) on the |
672 | /// Euclidean plane. Equivalently, compute the length of the hypotenuse of a |
673 | /// right-angle triangle with other sides having length `x.abs()` and |
674 | /// `y.abs()`. |
675 | /// |
676 | /// # Unspecified precision |
677 | /// |
678 | /// The precision of this function is non-deterministic. This means it varies by platform, |
679 | /// Rust version, and can even differ within the same execution from one invocation to the next. |
680 | /// |
681 | /// This function currently corresponds to the `hypotf` from libc on Unix |
682 | /// and Windows. Note that this might change in the future. |
683 | /// |
684 | /// # Examples |
685 | /// |
686 | /// ``` |
687 | /// #![feature(f16)] |
688 | /// # #[cfg (reliable_f16_math)] { |
689 | /// |
690 | /// let x = 2.0f16; |
691 | /// let y = 3.0f16; |
692 | /// |
693 | /// // sqrt(x^2 + y^2) |
694 | /// let abs_difference = (x.hypot(y) - (x.powi(2) + y.powi(2)).sqrt()).abs(); |
695 | /// |
696 | /// assert!(abs_difference <= f16::EPSILON); |
697 | /// # } |
698 | /// ``` |
699 | #[inline ] |
700 | #[rustc_allow_incoherent_impl ] |
701 | #[unstable (feature = "f16" , issue = "116909" )] |
702 | #[must_use = "method returns a new number and does not mutate the original value" ] |
703 | pub fn hypot(self, other: f16) -> f16 { |
704 | (unsafe { cmath::hypotf(self as f32, other as f32) }) as f16 |
705 | } |
706 | |
707 | /// Computes the sine of a number (in radians). |
708 | /// |
709 | /// # Unspecified precision |
710 | /// |
711 | /// The precision of this function is non-deterministic. This means it varies by platform, |
712 | /// Rust version, and can even differ within the same execution from one invocation to the next. |
713 | /// |
714 | /// # Examples |
715 | /// |
716 | /// ``` |
717 | /// #![feature(f16)] |
718 | /// # #[cfg (reliable_f16_math)] { |
719 | /// |
720 | /// let x = std::f16::consts::FRAC_PI_2; |
721 | /// |
722 | /// let abs_difference = (x.sin() - 1.0).abs(); |
723 | /// |
724 | /// assert!(abs_difference <= f16::EPSILON); |
725 | /// # } |
726 | /// ``` |
727 | #[inline ] |
728 | #[rustc_allow_incoherent_impl ] |
729 | #[unstable (feature = "f16" , issue = "116909" )] |
730 | #[must_use = "method returns a new number and does not mutate the original value" ] |
731 | pub fn sin(self) -> f16 { |
732 | unsafe { intrinsics::sinf16(self) } |
733 | } |
734 | |
735 | /// Computes the cosine of a number (in radians). |
736 | /// |
737 | /// # Unspecified precision |
738 | /// |
739 | /// The precision of this function is non-deterministic. This means it varies by platform, |
740 | /// Rust version, and can even differ within the same execution from one invocation to the next. |
741 | /// |
742 | /// # Examples |
743 | /// |
744 | /// ``` |
745 | /// #![feature(f16)] |
746 | /// # #[cfg (reliable_f16_math)] { |
747 | /// |
748 | /// let x = 2.0 * std::f16::consts::PI; |
749 | /// |
750 | /// let abs_difference = (x.cos() - 1.0).abs(); |
751 | /// |
752 | /// assert!(abs_difference <= f16::EPSILON); |
753 | /// # } |
754 | /// ``` |
755 | #[inline ] |
756 | #[rustc_allow_incoherent_impl ] |
757 | #[unstable (feature = "f16" , issue = "116909" )] |
758 | #[must_use = "method returns a new number and does not mutate the original value" ] |
759 | pub fn cos(self) -> f16 { |
760 | unsafe { intrinsics::cosf16(self) } |
761 | } |
762 | |
763 | /// Computes the tangent of a number (in radians). |
764 | /// |
765 | /// # Unspecified precision |
766 | /// |
767 | /// The precision of this function is non-deterministic. This means it varies by platform, |
768 | /// Rust version, and can even differ within the same execution from one invocation to the next. |
769 | /// |
770 | /// This function currently corresponds to the `tanf` from libc on Unix and |
771 | /// Windows. Note that this might change in the future. |
772 | /// |
773 | /// # Examples |
774 | /// |
775 | /// ``` |
776 | /// #![feature(f16)] |
777 | /// # #[cfg (reliable_f16_math)] { |
778 | /// |
779 | /// let x = std::f16::consts::FRAC_PI_4; |
780 | /// let abs_difference = (x.tan() - 1.0).abs(); |
781 | /// |
782 | /// assert!(abs_difference <= f16::EPSILON); |
783 | /// # } |
784 | /// ``` |
785 | #[inline ] |
786 | #[rustc_allow_incoherent_impl ] |
787 | #[unstable (feature = "f16" , issue = "116909" )] |
788 | #[must_use = "method returns a new number and does not mutate the original value" ] |
789 | pub fn tan(self) -> f16 { |
790 | (unsafe { cmath::tanf(self as f32) }) as f16 |
791 | } |
792 | |
793 | /// Computes the arcsine of a number. Return value is in radians in |
794 | /// the range [-pi/2, pi/2] or NaN if the number is outside the range |
795 | /// [-1, 1]. |
796 | /// |
797 | /// # Unspecified precision |
798 | /// |
799 | /// The precision of this function is non-deterministic. This means it varies by platform, |
800 | /// Rust version, and can even differ within the same execution from one invocation to the next. |
801 | /// |
802 | /// This function currently corresponds to the `asinf` from libc on Unix |
803 | /// and Windows. Note that this might change in the future. |
804 | /// |
805 | /// # Examples |
806 | /// |
807 | /// ``` |
808 | /// #![feature(f16)] |
809 | /// # #[cfg (reliable_f16_math)] { |
810 | /// |
811 | /// let f = std::f16::consts::FRAC_PI_2; |
812 | /// |
813 | /// // asin(sin(pi/2)) |
814 | /// let abs_difference = (f.sin().asin() - std::f16::consts::FRAC_PI_2).abs(); |
815 | /// |
816 | /// assert!(abs_difference <= f16::EPSILON); |
817 | /// # } |
818 | /// ``` |
819 | #[inline ] |
820 | #[doc (alias = "arcsin" )] |
821 | #[rustc_allow_incoherent_impl ] |
822 | #[unstable (feature = "f16" , issue = "116909" )] |
823 | #[must_use = "method returns a new number and does not mutate the original value" ] |
824 | pub fn asin(self) -> f16 { |
825 | (unsafe { cmath::asinf(self as f32) }) as f16 |
826 | } |
827 | |
828 | /// Computes the arccosine of a number. Return value is in radians in |
829 | /// the range [0, pi] or NaN if the number is outside the range |
830 | /// [-1, 1]. |
831 | /// |
832 | /// # Unspecified precision |
833 | /// |
834 | /// The precision of this function is non-deterministic. This means it varies by platform, |
835 | /// Rust version, and can even differ within the same execution from one invocation to the next. |
836 | /// |
837 | /// This function currently corresponds to the `acosf` from libc on Unix |
838 | /// and Windows. Note that this might change in the future. |
839 | /// |
840 | /// # Examples |
841 | /// |
842 | /// ``` |
843 | /// #![feature(f16)] |
844 | /// # #[cfg (reliable_f16_math)] { |
845 | /// |
846 | /// let f = std::f16::consts::FRAC_PI_4; |
847 | /// |
848 | /// // acos(cos(pi/4)) |
849 | /// let abs_difference = (f.cos().acos() - std::f16::consts::FRAC_PI_4).abs(); |
850 | /// |
851 | /// assert!(abs_difference <= f16::EPSILON); |
852 | /// # } |
853 | /// ``` |
854 | #[inline ] |
855 | #[doc (alias = "arccos" )] |
856 | #[rustc_allow_incoherent_impl ] |
857 | #[unstable (feature = "f16" , issue = "116909" )] |
858 | #[must_use = "method returns a new number and does not mutate the original value" ] |
859 | pub fn acos(self) -> f16 { |
860 | (unsafe { cmath::acosf(self as f32) }) as f16 |
861 | } |
862 | |
863 | /// Computes the arctangent of a number. Return value is in radians in the |
864 | /// range [-pi/2, pi/2]; |
865 | /// |
866 | /// # Unspecified precision |
867 | /// |
868 | /// The precision of this function is non-deterministic. This means it varies by platform, |
869 | /// Rust version, and can even differ within the same execution from one invocation to the next. |
870 | /// |
871 | /// This function currently corresponds to the `atanf` from libc on Unix |
872 | /// and Windows. Note that this might change in the future. |
873 | /// |
874 | /// # Examples |
875 | /// |
876 | /// ``` |
877 | /// #![feature(f16)] |
878 | /// # #[cfg (reliable_f16_math)] { |
879 | /// |
880 | /// let f = 1.0f16; |
881 | /// |
882 | /// // atan(tan(1)) |
883 | /// let abs_difference = (f.tan().atan() - 1.0).abs(); |
884 | /// |
885 | /// assert!(abs_difference <= f16::EPSILON); |
886 | /// # } |
887 | /// ``` |
888 | #[inline ] |
889 | #[doc (alias = "arctan" )] |
890 | #[rustc_allow_incoherent_impl ] |
891 | #[unstable (feature = "f16" , issue = "116909" )] |
892 | #[must_use = "method returns a new number and does not mutate the original value" ] |
893 | pub fn atan(self) -> f16 { |
894 | (unsafe { cmath::atanf(self as f32) }) as f16 |
895 | } |
896 | |
897 | /// Computes the four quadrant arctangent of `self` (`y`) and `other` (`x`) in radians. |
898 | /// |
899 | /// * `x = 0`, `y = 0`: `0` |
900 | /// * `x >= 0`: `arctan(y/x)` -> `[-pi/2, pi/2]` |
901 | /// * `y >= 0`: `arctan(y/x) + pi` -> `(pi/2, pi]` |
902 | /// * `y < 0`: `arctan(y/x) - pi` -> `(-pi, -pi/2)` |
903 | /// |
904 | /// # Unspecified precision |
905 | /// |
906 | /// The precision of this function is non-deterministic. This means it varies by platform, |
907 | /// Rust version, and can even differ within the same execution from one invocation to the next. |
908 | /// |
909 | /// This function currently corresponds to the `atan2f` from libc on Unix |
910 | /// and Windows. Note that this might change in the future. |
911 | /// |
912 | /// # Examples |
913 | /// |
914 | /// ``` |
915 | /// #![feature(f16)] |
916 | /// # #[cfg (reliable_f16_math)] { |
917 | /// |
918 | /// // Positive angles measured counter-clockwise |
919 | /// // from positive x axis |
920 | /// // -pi/4 radians (45 deg clockwise) |
921 | /// let x1 = 3.0f16; |
922 | /// let y1 = -3.0f16; |
923 | /// |
924 | /// // 3pi/4 radians (135 deg counter-clockwise) |
925 | /// let x2 = -3.0f16; |
926 | /// let y2 = 3.0f16; |
927 | /// |
928 | /// let abs_difference_1 = (y1.atan2(x1) - (-std::f16::consts::FRAC_PI_4)).abs(); |
929 | /// let abs_difference_2 = (y2.atan2(x2) - (3.0 * std::f16::consts::FRAC_PI_4)).abs(); |
930 | /// |
931 | /// assert!(abs_difference_1 <= f16::EPSILON); |
932 | /// assert!(abs_difference_2 <= f16::EPSILON); |
933 | /// # } |
934 | /// ``` |
935 | #[inline ] |
936 | #[rustc_allow_incoherent_impl ] |
937 | #[unstable (feature = "f16" , issue = "116909" )] |
938 | #[must_use = "method returns a new number and does not mutate the original value" ] |
939 | pub fn atan2(self, other: f16) -> f16 { |
940 | (unsafe { cmath::atan2f(self as f32, other as f32) }) as f16 |
941 | } |
942 | |
943 | /// Simultaneously computes the sine and cosine of the number, `x`. Returns |
944 | /// `(sin(x), cos(x))`. |
945 | /// |
946 | /// # Unspecified precision |
947 | /// |
948 | /// The precision of this function is non-deterministic. This means it varies by platform, |
949 | /// Rust version, and can even differ within the same execution from one invocation to the next. |
950 | /// |
951 | /// This function currently corresponds to the `(f16::sin(x), |
952 | /// f16::cos(x))`. Note that this might change in the future. |
953 | /// |
954 | /// # Examples |
955 | /// |
956 | /// ``` |
957 | /// #![feature(f16)] |
958 | /// # #[cfg (reliable_f16_math)] { |
959 | /// |
960 | /// let x = std::f16::consts::FRAC_PI_4; |
961 | /// let f = x.sin_cos(); |
962 | /// |
963 | /// let abs_difference_0 = (f.0 - x.sin()).abs(); |
964 | /// let abs_difference_1 = (f.1 - x.cos()).abs(); |
965 | /// |
966 | /// assert!(abs_difference_0 <= f16::EPSILON); |
967 | /// assert!(abs_difference_1 <= f16::EPSILON); |
968 | /// # } |
969 | /// ``` |
970 | #[inline ] |
971 | #[doc (alias = "sincos" )] |
972 | #[rustc_allow_incoherent_impl ] |
973 | #[unstable (feature = "f16" , issue = "116909" )] |
974 | pub fn sin_cos(self) -> (f16, f16) { |
975 | (self.sin(), self.cos()) |
976 | } |
977 | |
978 | /// Returns `e^(self) - 1` in a way that is accurate even if the |
979 | /// number is close to zero. |
980 | /// |
981 | /// # Unspecified precision |
982 | /// |
983 | /// The precision of this function is non-deterministic. This means it varies by platform, |
984 | /// Rust version, and can even differ within the same execution from one invocation to the next. |
985 | /// |
986 | /// This function currently corresponds to the `expm1f` from libc on Unix |
987 | /// and Windows. Note that this might change in the future. |
988 | /// |
989 | /// # Examples |
990 | /// |
991 | /// ``` |
992 | /// #![feature(f16)] |
993 | /// # #[cfg (reliable_f16_math)] { |
994 | /// |
995 | /// let x = 1e-4_f16; |
996 | /// |
997 | /// // for very small x, e^x is approximately 1 + x + x^2 / 2 |
998 | /// let approx = x + x * x / 2.0; |
999 | /// let abs_difference = (x.exp_m1() - approx).abs(); |
1000 | /// |
1001 | /// assert!(abs_difference < 1e-4); |
1002 | /// # } |
1003 | /// ``` |
1004 | #[inline ] |
1005 | #[rustc_allow_incoherent_impl ] |
1006 | #[unstable (feature = "f16" , issue = "116909" )] |
1007 | #[must_use = "method returns a new number and does not mutate the original value" ] |
1008 | pub fn exp_m1(self) -> f16 { |
1009 | (unsafe { cmath::expm1f(self as f32) }) as f16 |
1010 | } |
1011 | |
1012 | /// Returns `ln(1+n)` (natural logarithm) more accurately than if |
1013 | /// the operations were performed separately. |
1014 | /// |
1015 | /// This returns NaN when `n < -1.0`, and negative infinity when `n == -1.0`. |
1016 | /// |
1017 | /// # Unspecified precision |
1018 | /// |
1019 | /// The precision of this function is non-deterministic. This means it varies by platform, |
1020 | /// Rust version, and can even differ within the same execution from one invocation to the next. |
1021 | /// |
1022 | /// This function currently corresponds to the `log1pf` from libc on Unix |
1023 | /// and Windows. Note that this might change in the future. |
1024 | /// |
1025 | /// # Examples |
1026 | /// |
1027 | /// ``` |
1028 | /// #![feature(f16)] |
1029 | /// # #[cfg (reliable_f16_math)] { |
1030 | /// |
1031 | /// let x = 1e-4_f16; |
1032 | /// |
1033 | /// // for very small x, ln(1 + x) is approximately x - x^2 / 2 |
1034 | /// let approx = x - x * x / 2.0; |
1035 | /// let abs_difference = (x.ln_1p() - approx).abs(); |
1036 | /// |
1037 | /// assert!(abs_difference < 1e-4); |
1038 | /// # } |
1039 | /// ``` |
1040 | /// |
1041 | /// Out-of-range values: |
1042 | /// ``` |
1043 | /// #![feature(f16)] |
1044 | /// # #[cfg (reliable_f16_math)] { |
1045 | /// |
1046 | /// assert_eq!((-1.0_f16).ln_1p(), f16::NEG_INFINITY); |
1047 | /// assert!((-2.0_f16).ln_1p().is_nan()); |
1048 | /// # } |
1049 | /// ``` |
1050 | #[inline ] |
1051 | #[doc (alias = "log1p" )] |
1052 | #[rustc_allow_incoherent_impl ] |
1053 | #[unstable (feature = "f16" , issue = "116909" )] |
1054 | #[must_use = "method returns a new number and does not mutate the original value" ] |
1055 | pub fn ln_1p(self) -> f16 { |
1056 | (unsafe { cmath::log1pf(self as f32) }) as f16 |
1057 | } |
1058 | |
1059 | /// Hyperbolic sine function. |
1060 | /// |
1061 | /// # Unspecified precision |
1062 | /// |
1063 | /// The precision of this function is non-deterministic. This means it varies by platform, |
1064 | /// Rust version, and can even differ within the same execution from one invocation to the next. |
1065 | /// |
1066 | /// This function currently corresponds to the `sinhf` from libc on Unix |
1067 | /// and Windows. Note that this might change in the future. |
1068 | /// |
1069 | /// # Examples |
1070 | /// |
1071 | /// ``` |
1072 | /// #![feature(f16)] |
1073 | /// # #[cfg (reliable_f16_math)] { |
1074 | /// |
1075 | /// let e = std::f16::consts::E; |
1076 | /// let x = 1.0f16; |
1077 | /// |
1078 | /// let f = x.sinh(); |
1079 | /// // Solving sinh() at 1 gives `(e^2-1)/(2e)` |
1080 | /// let g = ((e * e) - 1.0) / (2.0 * e); |
1081 | /// let abs_difference = (f - g).abs(); |
1082 | /// |
1083 | /// assert!(abs_difference <= f16::EPSILON); |
1084 | /// # } |
1085 | /// ``` |
1086 | #[inline ] |
1087 | #[rustc_allow_incoherent_impl ] |
1088 | #[unstable (feature = "f16" , issue = "116909" )] |
1089 | #[must_use = "method returns a new number and does not mutate the original value" ] |
1090 | pub fn sinh(self) -> f16 { |
1091 | (unsafe { cmath::sinhf(self as f32) }) as f16 |
1092 | } |
1093 | |
1094 | /// Hyperbolic cosine function. |
1095 | /// |
1096 | /// # Unspecified precision |
1097 | /// |
1098 | /// The precision of this function is non-deterministic. This means it varies by platform, |
1099 | /// Rust version, and can even differ within the same execution from one invocation to the next. |
1100 | /// |
1101 | /// This function currently corresponds to the `coshf` from libc on Unix |
1102 | /// and Windows. Note that this might change in the future. |
1103 | /// |
1104 | /// # Examples |
1105 | /// |
1106 | /// ``` |
1107 | /// #![feature(f16)] |
1108 | /// # #[cfg (reliable_f16_math)] { |
1109 | /// |
1110 | /// let e = std::f16::consts::E; |
1111 | /// let x = 1.0f16; |
1112 | /// let f = x.cosh(); |
1113 | /// // Solving cosh() at 1 gives this result |
1114 | /// let g = ((e * e) + 1.0) / (2.0 * e); |
1115 | /// let abs_difference = (f - g).abs(); |
1116 | /// |
1117 | /// // Same result |
1118 | /// assert!(abs_difference <= f16::EPSILON); |
1119 | /// # } |
1120 | /// ``` |
1121 | #[inline ] |
1122 | #[rustc_allow_incoherent_impl ] |
1123 | #[unstable (feature = "f16" , issue = "116909" )] |
1124 | #[must_use = "method returns a new number and does not mutate the original value" ] |
1125 | pub fn cosh(self) -> f16 { |
1126 | (unsafe { cmath::coshf(self as f32) }) as f16 |
1127 | } |
1128 | |
1129 | /// Hyperbolic tangent function. |
1130 | /// |
1131 | /// # Unspecified precision |
1132 | /// |
1133 | /// The precision of this function is non-deterministic. This means it varies by platform, |
1134 | /// Rust version, and can even differ within the same execution from one invocation to the next. |
1135 | /// |
1136 | /// This function currently corresponds to the `tanhf` from libc on Unix |
1137 | /// and Windows. Note that this might change in the future. |
1138 | /// |
1139 | /// # Examples |
1140 | /// |
1141 | /// ``` |
1142 | /// #![feature(f16)] |
1143 | /// # #[cfg (reliable_f16_math)] { |
1144 | /// |
1145 | /// let e = std::f16::consts::E; |
1146 | /// let x = 1.0f16; |
1147 | /// |
1148 | /// let f = x.tanh(); |
1149 | /// // Solving tanh() at 1 gives `(1 - e^(-2))/(1 + e^(-2))` |
1150 | /// let g = (1.0 - e.powi(-2)) / (1.0 + e.powi(-2)); |
1151 | /// let abs_difference = (f - g).abs(); |
1152 | /// |
1153 | /// assert!(abs_difference <= f16::EPSILON); |
1154 | /// # } |
1155 | /// ``` |
1156 | #[inline ] |
1157 | #[rustc_allow_incoherent_impl ] |
1158 | #[unstable (feature = "f16" , issue = "116909" )] |
1159 | #[must_use = "method returns a new number and does not mutate the original value" ] |
1160 | pub fn tanh(self) -> f16 { |
1161 | (unsafe { cmath::tanhf(self as f32) }) as f16 |
1162 | } |
1163 | |
1164 | /// Inverse hyperbolic sine function. |
1165 | /// |
1166 | /// # Unspecified precision |
1167 | /// |
1168 | /// The precision of this function is non-deterministic. This means it varies by platform, |
1169 | /// Rust version, and can even differ within the same execution from one invocation to the next. |
1170 | /// |
1171 | /// # Examples |
1172 | /// |
1173 | /// ``` |
1174 | /// #![feature(f16)] |
1175 | /// # #[cfg (reliable_f16_math)] { |
1176 | /// |
1177 | /// let x = 1.0f16; |
1178 | /// let f = x.sinh().asinh(); |
1179 | /// |
1180 | /// let abs_difference = (f - x).abs(); |
1181 | /// |
1182 | /// assert!(abs_difference <= f16::EPSILON); |
1183 | /// # } |
1184 | /// ``` |
1185 | #[inline ] |
1186 | #[doc (alias = "arcsinh" )] |
1187 | #[rustc_allow_incoherent_impl ] |
1188 | #[unstable (feature = "f16" , issue = "116909" )] |
1189 | #[must_use = "method returns a new number and does not mutate the original value" ] |
1190 | pub fn asinh(self) -> f16 { |
1191 | let ax = self.abs(); |
1192 | let ix = 1.0 / ax; |
1193 | (ax + (ax / (Self::hypot(1.0, ix) + ix))).ln_1p().copysign(self) |
1194 | } |
1195 | |
1196 | /// Inverse hyperbolic cosine function. |
1197 | /// |
1198 | /// # Unspecified precision |
1199 | /// |
1200 | /// The precision of this function is non-deterministic. This means it varies by platform, |
1201 | /// Rust version, and can even differ within the same execution from one invocation to the next. |
1202 | /// |
1203 | /// # Examples |
1204 | /// |
1205 | /// ``` |
1206 | /// #![feature(f16)] |
1207 | /// # #[cfg (reliable_f16_math)] { |
1208 | /// |
1209 | /// let x = 1.0f16; |
1210 | /// let f = x.cosh().acosh(); |
1211 | /// |
1212 | /// let abs_difference = (f - x).abs(); |
1213 | /// |
1214 | /// assert!(abs_difference <= f16::EPSILON); |
1215 | /// # } |
1216 | /// ``` |
1217 | #[inline ] |
1218 | #[doc (alias = "arccosh" )] |
1219 | #[rustc_allow_incoherent_impl ] |
1220 | #[unstable (feature = "f16" , issue = "116909" )] |
1221 | #[must_use = "method returns a new number and does not mutate the original value" ] |
1222 | pub fn acosh(self) -> f16 { |
1223 | if self < 1.0 { |
1224 | Self::NAN |
1225 | } else { |
1226 | (self + ((self - 1.0).sqrt() * (self + 1.0).sqrt())).ln() |
1227 | } |
1228 | } |
1229 | |
1230 | /// Inverse hyperbolic tangent function. |
1231 | /// |
1232 | /// # Unspecified precision |
1233 | /// |
1234 | /// The precision of this function is non-deterministic. This means it varies by platform, |
1235 | /// Rust version, and can even differ within the same execution from one invocation to the next. |
1236 | /// |
1237 | /// # Examples |
1238 | /// |
1239 | /// ``` |
1240 | /// #![feature(f16)] |
1241 | /// # #[cfg (reliable_f16_math)] { |
1242 | /// |
1243 | /// let e = std::f16::consts::E; |
1244 | /// let f = e.tanh().atanh(); |
1245 | /// |
1246 | /// let abs_difference = (f - e).abs(); |
1247 | /// |
1248 | /// assert!(abs_difference <= 0.01); |
1249 | /// # } |
1250 | /// ``` |
1251 | #[inline ] |
1252 | #[doc (alias = "arctanh" )] |
1253 | #[rustc_allow_incoherent_impl ] |
1254 | #[unstable (feature = "f16" , issue = "116909" )] |
1255 | #[must_use = "method returns a new number and does not mutate the original value" ] |
1256 | pub fn atanh(self) -> f16 { |
1257 | 0.5 * ((2.0 * self) / (1.0 - self)).ln_1p() |
1258 | } |
1259 | |
1260 | /// Gamma function. |
1261 | /// |
1262 | /// # Unspecified precision |
1263 | /// |
1264 | /// The precision of this function is non-deterministic. This means it varies by platform, |
1265 | /// Rust version, and can even differ within the same execution from one invocation to the next. |
1266 | /// |
1267 | /// This function currently corresponds to the `tgammaf` from libc on Unix |
1268 | /// and Windows. Note that this might change in the future. |
1269 | /// |
1270 | /// # Examples |
1271 | /// |
1272 | /// ``` |
1273 | /// #![feature(f16)] |
1274 | /// #![feature(float_gamma)] |
1275 | /// # #[cfg (reliable_f16_math)] { |
1276 | /// |
1277 | /// let x = 5.0f16; |
1278 | /// |
1279 | /// let abs_difference = (x.gamma() - 24.0).abs(); |
1280 | /// |
1281 | /// assert!(abs_difference <= f16::EPSILON); |
1282 | /// # } |
1283 | /// ``` |
1284 | #[inline ] |
1285 | #[rustc_allow_incoherent_impl ] |
1286 | #[unstable (feature = "f16" , issue = "116909" )] |
1287 | // #[unstable(feature = "float_gamma", issue = "99842")] |
1288 | #[must_use = "method returns a new number and does not mutate the original value" ] |
1289 | pub fn gamma(self) -> f16 { |
1290 | (unsafe { cmath::tgammaf(self as f32) }) as f16 |
1291 | } |
1292 | |
1293 | /// Natural logarithm of the absolute value of the gamma function |
1294 | /// |
1295 | /// The integer part of the tuple indicates the sign of the gamma function. |
1296 | /// |
1297 | /// # Unspecified precision |
1298 | /// |
1299 | /// The precision of this function is non-deterministic. This means it varies by platform, |
1300 | /// Rust version, and can even differ within the same execution from one invocation to the next. |
1301 | /// |
1302 | /// This function currently corresponds to the `lgamma_r` from libc on Unix |
1303 | /// and Windows. Note that this might change in the future. |
1304 | /// |
1305 | /// # Examples |
1306 | /// |
1307 | /// ``` |
1308 | /// #![feature(f16)] |
1309 | /// #![feature(float_gamma)] |
1310 | /// # #[cfg (reliable_f16_math)] { |
1311 | /// |
1312 | /// let x = 2.0f16; |
1313 | /// |
1314 | /// let abs_difference = (x.ln_gamma().0 - 0.0).abs(); |
1315 | /// |
1316 | /// assert!(abs_difference <= f16::EPSILON); |
1317 | /// # } |
1318 | /// ``` |
1319 | #[inline ] |
1320 | #[rustc_allow_incoherent_impl ] |
1321 | #[unstable (feature = "f16" , issue = "116909" )] |
1322 | // #[unstable(feature = "float_gamma", issue = "99842")] |
1323 | #[must_use = "method returns a new number and does not mutate the original value" ] |
1324 | pub fn ln_gamma(self) -> (f16, i32) { |
1325 | let mut signgamp: i32 = 0; |
1326 | let x = (unsafe { cmath::lgammaf_r(self as f32, &mut signgamp) }) as f16; |
1327 | (x, signgamp) |
1328 | } |
1329 | |
1330 | /// Error function. |
1331 | /// |
1332 | /// # Unspecified precision |
1333 | /// |
1334 | /// The precision of this function is non-deterministic. This means it varies by platform, |
1335 | /// Rust version, and can even differ within the same execution from one invocation to the next. |
1336 | /// |
1337 | /// This function currently corresponds to the `erff` from libc on Unix |
1338 | /// and Windows. Note that this might change in the future. |
1339 | /// |
1340 | /// # Examples |
1341 | /// |
1342 | /// ``` |
1343 | /// #![feature(f16)] |
1344 | /// #![feature(float_erf)] |
1345 | /// # #[cfg (reliable_f16_math)] { |
1346 | /// /// The error function relates what percent of a normal distribution lies |
1347 | /// /// within `x` standard deviations (scaled by `1/sqrt(2)`). |
1348 | /// fn within_standard_deviations(x: f16) -> f16 { |
1349 | /// (x * std::f16::consts::FRAC_1_SQRT_2).erf() * 100.0 |
1350 | /// } |
1351 | /// |
1352 | /// // 68% of a normal distribution is within one standard deviation |
1353 | /// assert!((within_standard_deviations(1.0) - 68.269).abs() < 0.1); |
1354 | /// // 95% of a normal distribution is within two standard deviations |
1355 | /// assert!((within_standard_deviations(2.0) - 95.450).abs() < 0.1); |
1356 | /// // 99.7% of a normal distribution is within three standard deviations |
1357 | /// assert!((within_standard_deviations(3.0) - 99.730).abs() < 0.1); |
1358 | /// # } |
1359 | /// ``` |
1360 | #[rustc_allow_incoherent_impl ] |
1361 | #[must_use = "method returns a new number and does not mutate the original value" ] |
1362 | #[unstable (feature = "f16" , issue = "116909" )] |
1363 | // #[unstable(feature = "float_erf", issue = "136321")] |
1364 | #[inline ] |
1365 | pub fn erf(self) -> f16 { |
1366 | (unsafe { cmath::erff(self as f32) }) as f16 |
1367 | } |
1368 | |
1369 | /// Complementary error function. |
1370 | /// |
1371 | /// # Unspecified precision |
1372 | /// |
1373 | /// The precision of this function is non-deterministic. This means it varies by platform, |
1374 | /// Rust version, and can even differ within the same execution from one invocation to the next. |
1375 | /// |
1376 | /// This function currently corresponds to the `erfcf` from libc on Unix |
1377 | /// and Windows. Note that this might change in the future. |
1378 | /// |
1379 | /// # Examples |
1380 | /// |
1381 | /// ``` |
1382 | /// #![feature(f16)] |
1383 | /// #![feature(float_erf)] |
1384 | /// # #[cfg (reliable_f16_math)] { |
1385 | /// let x: f16 = 0.123; |
1386 | /// |
1387 | /// let one = x.erf() + x.erfc(); |
1388 | /// let abs_difference = (one - 1.0).abs(); |
1389 | /// |
1390 | /// assert!(abs_difference <= f16::EPSILON); |
1391 | /// # } |
1392 | /// ``` |
1393 | #[rustc_allow_incoherent_impl ] |
1394 | #[must_use = "method returns a new number and does not mutate the original value" ] |
1395 | #[unstable (feature = "f16" , issue = "116909" )] |
1396 | // #[unstable(feature = "float_erf", issue = "136321")] |
1397 | #[inline ] |
1398 | pub fn erfc(self) -> f16 { |
1399 | (unsafe { cmath::erfcf(self as f32) }) as f16 |
1400 | } |
1401 | } |
1402 | |