1 | //! Constants for the `f64` double-precision floating point type. |
2 | //! |
3 | //! *[See also the `f64` primitive type][f64].* |
4 | //! |
5 | //! Mathematically significant numbers are provided in the `consts` sub-module. |
6 | //! |
7 | //! For the constants defined directly in this module |
8 | //! (as distinct from those defined in the `consts` sub-module), |
9 | //! new code should instead use the associated constants |
10 | //! defined directly on the `f64` type. |
11 | |
12 | #![stable (feature = "rust1" , since = "1.0.0" )] |
13 | |
14 | use crate::convert::FloatToInt; |
15 | use crate::num::FpCategory; |
16 | use crate::panic::const_assert; |
17 | use crate::{intrinsics, mem}; |
18 | |
19 | /// The radix or base of the internal representation of `f64`. |
20 | /// Use [`f64::RADIX`] instead. |
21 | /// |
22 | /// # Examples |
23 | /// |
24 | /// ```rust |
25 | /// // deprecated way |
26 | /// # #[allow (deprecated, deprecated_in_future)] |
27 | /// let r = std::f64::RADIX; |
28 | /// |
29 | /// // intended way |
30 | /// let r = f64::RADIX; |
31 | /// ``` |
32 | #[stable (feature = "rust1" , since = "1.0.0" )] |
33 | #[deprecated (since = "TBD" , note = "replaced by the `RADIX` associated constant on `f64`" )] |
34 | #[rustc_diagnostic_item = "f64_legacy_const_radix" ] |
35 | pub const RADIX: u32 = f64::RADIX; |
36 | |
37 | /// Number of significant digits in base 2. |
38 | /// Use [`f64::MANTISSA_DIGITS`] instead. |
39 | /// |
40 | /// # Examples |
41 | /// |
42 | /// ```rust |
43 | /// // deprecated way |
44 | /// # #[allow (deprecated, deprecated_in_future)] |
45 | /// let d = std::f64::MANTISSA_DIGITS; |
46 | /// |
47 | /// // intended way |
48 | /// let d = f64::MANTISSA_DIGITS; |
49 | /// ``` |
50 | #[stable (feature = "rust1" , since = "1.0.0" )] |
51 | #[deprecated ( |
52 | since = "TBD" , |
53 | note = "replaced by the `MANTISSA_DIGITS` associated constant on `f64`" |
54 | )] |
55 | #[rustc_diagnostic_item = "f64_legacy_const_mantissa_dig" ] |
56 | pub const MANTISSA_DIGITS: u32 = f64::MANTISSA_DIGITS; |
57 | |
58 | /// Approximate number of significant digits in base 10. |
59 | /// Use [`f64::DIGITS`] instead. |
60 | /// |
61 | /// # Examples |
62 | /// |
63 | /// ```rust |
64 | /// // deprecated way |
65 | /// # #[allow (deprecated, deprecated_in_future)] |
66 | /// let d = std::f64::DIGITS; |
67 | /// |
68 | /// // intended way |
69 | /// let d = f64::DIGITS; |
70 | /// ``` |
71 | #[stable (feature = "rust1" , since = "1.0.0" )] |
72 | #[deprecated (since = "TBD" , note = "replaced by the `DIGITS` associated constant on `f64`" )] |
73 | #[rustc_diagnostic_item = "f64_legacy_const_digits" ] |
74 | pub const DIGITS: u32 = f64::DIGITS; |
75 | |
76 | /// [Machine epsilon] value for `f64`. |
77 | /// Use [`f64::EPSILON`] instead. |
78 | /// |
79 | /// This is the difference between `1.0` and the next larger representable number. |
80 | /// |
81 | /// [Machine epsilon]: https://en.wikipedia.org/wiki/Machine_epsilon |
82 | /// |
83 | /// # Examples |
84 | /// |
85 | /// ```rust |
86 | /// // deprecated way |
87 | /// # #[allow (deprecated, deprecated_in_future)] |
88 | /// let e = std::f64::EPSILON; |
89 | /// |
90 | /// // intended way |
91 | /// let e = f64::EPSILON; |
92 | /// ``` |
93 | #[stable (feature = "rust1" , since = "1.0.0" )] |
94 | #[deprecated (since = "TBD" , note = "replaced by the `EPSILON` associated constant on `f64`" )] |
95 | #[rustc_diagnostic_item = "f64_legacy_const_epsilon" ] |
96 | pub const EPSILON: f64 = f64::EPSILON; |
97 | |
98 | /// Smallest finite `f64` value. |
99 | /// Use [`f64::MIN`] instead. |
100 | /// |
101 | /// # Examples |
102 | /// |
103 | /// ```rust |
104 | /// // deprecated way |
105 | /// # #[allow (deprecated, deprecated_in_future)] |
106 | /// let min = std::f64::MIN; |
107 | /// |
108 | /// // intended way |
109 | /// let min = f64::MIN; |
110 | /// ``` |
111 | #[stable (feature = "rust1" , since = "1.0.0" )] |
112 | #[deprecated (since = "TBD" , note = "replaced by the `MIN` associated constant on `f64`" )] |
113 | #[rustc_diagnostic_item = "f64_legacy_const_min" ] |
114 | pub const MIN: f64 = f64::MIN; |
115 | |
116 | /// Smallest positive normal `f64` value. |
117 | /// Use [`f64::MIN_POSITIVE`] instead. |
118 | /// |
119 | /// # Examples |
120 | /// |
121 | /// ```rust |
122 | /// // deprecated way |
123 | /// # #[allow (deprecated, deprecated_in_future)] |
124 | /// let min = std::f64::MIN_POSITIVE; |
125 | /// |
126 | /// // intended way |
127 | /// let min = f64::MIN_POSITIVE; |
128 | /// ``` |
129 | #[stable (feature = "rust1" , since = "1.0.0" )] |
130 | #[deprecated (since = "TBD" , note = "replaced by the `MIN_POSITIVE` associated constant on `f64`" )] |
131 | #[rustc_diagnostic_item = "f64_legacy_const_min_positive" ] |
132 | pub const MIN_POSITIVE: f64 = f64::MIN_POSITIVE; |
133 | |
134 | /// Largest finite `f64` value. |
135 | /// Use [`f64::MAX`] instead. |
136 | /// |
137 | /// # Examples |
138 | /// |
139 | /// ```rust |
140 | /// // deprecated way |
141 | /// # #[allow (deprecated, deprecated_in_future)] |
142 | /// let max = std::f64::MAX; |
143 | /// |
144 | /// // intended way |
145 | /// let max = f64::MAX; |
146 | /// ``` |
147 | #[stable (feature = "rust1" , since = "1.0.0" )] |
148 | #[deprecated (since = "TBD" , note = "replaced by the `MAX` associated constant on `f64`" )] |
149 | #[rustc_diagnostic_item = "f64_legacy_const_max" ] |
150 | pub const MAX: f64 = f64::MAX; |
151 | |
152 | /// One greater than the minimum possible normal power of 2 exponent. |
153 | /// Use [`f64::MIN_EXP`] instead. |
154 | /// |
155 | /// # Examples |
156 | /// |
157 | /// ```rust |
158 | /// // deprecated way |
159 | /// # #[allow (deprecated, deprecated_in_future)] |
160 | /// let min = std::f64::MIN_EXP; |
161 | /// |
162 | /// // intended way |
163 | /// let min = f64::MIN_EXP; |
164 | /// ``` |
165 | #[stable (feature = "rust1" , since = "1.0.0" )] |
166 | #[deprecated (since = "TBD" , note = "replaced by the `MIN_EXP` associated constant on `f64`" )] |
167 | #[rustc_diagnostic_item = "f64_legacy_const_min_exp" ] |
168 | pub const MIN_EXP: i32 = f64::MIN_EXP; |
169 | |
170 | /// Maximum possible power of 2 exponent. |
171 | /// Use [`f64::MAX_EXP`] instead. |
172 | /// |
173 | /// # Examples |
174 | /// |
175 | /// ```rust |
176 | /// // deprecated way |
177 | /// # #[allow (deprecated, deprecated_in_future)] |
178 | /// let max = std::f64::MAX_EXP; |
179 | /// |
180 | /// // intended way |
181 | /// let max = f64::MAX_EXP; |
182 | /// ``` |
183 | #[stable (feature = "rust1" , since = "1.0.0" )] |
184 | #[deprecated (since = "TBD" , note = "replaced by the `MAX_EXP` associated constant on `f64`" )] |
185 | #[rustc_diagnostic_item = "f64_legacy_const_max_exp" ] |
186 | pub const MAX_EXP: i32 = f64::MAX_EXP; |
187 | |
188 | /// Minimum possible normal power of 10 exponent. |
189 | /// Use [`f64::MIN_10_EXP`] instead. |
190 | /// |
191 | /// # Examples |
192 | /// |
193 | /// ```rust |
194 | /// // deprecated way |
195 | /// # #[allow (deprecated, deprecated_in_future)] |
196 | /// let min = std::f64::MIN_10_EXP; |
197 | /// |
198 | /// // intended way |
199 | /// let min = f64::MIN_10_EXP; |
200 | /// ``` |
201 | #[stable (feature = "rust1" , since = "1.0.0" )] |
202 | #[deprecated (since = "TBD" , note = "replaced by the `MIN_10_EXP` associated constant on `f64`" )] |
203 | #[rustc_diagnostic_item = "f64_legacy_const_min_10_exp" ] |
204 | pub const MIN_10_EXP: i32 = f64::MIN_10_EXP; |
205 | |
206 | /// Maximum possible power of 10 exponent. |
207 | /// Use [`f64::MAX_10_EXP`] instead. |
208 | /// |
209 | /// # Examples |
210 | /// |
211 | /// ```rust |
212 | /// // deprecated way |
213 | /// # #[allow (deprecated, deprecated_in_future)] |
214 | /// let max = std::f64::MAX_10_EXP; |
215 | /// |
216 | /// // intended way |
217 | /// let max = f64::MAX_10_EXP; |
218 | /// ``` |
219 | #[stable (feature = "rust1" , since = "1.0.0" )] |
220 | #[deprecated (since = "TBD" , note = "replaced by the `MAX_10_EXP` associated constant on `f64`" )] |
221 | #[rustc_diagnostic_item = "f64_legacy_const_max_10_exp" ] |
222 | pub const MAX_10_EXP: i32 = f64::MAX_10_EXP; |
223 | |
224 | /// Not a Number (NaN). |
225 | /// Use [`f64::NAN`] instead. |
226 | /// |
227 | /// # Examples |
228 | /// |
229 | /// ```rust |
230 | /// // deprecated way |
231 | /// # #[allow (deprecated, deprecated_in_future)] |
232 | /// let nan = std::f64::NAN; |
233 | /// |
234 | /// // intended way |
235 | /// let nan = f64::NAN; |
236 | /// ``` |
237 | #[stable (feature = "rust1" , since = "1.0.0" )] |
238 | #[deprecated (since = "TBD" , note = "replaced by the `NAN` associated constant on `f64`" )] |
239 | #[rustc_diagnostic_item = "f64_legacy_const_nan" ] |
240 | pub const NAN: f64 = f64::NAN; |
241 | |
242 | /// Infinity (∞). |
243 | /// Use [`f64::INFINITY`] instead. |
244 | /// |
245 | /// # Examples |
246 | /// |
247 | /// ```rust |
248 | /// // deprecated way |
249 | /// # #[allow (deprecated, deprecated_in_future)] |
250 | /// let inf = std::f64::INFINITY; |
251 | /// |
252 | /// // intended way |
253 | /// let inf = f64::INFINITY; |
254 | /// ``` |
255 | #[stable (feature = "rust1" , since = "1.0.0" )] |
256 | #[deprecated (since = "TBD" , note = "replaced by the `INFINITY` associated constant on `f64`" )] |
257 | #[rustc_diagnostic_item = "f64_legacy_const_infinity" ] |
258 | pub const INFINITY: f64 = f64::INFINITY; |
259 | |
260 | /// Negative infinity (−∞). |
261 | /// Use [`f64::NEG_INFINITY`] instead. |
262 | /// |
263 | /// # Examples |
264 | /// |
265 | /// ```rust |
266 | /// // deprecated way |
267 | /// # #[allow (deprecated, deprecated_in_future)] |
268 | /// let ninf = std::f64::NEG_INFINITY; |
269 | /// |
270 | /// // intended way |
271 | /// let ninf = f64::NEG_INFINITY; |
272 | /// ``` |
273 | #[stable (feature = "rust1" , since = "1.0.0" )] |
274 | #[deprecated (since = "TBD" , note = "replaced by the `NEG_INFINITY` associated constant on `f64`" )] |
275 | #[rustc_diagnostic_item = "f64_legacy_const_neg_infinity" ] |
276 | pub const NEG_INFINITY: f64 = f64::NEG_INFINITY; |
277 | |
278 | /// Basic mathematical constants. |
279 | #[stable (feature = "rust1" , since = "1.0.0" )] |
280 | pub mod consts { |
281 | // FIXME: replace with mathematical constants from cmath. |
282 | |
283 | /// Archimedes' constant (π) |
284 | #[stable (feature = "rust1" , since = "1.0.0" )] |
285 | pub const PI: f64 = 3.14159265358979323846264338327950288_f64; |
286 | |
287 | /// The full circle constant (τ) |
288 | /// |
289 | /// Equal to 2π. |
290 | #[stable (feature = "tau_constant" , since = "1.47.0" )] |
291 | pub const TAU: f64 = 6.28318530717958647692528676655900577_f64; |
292 | |
293 | /// The golden ratio (φ) |
294 | #[unstable (feature = "more_float_constants" , issue = "103883" )] |
295 | pub const PHI: f64 = 1.618033988749894848204586834365638118_f64; |
296 | |
297 | /// The Euler-Mascheroni constant (γ) |
298 | #[unstable (feature = "more_float_constants" , issue = "103883" )] |
299 | pub const EGAMMA: f64 = 0.577215664901532860606512090082402431_f64; |
300 | |
301 | /// π/2 |
302 | #[stable (feature = "rust1" , since = "1.0.0" )] |
303 | pub const FRAC_PI_2: f64 = 1.57079632679489661923132169163975144_f64; |
304 | |
305 | /// π/3 |
306 | #[stable (feature = "rust1" , since = "1.0.0" )] |
307 | pub const FRAC_PI_3: f64 = 1.04719755119659774615421446109316763_f64; |
308 | |
309 | /// π/4 |
310 | #[stable (feature = "rust1" , since = "1.0.0" )] |
311 | pub const FRAC_PI_4: f64 = 0.785398163397448309615660845819875721_f64; |
312 | |
313 | /// π/6 |
314 | #[stable (feature = "rust1" , since = "1.0.0" )] |
315 | pub const FRAC_PI_6: f64 = 0.52359877559829887307710723054658381_f64; |
316 | |
317 | /// π/8 |
318 | #[stable (feature = "rust1" , since = "1.0.0" )] |
319 | pub const FRAC_PI_8: f64 = 0.39269908169872415480783042290993786_f64; |
320 | |
321 | /// 1/π |
322 | #[stable (feature = "rust1" , since = "1.0.0" )] |
323 | pub const FRAC_1_PI: f64 = 0.318309886183790671537767526745028724_f64; |
324 | |
325 | /// 1/sqrt(π) |
326 | #[unstable (feature = "more_float_constants" , issue = "103883" )] |
327 | pub const FRAC_1_SQRT_PI: f64 = 0.564189583547756286948079451560772586_f64; |
328 | |
329 | /// 1/sqrt(2π) |
330 | #[doc (alias = "FRAC_1_SQRT_TAU" )] |
331 | #[unstable (feature = "more_float_constants" , issue = "103883" )] |
332 | pub const FRAC_1_SQRT_2PI: f64 = 0.398942280401432677939946059934381868_f64; |
333 | |
334 | /// 2/π |
335 | #[stable (feature = "rust1" , since = "1.0.0" )] |
336 | pub const FRAC_2_PI: f64 = 0.636619772367581343075535053490057448_f64; |
337 | |
338 | /// 2/sqrt(π) |
339 | #[stable (feature = "rust1" , since = "1.0.0" )] |
340 | pub const FRAC_2_SQRT_PI: f64 = 1.12837916709551257389615890312154517_f64; |
341 | |
342 | /// sqrt(2) |
343 | #[stable (feature = "rust1" , since = "1.0.0" )] |
344 | pub const SQRT_2: f64 = 1.41421356237309504880168872420969808_f64; |
345 | |
346 | /// 1/sqrt(2) |
347 | #[stable (feature = "rust1" , since = "1.0.0" )] |
348 | pub const FRAC_1_SQRT_2: f64 = 0.707106781186547524400844362104849039_f64; |
349 | |
350 | /// sqrt(3) |
351 | #[unstable (feature = "more_float_constants" , issue = "103883" )] |
352 | pub const SQRT_3: f64 = 1.732050807568877293527446341505872367_f64; |
353 | |
354 | /// 1/sqrt(3) |
355 | #[unstable (feature = "more_float_constants" , issue = "103883" )] |
356 | pub const FRAC_1_SQRT_3: f64 = 0.577350269189625764509148780501957456_f64; |
357 | |
358 | /// Euler's number (e) |
359 | #[stable (feature = "rust1" , since = "1.0.0" )] |
360 | pub const E: f64 = 2.71828182845904523536028747135266250_f64; |
361 | |
362 | /// log<sub>2</sub>(10) |
363 | #[stable (feature = "extra_log_consts" , since = "1.43.0" )] |
364 | pub const LOG2_10: f64 = 3.32192809488736234787031942948939018_f64; |
365 | |
366 | /// log<sub>2</sub>(e) |
367 | #[stable (feature = "rust1" , since = "1.0.0" )] |
368 | pub const LOG2_E: f64 = 1.44269504088896340735992468100189214_f64; |
369 | |
370 | /// log<sub>10</sub>(2) |
371 | #[stable (feature = "extra_log_consts" , since = "1.43.0" )] |
372 | pub const LOG10_2: f64 = 0.301029995663981195213738894724493027_f64; |
373 | |
374 | /// log<sub>10</sub>(e) |
375 | #[stable (feature = "rust1" , since = "1.0.0" )] |
376 | pub const LOG10_E: f64 = 0.434294481903251827651128918916605082_f64; |
377 | |
378 | /// ln(2) |
379 | #[stable (feature = "rust1" , since = "1.0.0" )] |
380 | pub const LN_2: f64 = 0.693147180559945309417232121458176568_f64; |
381 | |
382 | /// ln(10) |
383 | #[stable (feature = "rust1" , since = "1.0.0" )] |
384 | pub const LN_10: f64 = 2.30258509299404568401799145468436421_f64; |
385 | } |
386 | |
387 | impl f64 { |
388 | /// The radix or base of the internal representation of `f64`. |
389 | #[stable (feature = "assoc_int_consts" , since = "1.43.0" )] |
390 | pub const RADIX: u32 = 2; |
391 | |
392 | /// Number of significant digits in base 2. |
393 | #[stable (feature = "assoc_int_consts" , since = "1.43.0" )] |
394 | pub const MANTISSA_DIGITS: u32 = 53; |
395 | /// Approximate number of significant digits in base 10. |
396 | /// |
397 | /// This is the maximum <i>x</i> such that any decimal number with <i>x</i> |
398 | /// significant digits can be converted to `f64` and back without loss. |
399 | /// |
400 | /// Equal to floor(log<sub>10</sub> 2<sup>[`MANTISSA_DIGITS`] − 1</sup>). |
401 | /// |
402 | /// [`MANTISSA_DIGITS`]: f64::MANTISSA_DIGITS |
403 | #[stable (feature = "assoc_int_consts" , since = "1.43.0" )] |
404 | pub const DIGITS: u32 = 15; |
405 | |
406 | /// [Machine epsilon] value for `f64`. |
407 | /// |
408 | /// This is the difference between `1.0` and the next larger representable number. |
409 | /// |
410 | /// Equal to 2<sup>1 − [`MANTISSA_DIGITS`]</sup>. |
411 | /// |
412 | /// [Machine epsilon]: https://en.wikipedia.org/wiki/Machine_epsilon |
413 | /// [`MANTISSA_DIGITS`]: f64::MANTISSA_DIGITS |
414 | #[stable (feature = "assoc_int_consts" , since = "1.43.0" )] |
415 | #[rustc_diagnostic_item = "f64_epsilon" ] |
416 | pub const EPSILON: f64 = 2.2204460492503131e-16_f64; |
417 | |
418 | /// Smallest finite `f64` value. |
419 | /// |
420 | /// Equal to −[`MAX`]. |
421 | /// |
422 | /// [`MAX`]: f64::MAX |
423 | #[stable (feature = "assoc_int_consts" , since = "1.43.0" )] |
424 | pub const MIN: f64 = -1.7976931348623157e+308_f64; |
425 | /// Smallest positive normal `f64` value. |
426 | /// |
427 | /// Equal to 2<sup>[`MIN_EXP`] − 1</sup>. |
428 | /// |
429 | /// [`MIN_EXP`]: f64::MIN_EXP |
430 | #[stable (feature = "assoc_int_consts" , since = "1.43.0" )] |
431 | pub const MIN_POSITIVE: f64 = 2.2250738585072014e-308_f64; |
432 | /// Largest finite `f64` value. |
433 | /// |
434 | /// Equal to |
435 | /// (1 − 2<sup>−[`MANTISSA_DIGITS`]</sup>) 2<sup>[`MAX_EXP`]</sup>. |
436 | /// |
437 | /// [`MANTISSA_DIGITS`]: f64::MANTISSA_DIGITS |
438 | /// [`MAX_EXP`]: f64::MAX_EXP |
439 | #[stable (feature = "assoc_int_consts" , since = "1.43.0" )] |
440 | pub const MAX: f64 = 1.7976931348623157e+308_f64; |
441 | |
442 | /// One greater than the minimum possible normal power of 2 exponent. |
443 | /// |
444 | /// If <i>x</i> = `MIN_EXP`, then normal numbers |
445 | /// ≥ 0.5 × 2<sup><i>x</i></sup>. |
446 | #[stable (feature = "assoc_int_consts" , since = "1.43.0" )] |
447 | pub const MIN_EXP: i32 = -1021; |
448 | /// Maximum possible power of 2 exponent. |
449 | /// |
450 | /// If <i>x</i> = `MAX_EXP`, then normal numbers |
451 | /// < 1 × 2<sup><i>x</i></sup>. |
452 | #[stable (feature = "assoc_int_consts" , since = "1.43.0" )] |
453 | pub const MAX_EXP: i32 = 1024; |
454 | |
455 | /// Minimum <i>x</i> for which 10<sup><i>x</i></sup> is normal. |
456 | /// |
457 | /// Equal to ceil(log<sub>10</sub> [`MIN_POSITIVE`]). |
458 | /// |
459 | /// [`MIN_POSITIVE`]: f64::MIN_POSITIVE |
460 | #[stable (feature = "assoc_int_consts" , since = "1.43.0" )] |
461 | pub const MIN_10_EXP: i32 = -307; |
462 | /// Maximum <i>x</i> for which 10<sup><i>x</i></sup> is normal. |
463 | /// |
464 | /// Equal to floor(log<sub>10</sub> [`MAX`]). |
465 | /// |
466 | /// [`MAX`]: f64::MAX |
467 | #[stable (feature = "assoc_int_consts" , since = "1.43.0" )] |
468 | pub const MAX_10_EXP: i32 = 308; |
469 | |
470 | /// Not a Number (NaN). |
471 | /// |
472 | /// Note that IEEE 754 doesn't define just a single NaN value; |
473 | /// a plethora of bit patterns are considered to be NaN. |
474 | /// Furthermore, the standard makes a difference |
475 | /// between a "signaling" and a "quiet" NaN, |
476 | /// and allows inspecting its "payload" (the unspecified bits in the bit pattern). |
477 | /// This constant isn't guaranteed to equal to any specific NaN bitpattern, |
478 | /// and the stability of its representation over Rust versions |
479 | /// and target platforms isn't guaranteed. |
480 | #[rustc_diagnostic_item = "f64_nan" ] |
481 | #[stable (feature = "assoc_int_consts" , since = "1.43.0" )] |
482 | #[allow (clippy::eq_op)] |
483 | pub const NAN: f64 = 0.0_f64 / 0.0_f64; |
484 | /// Infinity (∞). |
485 | #[stable (feature = "assoc_int_consts" , since = "1.43.0" )] |
486 | pub const INFINITY: f64 = 1.0_f64 / 0.0_f64; |
487 | /// Negative infinity (−∞). |
488 | #[stable (feature = "assoc_int_consts" , since = "1.43.0" )] |
489 | pub const NEG_INFINITY: f64 = -1.0_f64 / 0.0_f64; |
490 | |
491 | /// Sign bit |
492 | pub(crate) const SIGN_MASK: u64 = 0x8000_0000_0000_0000; |
493 | |
494 | /// Exponent mask |
495 | pub(crate) const EXP_MASK: u64 = 0x7ff0_0000_0000_0000; |
496 | |
497 | /// Mantissa mask |
498 | pub(crate) const MAN_MASK: u64 = 0x000f_ffff_ffff_ffff; |
499 | |
500 | /// Minimum representable positive value (min subnormal) |
501 | const TINY_BITS: u64 = 0x1; |
502 | |
503 | /// Minimum representable negative value (min negative subnormal) |
504 | const NEG_TINY_BITS: u64 = Self::TINY_BITS | Self::SIGN_MASK; |
505 | |
506 | /// Returns `true` if this value is NaN. |
507 | /// |
508 | /// ``` |
509 | /// let nan = f64::NAN; |
510 | /// let f = 7.0_f64; |
511 | /// |
512 | /// assert!(nan.is_nan()); |
513 | /// assert!(!f.is_nan()); |
514 | /// ``` |
515 | #[must_use ] |
516 | #[stable (feature = "rust1" , since = "1.0.0" )] |
517 | #[rustc_const_stable (feature = "const_float_classify" , since = "1.83.0" )] |
518 | #[inline ] |
519 | #[allow (clippy::eq_op)] // > if you intended to check if the operand is NaN, use `.is_nan()` instead :) |
520 | pub const fn is_nan(self) -> bool { |
521 | self != self |
522 | } |
523 | |
524 | /// Returns `true` if this value is positive infinity or negative infinity, and |
525 | /// `false` otherwise. |
526 | /// |
527 | /// ``` |
528 | /// let f = 7.0f64; |
529 | /// let inf = f64::INFINITY; |
530 | /// let neg_inf = f64::NEG_INFINITY; |
531 | /// let nan = f64::NAN; |
532 | /// |
533 | /// assert!(!f.is_infinite()); |
534 | /// assert!(!nan.is_infinite()); |
535 | /// |
536 | /// assert!(inf.is_infinite()); |
537 | /// assert!(neg_inf.is_infinite()); |
538 | /// ``` |
539 | #[must_use ] |
540 | #[stable (feature = "rust1" , since = "1.0.0" )] |
541 | #[rustc_const_stable (feature = "const_float_classify" , since = "1.83.0" )] |
542 | #[inline ] |
543 | pub const fn is_infinite(self) -> bool { |
544 | // Getting clever with transmutation can result in incorrect answers on some FPUs |
545 | // FIXME: alter the Rust <-> Rust calling convention to prevent this problem. |
546 | // See https://github.com/rust-lang/rust/issues/72327 |
547 | (self == f64::INFINITY) | (self == f64::NEG_INFINITY) |
548 | } |
549 | |
550 | /// Returns `true` if this number is neither infinite nor NaN. |
551 | /// |
552 | /// ``` |
553 | /// let f = 7.0f64; |
554 | /// let inf: f64 = f64::INFINITY; |
555 | /// let neg_inf: f64 = f64::NEG_INFINITY; |
556 | /// let nan: f64 = f64::NAN; |
557 | /// |
558 | /// assert!(f.is_finite()); |
559 | /// |
560 | /// assert!(!nan.is_finite()); |
561 | /// assert!(!inf.is_finite()); |
562 | /// assert!(!neg_inf.is_finite()); |
563 | /// ``` |
564 | #[must_use ] |
565 | #[stable (feature = "rust1" , since = "1.0.0" )] |
566 | #[rustc_const_stable (feature = "const_float_classify" , since = "1.83.0" )] |
567 | #[inline ] |
568 | pub const fn is_finite(self) -> bool { |
569 | // There's no need to handle NaN separately: if self is NaN, |
570 | // the comparison is not true, exactly as desired. |
571 | self.abs() < Self::INFINITY |
572 | } |
573 | |
574 | /// Returns `true` if the number is [subnormal]. |
575 | /// |
576 | /// ``` |
577 | /// let min = f64::MIN_POSITIVE; // 2.2250738585072014e-308_f64 |
578 | /// let max = f64::MAX; |
579 | /// let lower_than_min = 1.0e-308_f64; |
580 | /// let zero = 0.0_f64; |
581 | /// |
582 | /// assert!(!min.is_subnormal()); |
583 | /// assert!(!max.is_subnormal()); |
584 | /// |
585 | /// assert!(!zero.is_subnormal()); |
586 | /// assert!(!f64::NAN.is_subnormal()); |
587 | /// assert!(!f64::INFINITY.is_subnormal()); |
588 | /// // Values between `0` and `min` are Subnormal. |
589 | /// assert!(lower_than_min.is_subnormal()); |
590 | /// ``` |
591 | /// [subnormal]: https://en.wikipedia.org/wiki/Denormal_number |
592 | #[must_use ] |
593 | #[stable (feature = "is_subnormal" , since = "1.53.0" )] |
594 | #[rustc_const_stable (feature = "const_float_classify" , since = "1.83.0" )] |
595 | #[inline ] |
596 | pub const fn is_subnormal(self) -> bool { |
597 | matches!(self.classify(), FpCategory::Subnormal) |
598 | } |
599 | |
600 | /// Returns `true` if the number is neither zero, infinite, |
601 | /// [subnormal], or NaN. |
602 | /// |
603 | /// ``` |
604 | /// let min = f64::MIN_POSITIVE; // 2.2250738585072014e-308f64 |
605 | /// let max = f64::MAX; |
606 | /// let lower_than_min = 1.0e-308_f64; |
607 | /// let zero = 0.0f64; |
608 | /// |
609 | /// assert!(min.is_normal()); |
610 | /// assert!(max.is_normal()); |
611 | /// |
612 | /// assert!(!zero.is_normal()); |
613 | /// assert!(!f64::NAN.is_normal()); |
614 | /// assert!(!f64::INFINITY.is_normal()); |
615 | /// // Values between `0` and `min` are Subnormal. |
616 | /// assert!(!lower_than_min.is_normal()); |
617 | /// ``` |
618 | /// [subnormal]: https://en.wikipedia.org/wiki/Denormal_number |
619 | #[must_use ] |
620 | #[stable (feature = "rust1" , since = "1.0.0" )] |
621 | #[rustc_const_stable (feature = "const_float_classify" , since = "1.83.0" )] |
622 | #[inline ] |
623 | pub const fn is_normal(self) -> bool { |
624 | matches!(self.classify(), FpCategory::Normal) |
625 | } |
626 | |
627 | /// Returns the floating point category of the number. If only one property |
628 | /// is going to be tested, it is generally faster to use the specific |
629 | /// predicate instead. |
630 | /// |
631 | /// ``` |
632 | /// use std::num::FpCategory; |
633 | /// |
634 | /// let num = 12.4_f64; |
635 | /// let inf = f64::INFINITY; |
636 | /// |
637 | /// assert_eq!(num.classify(), FpCategory::Normal); |
638 | /// assert_eq!(inf.classify(), FpCategory::Infinite); |
639 | /// ``` |
640 | #[stable (feature = "rust1" , since = "1.0.0" )] |
641 | #[rustc_const_stable (feature = "const_float_classify" , since = "1.83.0" )] |
642 | pub const fn classify(self) -> FpCategory { |
643 | // We used to have complicated logic here that avoids the simple bit-based tests to work |
644 | // around buggy codegen for x87 targets (see |
645 | // https://github.com/rust-lang/rust/issues/114479). However, some LLVM versions later, none |
646 | // of our tests is able to find any difference between the complicated and the naive |
647 | // version, so now we are back to the naive version. |
648 | let b = self.to_bits(); |
649 | match (b & Self::MAN_MASK, b & Self::EXP_MASK) { |
650 | (0, Self::EXP_MASK) => FpCategory::Infinite, |
651 | (_, Self::EXP_MASK) => FpCategory::Nan, |
652 | (0, 0) => FpCategory::Zero, |
653 | (_, 0) => FpCategory::Subnormal, |
654 | _ => FpCategory::Normal, |
655 | } |
656 | } |
657 | |
658 | /// Returns `true` if `self` has a positive sign, including `+0.0`, NaNs with |
659 | /// positive sign bit and positive infinity. |
660 | /// |
661 | /// Note that IEEE 754 doesn't assign any meaning to the sign bit in case of |
662 | /// a NaN, and as Rust doesn't guarantee that the bit pattern of NaNs are |
663 | /// conserved over arithmetic operations, the result of `is_sign_positive` on |
664 | /// a NaN might produce an unexpected or non-portable result. See the [specification |
665 | /// of NaN bit patterns](f32#nan-bit-patterns) for more info. Use `self.signum() == 1.0` |
666 | /// if you need fully portable behavior (will return `false` for all NaNs). |
667 | /// |
668 | /// ``` |
669 | /// let f = 7.0_f64; |
670 | /// let g = -7.0_f64; |
671 | /// |
672 | /// assert!(f.is_sign_positive()); |
673 | /// assert!(!g.is_sign_positive()); |
674 | /// ``` |
675 | #[must_use ] |
676 | #[stable (feature = "rust1" , since = "1.0.0" )] |
677 | #[rustc_const_stable (feature = "const_float_classify" , since = "1.83.0" )] |
678 | #[inline ] |
679 | pub const fn is_sign_positive(self) -> bool { |
680 | !self.is_sign_negative() |
681 | } |
682 | |
683 | #[must_use ] |
684 | #[stable (feature = "rust1" , since = "1.0.0" )] |
685 | #[deprecated (since = "1.0.0" , note = "renamed to is_sign_positive" )] |
686 | #[inline ] |
687 | #[doc (hidden)] |
688 | pub fn is_positive(self) -> bool { |
689 | self.is_sign_positive() |
690 | } |
691 | |
692 | /// Returns `true` if `self` has a negative sign, including `-0.0`, NaNs with |
693 | /// negative sign bit and negative infinity. |
694 | /// |
695 | /// Note that IEEE 754 doesn't assign any meaning to the sign bit in case of |
696 | /// a NaN, and as Rust doesn't guarantee that the bit pattern of NaNs are |
697 | /// conserved over arithmetic operations, the result of `is_sign_negative` on |
698 | /// a NaN might produce an unexpected or non-portable result. See the [specification |
699 | /// of NaN bit patterns](f32#nan-bit-patterns) for more info. Use `self.signum() == -1.0` |
700 | /// if you need fully portable behavior (will return `false` for all NaNs). |
701 | /// |
702 | /// ``` |
703 | /// let f = 7.0_f64; |
704 | /// let g = -7.0_f64; |
705 | /// |
706 | /// assert!(!f.is_sign_negative()); |
707 | /// assert!(g.is_sign_negative()); |
708 | /// ``` |
709 | #[must_use ] |
710 | #[stable (feature = "rust1" , since = "1.0.0" )] |
711 | #[rustc_const_stable (feature = "const_float_classify" , since = "1.83.0" )] |
712 | #[inline ] |
713 | pub const fn is_sign_negative(self) -> bool { |
714 | // IEEE754 says: isSignMinus(x) is true if and only if x has negative sign. isSignMinus |
715 | // applies to zeros and NaNs as well. |
716 | // SAFETY: This is just transmuting to get the sign bit, it's fine. |
717 | unsafe { mem::transmute::<f64, u64>(self) & Self::SIGN_MASK != 0 } |
718 | } |
719 | |
720 | #[must_use ] |
721 | #[stable (feature = "rust1" , since = "1.0.0" )] |
722 | #[deprecated (since = "1.0.0" , note = "renamed to is_sign_negative" )] |
723 | #[inline ] |
724 | #[doc (hidden)] |
725 | pub fn is_negative(self) -> bool { |
726 | self.is_sign_negative() |
727 | } |
728 | |
729 | /// Returns the least number greater than `self`. |
730 | /// |
731 | /// Let `TINY` be the smallest representable positive `f64`. Then, |
732 | /// - if `self.is_nan()`, this returns `self`; |
733 | /// - if `self` is [`NEG_INFINITY`], this returns [`MIN`]; |
734 | /// - if `self` is `-TINY`, this returns -0.0; |
735 | /// - if `self` is -0.0 or +0.0, this returns `TINY`; |
736 | /// - if `self` is [`MAX`] or [`INFINITY`], this returns [`INFINITY`]; |
737 | /// - otherwise the unique least value greater than `self` is returned. |
738 | /// |
739 | /// The identity `x.next_up() == -(-x).next_down()` holds for all non-NaN `x`. When `x` |
740 | /// is finite `x == x.next_up().next_down()` also holds. |
741 | /// |
742 | /// ```rust |
743 | /// // f64::EPSILON is the difference between 1.0 and the next number up. |
744 | /// assert_eq!(1.0f64.next_up(), 1.0 + f64::EPSILON); |
745 | /// // But not for most numbers. |
746 | /// assert!(0.1f64.next_up() < 0.1 + f64::EPSILON); |
747 | /// assert_eq!(9007199254740992f64.next_up(), 9007199254740994.0); |
748 | /// ``` |
749 | /// |
750 | /// This operation corresponds to IEEE-754 `nextUp`. |
751 | /// |
752 | /// [`NEG_INFINITY`]: Self::NEG_INFINITY |
753 | /// [`INFINITY`]: Self::INFINITY |
754 | /// [`MIN`]: Self::MIN |
755 | /// [`MAX`]: Self::MAX |
756 | #[inline ] |
757 | #[doc (alias = "nextUp" )] |
758 | #[stable (feature = "float_next_up_down" , since = "1.86.0" )] |
759 | #[rustc_const_stable (feature = "float_next_up_down" , since = "1.86.0" )] |
760 | pub const fn next_up(self) -> Self { |
761 | // Some targets violate Rust's assumption of IEEE semantics, e.g. by flushing |
762 | // denormals to zero. This is in general unsound and unsupported, but here |
763 | // we do our best to still produce the correct result on such targets. |
764 | let bits = self.to_bits(); |
765 | if self.is_nan() || bits == Self::INFINITY.to_bits() { |
766 | return self; |
767 | } |
768 | |
769 | let abs = bits & !Self::SIGN_MASK; |
770 | let next_bits = if abs == 0 { |
771 | Self::TINY_BITS |
772 | } else if bits == abs { |
773 | bits + 1 |
774 | } else { |
775 | bits - 1 |
776 | }; |
777 | Self::from_bits(next_bits) |
778 | } |
779 | |
780 | /// Returns the greatest number less than `self`. |
781 | /// |
782 | /// Let `TINY` be the smallest representable positive `f64`. Then, |
783 | /// - if `self.is_nan()`, this returns `self`; |
784 | /// - if `self` is [`INFINITY`], this returns [`MAX`]; |
785 | /// - if `self` is `TINY`, this returns 0.0; |
786 | /// - if `self` is -0.0 or +0.0, this returns `-TINY`; |
787 | /// - if `self` is [`MIN`] or [`NEG_INFINITY`], this returns [`NEG_INFINITY`]; |
788 | /// - otherwise the unique greatest value less than `self` is returned. |
789 | /// |
790 | /// The identity `x.next_down() == -(-x).next_up()` holds for all non-NaN `x`. When `x` |
791 | /// is finite `x == x.next_down().next_up()` also holds. |
792 | /// |
793 | /// ```rust |
794 | /// let x = 1.0f64; |
795 | /// // Clamp value into range [0, 1). |
796 | /// let clamped = x.clamp(0.0, 1.0f64.next_down()); |
797 | /// assert!(clamped < 1.0); |
798 | /// assert_eq!(clamped.next_up(), 1.0); |
799 | /// ``` |
800 | /// |
801 | /// This operation corresponds to IEEE-754 `nextDown`. |
802 | /// |
803 | /// [`NEG_INFINITY`]: Self::NEG_INFINITY |
804 | /// [`INFINITY`]: Self::INFINITY |
805 | /// [`MIN`]: Self::MIN |
806 | /// [`MAX`]: Self::MAX |
807 | #[inline ] |
808 | #[doc (alias = "nextDown" )] |
809 | #[stable (feature = "float_next_up_down" , since = "1.86.0" )] |
810 | #[rustc_const_stable (feature = "float_next_up_down" , since = "1.86.0" )] |
811 | pub const fn next_down(self) -> Self { |
812 | // Some targets violate Rust's assumption of IEEE semantics, e.g. by flushing |
813 | // denormals to zero. This is in general unsound and unsupported, but here |
814 | // we do our best to still produce the correct result on such targets. |
815 | let bits = self.to_bits(); |
816 | if self.is_nan() || bits == Self::NEG_INFINITY.to_bits() { |
817 | return self; |
818 | } |
819 | |
820 | let abs = bits & !Self::SIGN_MASK; |
821 | let next_bits = if abs == 0 { |
822 | Self::NEG_TINY_BITS |
823 | } else if bits == abs { |
824 | bits - 1 |
825 | } else { |
826 | bits + 1 |
827 | }; |
828 | Self::from_bits(next_bits) |
829 | } |
830 | |
831 | /// Takes the reciprocal (inverse) of a number, `1/x`. |
832 | /// |
833 | /// ``` |
834 | /// let x = 2.0_f64; |
835 | /// let abs_difference = (x.recip() - (1.0 / x)).abs(); |
836 | /// |
837 | /// assert!(abs_difference < 1e-10); |
838 | /// ``` |
839 | #[must_use = "this returns the result of the operation, without modifying the original" ] |
840 | #[stable (feature = "rust1" , since = "1.0.0" )] |
841 | #[rustc_const_stable (feature = "const_float_methods" , since = "1.85.0" )] |
842 | #[inline ] |
843 | pub const fn recip(self) -> f64 { |
844 | 1.0 / self |
845 | } |
846 | |
847 | /// Converts radians to degrees. |
848 | /// |
849 | /// ``` |
850 | /// let angle = std::f64::consts::PI; |
851 | /// |
852 | /// let abs_difference = (angle.to_degrees() - 180.0).abs(); |
853 | /// |
854 | /// assert!(abs_difference < 1e-10); |
855 | /// ``` |
856 | #[must_use = "this returns the result of the operation, \ |
857 | without modifying the original" ] |
858 | #[stable (feature = "rust1" , since = "1.0.0" )] |
859 | #[rustc_const_stable (feature = "const_float_methods" , since = "1.85.0" )] |
860 | #[inline ] |
861 | pub const fn to_degrees(self) -> f64 { |
862 | // The division here is correctly rounded with respect to the true |
863 | // value of 180/π. (This differs from f32, where a constant must be |
864 | // used to ensure a correctly rounded result.) |
865 | self * (180.0f64 / consts::PI) |
866 | } |
867 | |
868 | /// Converts degrees to radians. |
869 | /// |
870 | /// ``` |
871 | /// let angle = 180.0_f64; |
872 | /// |
873 | /// let abs_difference = (angle.to_radians() - std::f64::consts::PI).abs(); |
874 | /// |
875 | /// assert!(abs_difference < 1e-10); |
876 | /// ``` |
877 | #[must_use = "this returns the result of the operation, \ |
878 | without modifying the original" ] |
879 | #[stable (feature = "rust1" , since = "1.0.0" )] |
880 | #[rustc_const_stable (feature = "const_float_methods" , since = "1.85.0" )] |
881 | #[inline ] |
882 | pub const fn to_radians(self) -> f64 { |
883 | const RADS_PER_DEG: f64 = consts::PI / 180.0; |
884 | self * RADS_PER_DEG |
885 | } |
886 | |
887 | /// Returns the maximum of the two numbers, ignoring NaN. |
888 | /// |
889 | /// If one of the arguments is NaN, then the other argument is returned. |
890 | /// This follows the IEEE 754-2008 semantics for maxNum, except for handling of signaling NaNs; |
891 | /// this function handles all NaNs the same way and avoids maxNum's problems with associativity. |
892 | /// This also matches the behavior of libm’s fmax. In particular, if the inputs compare equal |
893 | /// (such as for the case of `+0.0` and `-0.0`), either input may be returned non-deterministically. |
894 | /// |
895 | /// ``` |
896 | /// let x = 1.0_f64; |
897 | /// let y = 2.0_f64; |
898 | /// |
899 | /// assert_eq!(x.max(y), y); |
900 | /// ``` |
901 | #[must_use = "this returns the result of the comparison, without modifying either input" ] |
902 | #[stable (feature = "rust1" , since = "1.0.0" )] |
903 | #[rustc_const_stable (feature = "const_float_methods" , since = "1.85.0" )] |
904 | #[inline ] |
905 | pub const fn max(self, other: f64) -> f64 { |
906 | intrinsics::maxnumf64(self, other) |
907 | } |
908 | |
909 | /// Returns the minimum of the two numbers, ignoring NaN. |
910 | /// |
911 | /// If one of the arguments is NaN, then the other argument is returned. |
912 | /// This follows the IEEE 754-2008 semantics for minNum, except for handling of signaling NaNs; |
913 | /// this function handles all NaNs the same way and avoids minNum's problems with associativity. |
914 | /// This also matches the behavior of libm’s fmin. In particular, if the inputs compare equal |
915 | /// (such as for the case of `+0.0` and `-0.0`), either input may be returned non-deterministically. |
916 | /// |
917 | /// ``` |
918 | /// let x = 1.0_f64; |
919 | /// let y = 2.0_f64; |
920 | /// |
921 | /// assert_eq!(x.min(y), x); |
922 | /// ``` |
923 | #[must_use = "this returns the result of the comparison, without modifying either input" ] |
924 | #[stable (feature = "rust1" , since = "1.0.0" )] |
925 | #[rustc_const_stable (feature = "const_float_methods" , since = "1.85.0" )] |
926 | #[inline ] |
927 | pub const fn min(self, other: f64) -> f64 { |
928 | intrinsics::minnumf64(self, other) |
929 | } |
930 | |
931 | /// Returns the maximum of the two numbers, propagating NaN. |
932 | /// |
933 | /// This returns NaN when *either* argument is NaN, as opposed to |
934 | /// [`f64::max`] which only returns NaN when *both* arguments are NaN. |
935 | /// |
936 | /// ``` |
937 | /// #![feature(float_minimum_maximum)] |
938 | /// let x = 1.0_f64; |
939 | /// let y = 2.0_f64; |
940 | /// |
941 | /// assert_eq!(x.maximum(y), y); |
942 | /// assert!(x.maximum(f64::NAN).is_nan()); |
943 | /// ``` |
944 | /// |
945 | /// If one of the arguments is NaN, then NaN is returned. Otherwise this returns the greater |
946 | /// of the two numbers. For this operation, -0.0 is considered to be less than +0.0. |
947 | /// Note that this follows the semantics specified in IEEE 754-2019. |
948 | /// |
949 | /// Also note that "propagation" of NaNs here doesn't necessarily mean that the bitpattern of a NaN |
950 | /// operand is conserved; see the [specification of NaN bit patterns](f32#nan-bit-patterns) for more info. |
951 | #[must_use = "this returns the result of the comparison, without modifying either input" ] |
952 | #[unstable (feature = "float_minimum_maximum" , issue = "91079" )] |
953 | #[inline ] |
954 | pub const fn maximum(self, other: f64) -> f64 { |
955 | if self > other { |
956 | self |
957 | } else if other > self { |
958 | other |
959 | } else if self == other { |
960 | if self.is_sign_positive() && other.is_sign_negative() { self } else { other } |
961 | } else { |
962 | self + other |
963 | } |
964 | } |
965 | |
966 | /// Returns the minimum of the two numbers, propagating NaN. |
967 | /// |
968 | /// This returns NaN when *either* argument is NaN, as opposed to |
969 | /// [`f64::min`] which only returns NaN when *both* arguments are NaN. |
970 | /// |
971 | /// ``` |
972 | /// #![feature(float_minimum_maximum)] |
973 | /// let x = 1.0_f64; |
974 | /// let y = 2.0_f64; |
975 | /// |
976 | /// assert_eq!(x.minimum(y), x); |
977 | /// assert!(x.minimum(f64::NAN).is_nan()); |
978 | /// ``` |
979 | /// |
980 | /// If one of the arguments is NaN, then NaN is returned. Otherwise this returns the lesser |
981 | /// of the two numbers. For this operation, -0.0 is considered to be less than +0.0. |
982 | /// Note that this follows the semantics specified in IEEE 754-2019. |
983 | /// |
984 | /// Also note that "propagation" of NaNs here doesn't necessarily mean that the bitpattern of a NaN |
985 | /// operand is conserved; see the [specification of NaN bit patterns](f32#nan-bit-patterns) for more info. |
986 | #[must_use = "this returns the result of the comparison, without modifying either input" ] |
987 | #[unstable (feature = "float_minimum_maximum" , issue = "91079" )] |
988 | #[inline ] |
989 | pub const fn minimum(self, other: f64) -> f64 { |
990 | if self < other { |
991 | self |
992 | } else if other < self { |
993 | other |
994 | } else if self == other { |
995 | if self.is_sign_negative() && other.is_sign_positive() { self } else { other } |
996 | } else { |
997 | // At least one input is NaN. Use `+` to perform NaN propagation and quieting. |
998 | self + other |
999 | } |
1000 | } |
1001 | |
1002 | /// Calculates the middle point of `self` and `rhs`. |
1003 | /// |
1004 | /// This returns NaN when *either* argument is NaN or if a combination of |
1005 | /// +inf and -inf is provided as arguments. |
1006 | /// |
1007 | /// # Examples |
1008 | /// |
1009 | /// ``` |
1010 | /// assert_eq!(1f64.midpoint(4.0), 2.5); |
1011 | /// assert_eq!((-5.5f64).midpoint(8.0), 1.25); |
1012 | /// ``` |
1013 | #[inline ] |
1014 | #[stable (feature = "num_midpoint" , since = "1.85.0" )] |
1015 | #[rustc_const_stable (feature = "num_midpoint" , since = "1.85.0" )] |
1016 | pub const fn midpoint(self, other: f64) -> f64 { |
1017 | const LO: f64 = f64::MIN_POSITIVE * 2.; |
1018 | const HI: f64 = f64::MAX / 2.; |
1019 | |
1020 | let (a, b) = (self, other); |
1021 | let abs_a = a.abs(); |
1022 | let abs_b = b.abs(); |
1023 | |
1024 | if abs_a <= HI && abs_b <= HI { |
1025 | // Overflow is impossible |
1026 | (a + b) / 2. |
1027 | } else if abs_a < LO { |
1028 | // Not safe to halve `a` (would underflow) |
1029 | a + (b / 2.) |
1030 | } else if abs_b < LO { |
1031 | // Not safe to halve `b` (would underflow) |
1032 | (a / 2.) + b |
1033 | } else { |
1034 | // Safe to halve `a` and `b` |
1035 | (a / 2.) + (b / 2.) |
1036 | } |
1037 | } |
1038 | |
1039 | /// Rounds toward zero and converts to any primitive integer type, |
1040 | /// assuming that the value is finite and fits in that type. |
1041 | /// |
1042 | /// ``` |
1043 | /// let value = 4.6_f64; |
1044 | /// let rounded = unsafe { value.to_int_unchecked::<u16>() }; |
1045 | /// assert_eq!(rounded, 4); |
1046 | /// |
1047 | /// let value = -128.9_f64; |
1048 | /// let rounded = unsafe { value.to_int_unchecked::<i8>() }; |
1049 | /// assert_eq!(rounded, i8::MIN); |
1050 | /// ``` |
1051 | /// |
1052 | /// # Safety |
1053 | /// |
1054 | /// The value must: |
1055 | /// |
1056 | /// * Not be `NaN` |
1057 | /// * Not be infinite |
1058 | /// * Be representable in the return type `Int`, after truncating off its fractional part |
1059 | #[must_use = "this returns the result of the operation, \ |
1060 | without modifying the original" ] |
1061 | #[stable (feature = "float_approx_unchecked_to" , since = "1.44.0" )] |
1062 | #[inline ] |
1063 | pub unsafe fn to_int_unchecked<Int>(self) -> Int |
1064 | where |
1065 | Self: FloatToInt<Int>, |
1066 | { |
1067 | // SAFETY: the caller must uphold the safety contract for |
1068 | // `FloatToInt::to_int_unchecked`. |
1069 | unsafe { FloatToInt::<Int>::to_int_unchecked(self) } |
1070 | } |
1071 | |
1072 | /// Raw transmutation to `u64`. |
1073 | /// |
1074 | /// This is currently identical to `transmute::<f64, u64>(self)` on all platforms. |
1075 | /// |
1076 | /// See [`from_bits`](Self::from_bits) for some discussion of the |
1077 | /// portability of this operation (there are almost no issues). |
1078 | /// |
1079 | /// Note that this function is distinct from `as` casting, which attempts to |
1080 | /// preserve the *numeric* value, and not the bitwise value. |
1081 | /// |
1082 | /// # Examples |
1083 | /// |
1084 | /// ``` |
1085 | /// assert!((1f64).to_bits() != 1f64 as u64); // to_bits() is not casting! |
1086 | /// assert_eq!((12.5f64).to_bits(), 0x4029000000000000); |
1087 | /// ``` |
1088 | #[must_use = "this returns the result of the operation, \ |
1089 | without modifying the original" ] |
1090 | #[stable (feature = "float_bits_conv" , since = "1.20.0" )] |
1091 | #[rustc_const_stable (feature = "const_float_bits_conv" , since = "1.83.0" )] |
1092 | #[inline ] |
1093 | pub const fn to_bits(self) -> u64 { |
1094 | // SAFETY: `u64` is a plain old datatype so we can always transmute to it. |
1095 | unsafe { mem::transmute(self) } |
1096 | } |
1097 | |
1098 | /// Raw transmutation from `u64`. |
1099 | /// |
1100 | /// This is currently identical to `transmute::<u64, f64>(v)` on all platforms. |
1101 | /// It turns out this is incredibly portable, for two reasons: |
1102 | /// |
1103 | /// * Floats and Ints have the same endianness on all supported platforms. |
1104 | /// * IEEE 754 very precisely specifies the bit layout of floats. |
1105 | /// |
1106 | /// However there is one caveat: prior to the 2008 version of IEEE 754, how |
1107 | /// to interpret the NaN signaling bit wasn't actually specified. Most platforms |
1108 | /// (notably x86 and ARM) picked the interpretation that was ultimately |
1109 | /// standardized in 2008, but some didn't (notably MIPS). As a result, all |
1110 | /// signaling NaNs on MIPS are quiet NaNs on x86, and vice-versa. |
1111 | /// |
1112 | /// Rather than trying to preserve signaling-ness cross-platform, this |
1113 | /// implementation favors preserving the exact bits. This means that |
1114 | /// any payloads encoded in NaNs will be preserved even if the result of |
1115 | /// this method is sent over the network from an x86 machine to a MIPS one. |
1116 | /// |
1117 | /// If the results of this method are only manipulated by the same |
1118 | /// architecture that produced them, then there is no portability concern. |
1119 | /// |
1120 | /// If the input isn't NaN, then there is no portability concern. |
1121 | /// |
1122 | /// If you don't care about signaling-ness (very likely), then there is no |
1123 | /// portability concern. |
1124 | /// |
1125 | /// Note that this function is distinct from `as` casting, which attempts to |
1126 | /// preserve the *numeric* value, and not the bitwise value. |
1127 | /// |
1128 | /// # Examples |
1129 | /// |
1130 | /// ``` |
1131 | /// let v = f64::from_bits(0x4029000000000000); |
1132 | /// assert_eq!(v, 12.5); |
1133 | /// ``` |
1134 | #[stable (feature = "float_bits_conv" , since = "1.20.0" )] |
1135 | #[rustc_const_stable (feature = "const_float_bits_conv" , since = "1.83.0" )] |
1136 | #[must_use ] |
1137 | #[inline ] |
1138 | pub const fn from_bits(v: u64) -> Self { |
1139 | // It turns out the safety issues with sNaN were overblown! Hooray! |
1140 | // SAFETY: `u64` is a plain old datatype so we can always transmute from it. |
1141 | unsafe { mem::transmute(v) } |
1142 | } |
1143 | |
1144 | /// Returns the memory representation of this floating point number as a byte array in |
1145 | /// big-endian (network) byte order. |
1146 | /// |
1147 | /// See [`from_bits`](Self::from_bits) for some discussion of the |
1148 | /// portability of this operation (there are almost no issues). |
1149 | /// |
1150 | /// # Examples |
1151 | /// |
1152 | /// ``` |
1153 | /// let bytes = 12.5f64.to_be_bytes(); |
1154 | /// assert_eq!(bytes, [0x40, 0x29, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]); |
1155 | /// ``` |
1156 | #[must_use = "this returns the result of the operation, \ |
1157 | without modifying the original" ] |
1158 | #[stable (feature = "float_to_from_bytes" , since = "1.40.0" )] |
1159 | #[rustc_const_stable (feature = "const_float_bits_conv" , since = "1.83.0" )] |
1160 | #[inline ] |
1161 | pub const fn to_be_bytes(self) -> [u8; 8] { |
1162 | self.to_bits().to_be_bytes() |
1163 | } |
1164 | |
1165 | /// Returns the memory representation of this floating point number as a byte array in |
1166 | /// little-endian byte order. |
1167 | /// |
1168 | /// See [`from_bits`](Self::from_bits) for some discussion of the |
1169 | /// portability of this operation (there are almost no issues). |
1170 | /// |
1171 | /// # Examples |
1172 | /// |
1173 | /// ``` |
1174 | /// let bytes = 12.5f64.to_le_bytes(); |
1175 | /// assert_eq!(bytes, [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x29, 0x40]); |
1176 | /// ``` |
1177 | #[must_use = "this returns the result of the operation, \ |
1178 | without modifying the original" ] |
1179 | #[stable (feature = "float_to_from_bytes" , since = "1.40.0" )] |
1180 | #[rustc_const_stable (feature = "const_float_bits_conv" , since = "1.83.0" )] |
1181 | #[inline ] |
1182 | pub const fn to_le_bytes(self) -> [u8; 8] { |
1183 | self.to_bits().to_le_bytes() |
1184 | } |
1185 | |
1186 | /// Returns the memory representation of this floating point number as a byte array in |
1187 | /// native byte order. |
1188 | /// |
1189 | /// As the target platform's native endianness is used, portable code |
1190 | /// should use [`to_be_bytes`] or [`to_le_bytes`], as appropriate, instead. |
1191 | /// |
1192 | /// [`to_be_bytes`]: f64::to_be_bytes |
1193 | /// [`to_le_bytes`]: f64::to_le_bytes |
1194 | /// |
1195 | /// See [`from_bits`](Self::from_bits) for some discussion of the |
1196 | /// portability of this operation (there are almost no issues). |
1197 | /// |
1198 | /// # Examples |
1199 | /// |
1200 | /// ``` |
1201 | /// let bytes = 12.5f64.to_ne_bytes(); |
1202 | /// assert_eq!( |
1203 | /// bytes, |
1204 | /// if cfg!(target_endian = "big" ) { |
1205 | /// [0x40, 0x29, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00] |
1206 | /// } else { |
1207 | /// [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x29, 0x40] |
1208 | /// } |
1209 | /// ); |
1210 | /// ``` |
1211 | #[must_use = "this returns the result of the operation, \ |
1212 | without modifying the original" ] |
1213 | #[stable (feature = "float_to_from_bytes" , since = "1.40.0" )] |
1214 | #[rustc_const_stable (feature = "const_float_bits_conv" , since = "1.83.0" )] |
1215 | #[inline ] |
1216 | pub const fn to_ne_bytes(self) -> [u8; 8] { |
1217 | self.to_bits().to_ne_bytes() |
1218 | } |
1219 | |
1220 | /// Creates a floating point value from its representation as a byte array in big endian. |
1221 | /// |
1222 | /// See [`from_bits`](Self::from_bits) for some discussion of the |
1223 | /// portability of this operation (there are almost no issues). |
1224 | /// |
1225 | /// # Examples |
1226 | /// |
1227 | /// ``` |
1228 | /// let value = f64::from_be_bytes([0x40, 0x29, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]); |
1229 | /// assert_eq!(value, 12.5); |
1230 | /// ``` |
1231 | #[stable (feature = "float_to_from_bytes" , since = "1.40.0" )] |
1232 | #[rustc_const_stable (feature = "const_float_bits_conv" , since = "1.83.0" )] |
1233 | #[must_use ] |
1234 | #[inline ] |
1235 | pub const fn from_be_bytes(bytes: [u8; 8]) -> Self { |
1236 | Self::from_bits(u64::from_be_bytes(bytes)) |
1237 | } |
1238 | |
1239 | /// Creates a floating point value from its representation as a byte array in little endian. |
1240 | /// |
1241 | /// See [`from_bits`](Self::from_bits) for some discussion of the |
1242 | /// portability of this operation (there are almost no issues). |
1243 | /// |
1244 | /// # Examples |
1245 | /// |
1246 | /// ``` |
1247 | /// let value = f64::from_le_bytes([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x29, 0x40]); |
1248 | /// assert_eq!(value, 12.5); |
1249 | /// ``` |
1250 | #[stable (feature = "float_to_from_bytes" , since = "1.40.0" )] |
1251 | #[rustc_const_stable (feature = "const_float_bits_conv" , since = "1.83.0" )] |
1252 | #[must_use ] |
1253 | #[inline ] |
1254 | pub const fn from_le_bytes(bytes: [u8; 8]) -> Self { |
1255 | Self::from_bits(u64::from_le_bytes(bytes)) |
1256 | } |
1257 | |
1258 | /// Creates a floating point value from its representation as a byte array in native endian. |
1259 | /// |
1260 | /// As the target platform's native endianness is used, portable code |
1261 | /// likely wants to use [`from_be_bytes`] or [`from_le_bytes`], as |
1262 | /// appropriate instead. |
1263 | /// |
1264 | /// [`from_be_bytes`]: f64::from_be_bytes |
1265 | /// [`from_le_bytes`]: f64::from_le_bytes |
1266 | /// |
1267 | /// See [`from_bits`](Self::from_bits) for some discussion of the |
1268 | /// portability of this operation (there are almost no issues). |
1269 | /// |
1270 | /// # Examples |
1271 | /// |
1272 | /// ``` |
1273 | /// let value = f64::from_ne_bytes(if cfg!(target_endian = "big" ) { |
1274 | /// [0x40, 0x29, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00] |
1275 | /// } else { |
1276 | /// [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x29, 0x40] |
1277 | /// }); |
1278 | /// assert_eq!(value, 12.5); |
1279 | /// ``` |
1280 | #[stable (feature = "float_to_from_bytes" , since = "1.40.0" )] |
1281 | #[rustc_const_stable (feature = "const_float_bits_conv" , since = "1.83.0" )] |
1282 | #[must_use ] |
1283 | #[inline ] |
1284 | pub const fn from_ne_bytes(bytes: [u8; 8]) -> Self { |
1285 | Self::from_bits(u64::from_ne_bytes(bytes)) |
1286 | } |
1287 | |
1288 | /// Returns the ordering between `self` and `other`. |
1289 | /// |
1290 | /// Unlike the standard partial comparison between floating point numbers, |
1291 | /// this comparison always produces an ordering in accordance to |
1292 | /// the `totalOrder` predicate as defined in the IEEE 754 (2008 revision) |
1293 | /// floating point standard. The values are ordered in the following sequence: |
1294 | /// |
1295 | /// - negative quiet NaN |
1296 | /// - negative signaling NaN |
1297 | /// - negative infinity |
1298 | /// - negative numbers |
1299 | /// - negative subnormal numbers |
1300 | /// - negative zero |
1301 | /// - positive zero |
1302 | /// - positive subnormal numbers |
1303 | /// - positive numbers |
1304 | /// - positive infinity |
1305 | /// - positive signaling NaN |
1306 | /// - positive quiet NaN. |
1307 | /// |
1308 | /// The ordering established by this function does not always agree with the |
1309 | /// [`PartialOrd`] and [`PartialEq`] implementations of `f64`. For example, |
1310 | /// they consider negative and positive zero equal, while `total_cmp` |
1311 | /// doesn't. |
1312 | /// |
1313 | /// The interpretation of the signaling NaN bit follows the definition in |
1314 | /// the IEEE 754 standard, which may not match the interpretation by some of |
1315 | /// the older, non-conformant (e.g. MIPS) hardware implementations. |
1316 | /// |
1317 | /// # Example |
1318 | /// |
1319 | /// ``` |
1320 | /// struct GoodBoy { |
1321 | /// name: String, |
1322 | /// weight: f64, |
1323 | /// } |
1324 | /// |
1325 | /// let mut bois = vec![ |
1326 | /// GoodBoy { name: "Pucci" .to_owned(), weight: 0.1 }, |
1327 | /// GoodBoy { name: "Woofer" .to_owned(), weight: 99.0 }, |
1328 | /// GoodBoy { name: "Yapper" .to_owned(), weight: 10.0 }, |
1329 | /// GoodBoy { name: "Chonk" .to_owned(), weight: f64::INFINITY }, |
1330 | /// GoodBoy { name: "Abs. Unit" .to_owned(), weight: f64::NAN }, |
1331 | /// GoodBoy { name: "Floaty" .to_owned(), weight: -5.0 }, |
1332 | /// ]; |
1333 | /// |
1334 | /// bois.sort_by(|a, b| a.weight.total_cmp(&b.weight)); |
1335 | /// |
1336 | /// // `f64::NAN` could be positive or negative, which will affect the sort order. |
1337 | /// if f64::NAN.is_sign_negative() { |
1338 | /// assert!(bois.into_iter().map(|b| b.weight) |
1339 | /// .zip([f64::NAN, -5.0, 0.1, 10.0, 99.0, f64::INFINITY].iter()) |
1340 | /// .all(|(a, b)| a.to_bits() == b.to_bits())) |
1341 | /// } else { |
1342 | /// assert!(bois.into_iter().map(|b| b.weight) |
1343 | /// .zip([-5.0, 0.1, 10.0, 99.0, f64::INFINITY, f64::NAN].iter()) |
1344 | /// .all(|(a, b)| a.to_bits() == b.to_bits())) |
1345 | /// } |
1346 | /// ``` |
1347 | #[stable (feature = "total_cmp" , since = "1.62.0" )] |
1348 | #[must_use ] |
1349 | #[inline ] |
1350 | pub fn total_cmp(&self, other: &Self) -> crate::cmp::Ordering { |
1351 | let mut left = self.to_bits() as i64; |
1352 | let mut right = other.to_bits() as i64; |
1353 | |
1354 | // In case of negatives, flip all the bits except the sign |
1355 | // to achieve a similar layout as two's complement integers |
1356 | // |
1357 | // Why does this work? IEEE 754 floats consist of three fields: |
1358 | // Sign bit, exponent and mantissa. The set of exponent and mantissa |
1359 | // fields as a whole have the property that their bitwise order is |
1360 | // equal to the numeric magnitude where the magnitude is defined. |
1361 | // The magnitude is not normally defined on NaN values, but |
1362 | // IEEE 754 totalOrder defines the NaN values also to follow the |
1363 | // bitwise order. This leads to order explained in the doc comment. |
1364 | // However, the representation of magnitude is the same for negative |
1365 | // and positive numbers – only the sign bit is different. |
1366 | // To easily compare the floats as signed integers, we need to |
1367 | // flip the exponent and mantissa bits in case of negative numbers. |
1368 | // We effectively convert the numbers to "two's complement" form. |
1369 | // |
1370 | // To do the flipping, we construct a mask and XOR against it. |
1371 | // We branchlessly calculate an "all-ones except for the sign bit" |
1372 | // mask from negative-signed values: right shifting sign-extends |
1373 | // the integer, so we "fill" the mask with sign bits, and then |
1374 | // convert to unsigned to push one more zero bit. |
1375 | // On positive values, the mask is all zeros, so it's a no-op. |
1376 | left ^= (((left >> 63) as u64) >> 1) as i64; |
1377 | right ^= (((right >> 63) as u64) >> 1) as i64; |
1378 | |
1379 | left.cmp(&right) |
1380 | } |
1381 | |
1382 | /// Restrict a value to a certain interval unless it is NaN. |
1383 | /// |
1384 | /// Returns `max` if `self` is greater than `max`, and `min` if `self` is |
1385 | /// less than `min`. Otherwise this returns `self`. |
1386 | /// |
1387 | /// Note that this function returns NaN if the initial value was NaN as |
1388 | /// well. |
1389 | /// |
1390 | /// # Panics |
1391 | /// |
1392 | /// Panics if `min > max`, `min` is NaN, or `max` is NaN. |
1393 | /// |
1394 | /// # Examples |
1395 | /// |
1396 | /// ``` |
1397 | /// assert!((-3.0f64).clamp(-2.0, 1.0) == -2.0); |
1398 | /// assert!((0.0f64).clamp(-2.0, 1.0) == 0.0); |
1399 | /// assert!((2.0f64).clamp(-2.0, 1.0) == 1.0); |
1400 | /// assert!((f64::NAN).clamp(-2.0, 1.0).is_nan()); |
1401 | /// ``` |
1402 | #[must_use = "method returns a new number and does not mutate the original value" ] |
1403 | #[stable (feature = "clamp" , since = "1.50.0" )] |
1404 | #[rustc_const_stable (feature = "const_float_methods" , since = "1.85.0" )] |
1405 | #[inline ] |
1406 | pub const fn clamp(mut self, min: f64, max: f64) -> f64 { |
1407 | const_assert!( |
1408 | min <= max, |
1409 | "min > max, or either was NaN" , |
1410 | "min > max, or either was NaN. min = {min:?}, max = {max:?}" , |
1411 | min: f64, |
1412 | max: f64, |
1413 | ); |
1414 | |
1415 | if self < min { |
1416 | self = min; |
1417 | } |
1418 | if self > max { |
1419 | self = max; |
1420 | } |
1421 | self |
1422 | } |
1423 | |
1424 | /// Computes the absolute value of `self`. |
1425 | /// |
1426 | /// This function always returns the precise result. |
1427 | /// |
1428 | /// # Examples |
1429 | /// |
1430 | /// ``` |
1431 | /// let x = 3.5_f64; |
1432 | /// let y = -3.5_f64; |
1433 | /// |
1434 | /// assert_eq!(x.abs(), x); |
1435 | /// assert_eq!(y.abs(), -y); |
1436 | /// |
1437 | /// assert!(f64::NAN.abs().is_nan()); |
1438 | /// ``` |
1439 | #[must_use = "method returns a new number and does not mutate the original value" ] |
1440 | #[stable (feature = "rust1" , since = "1.0.0" )] |
1441 | #[rustc_const_stable (feature = "const_float_methods" , since = "1.85.0" )] |
1442 | #[inline ] |
1443 | pub const fn abs(self) -> f64 { |
1444 | // SAFETY: this is actually a safe intrinsic |
1445 | unsafe { intrinsics::fabsf64(self) } |
1446 | } |
1447 | |
1448 | /// Returns a number that represents the sign of `self`. |
1449 | /// |
1450 | /// - `1.0` if the number is positive, `+0.0` or `INFINITY` |
1451 | /// - `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY` |
1452 | /// - NaN if the number is NaN |
1453 | /// |
1454 | /// # Examples |
1455 | /// |
1456 | /// ``` |
1457 | /// let f = 3.5_f64; |
1458 | /// |
1459 | /// assert_eq!(f.signum(), 1.0); |
1460 | /// assert_eq!(f64::NEG_INFINITY.signum(), -1.0); |
1461 | /// |
1462 | /// assert!(f64::NAN.signum().is_nan()); |
1463 | /// ``` |
1464 | #[must_use = "method returns a new number and does not mutate the original value" ] |
1465 | #[stable (feature = "rust1" , since = "1.0.0" )] |
1466 | #[rustc_const_stable (feature = "const_float_methods" , since = "1.85.0" )] |
1467 | #[inline ] |
1468 | pub const fn signum(self) -> f64 { |
1469 | if self.is_nan() { Self::NAN } else { 1.0_f64.copysign(self) } |
1470 | } |
1471 | |
1472 | /// Returns a number composed of the magnitude of `self` and the sign of |
1473 | /// `sign`. |
1474 | /// |
1475 | /// Equal to `self` if the sign of `self` and `sign` are the same, otherwise equal to `-self`. |
1476 | /// If `self` is a NaN, then a NaN with the same payload as `self` and the sign bit of `sign` is |
1477 | /// returned. |
1478 | /// |
1479 | /// If `sign` is a NaN, then this operation will still carry over its sign into the result. Note |
1480 | /// that IEEE 754 doesn't assign any meaning to the sign bit in case of a NaN, and as Rust |
1481 | /// doesn't guarantee that the bit pattern of NaNs are conserved over arithmetic operations, the |
1482 | /// result of `copysign` with `sign` being a NaN might produce an unexpected or non-portable |
1483 | /// result. See the [specification of NaN bit patterns](primitive@f32#nan-bit-patterns) for more |
1484 | /// info. |
1485 | /// |
1486 | /// # Examples |
1487 | /// |
1488 | /// ``` |
1489 | /// let f = 3.5_f64; |
1490 | /// |
1491 | /// assert_eq!(f.copysign(0.42), 3.5_f64); |
1492 | /// assert_eq!(f.copysign(-0.42), -3.5_f64); |
1493 | /// assert_eq!((-f).copysign(0.42), 3.5_f64); |
1494 | /// assert_eq!((-f).copysign(-0.42), -3.5_f64); |
1495 | /// |
1496 | /// assert!(f64::NAN.copysign(1.0).is_nan()); |
1497 | /// ``` |
1498 | #[must_use = "method returns a new number and does not mutate the original value" ] |
1499 | #[stable (feature = "copysign" , since = "1.35.0" )] |
1500 | #[rustc_const_stable (feature = "const_float_methods" , since = "1.85.0" )] |
1501 | #[inline ] |
1502 | pub const fn copysign(self, sign: f64) -> f64 { |
1503 | // SAFETY: this is actually a safe intrinsic |
1504 | unsafe { intrinsics::copysignf64(self, sign) } |
1505 | } |
1506 | } |
1507 | |