]> git.lizzy.rs Git - rust.git/blob - src/libstd/num/f64.rs
60527905779f035296e8a126b7dfbe9b037677a0
[rust.git] / src / libstd / num / f64.rs
1 // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! Operations and constants for `f64`
12
13 #[allow(missing_doc)];
14 #[allow(non_uppercase_statics)];
15
16 use libc::c_int;
17 use num::{Zero, One, strconv};
18 use num::{FPCategory, FPNaN, FPInfinite , FPZero, FPSubnormal, FPNormal};
19 use num;
20 use prelude::*;
21 use to_str;
22
23 pub use cmath::c_double_targ_consts::*;
24 pub use cmp::{min, max};
25
26 use self::delegated::*;
27
28 macro_rules! delegate(
29     (
30         $(
31             fn $name:ident(
32                 $(
33                     $arg:ident : $arg_ty:ty
34                 ),*
35             ) -> $rv:ty = $bound_name:path
36         ),*
37     ) => (
38         // An inner module is required to get the #[inline] attribute on the
39         // functions.
40         mod delegated {
41             use cmath::c_double_utils;
42             use libc::{c_double, c_int};
43             use unstable::intrinsics;
44
45             $(
46                 #[inline]
47                 pub fn $name($( $arg : $arg_ty ),*) -> $rv {
48                     unsafe {
49                         $bound_name($( $arg ),*)
50                     }
51                 }
52             )*
53         }
54     )
55 )
56
57 delegate!(
58     // intrinsics
59     fn abs(n: f64) -> f64 = intrinsics::fabsf64,
60     fn cos(n: f64) -> f64 = intrinsics::cosf64,
61     fn exp(n: f64) -> f64 = intrinsics::expf64,
62     fn exp2(n: f64) -> f64 = intrinsics::exp2f64,
63     fn floor(x: f64) -> f64 = intrinsics::floorf64,
64     fn ln(n: f64) -> f64 = intrinsics::logf64,
65     fn log10(n: f64) -> f64 = intrinsics::log10f64,
66     fn log2(n: f64) -> f64 = intrinsics::log2f64,
67     fn mul_add(a: f64, b: f64, c: f64) -> f64 = intrinsics::fmaf64,
68     fn pow(n: f64, e: f64) -> f64 = intrinsics::powf64,
69     fn powi(n: f64, e: c_int) -> f64 = intrinsics::powif64,
70     fn sin(n: f64) -> f64 = intrinsics::sinf64,
71     fn sqrt(n: f64) -> f64 = intrinsics::sqrtf64,
72
73     // LLVM 3.3 required to use intrinsics for these four
74     fn ceil(n: c_double) -> c_double = c_double_utils::ceil,
75     fn trunc(n: c_double) -> c_double = c_double_utils::trunc,
76     /*
77     fn ceil(n: f64) -> f64 = intrinsics::ceilf64,
78     fn trunc(n: f64) -> f64 = intrinsics::truncf64,
79     fn rint(n: c_double) -> c_double = intrinsics::rintf64,
80     fn nearbyint(n: c_double) -> c_double = intrinsics::nearbyintf64,
81     */
82
83     // cmath
84     fn acos(n: c_double) -> c_double = c_double_utils::acos,
85     fn asin(n: c_double) -> c_double = c_double_utils::asin,
86     fn atan(n: c_double) -> c_double = c_double_utils::atan,
87     fn atan2(a: c_double, b: c_double) -> c_double = c_double_utils::atan2,
88     fn cbrt(n: c_double) -> c_double = c_double_utils::cbrt,
89     fn copysign(x: c_double, y: c_double) -> c_double = c_double_utils::copysign,
90     fn cosh(n: c_double) -> c_double = c_double_utils::cosh,
91     fn erf(n: c_double) -> c_double = c_double_utils::erf,
92     fn erfc(n: c_double) -> c_double = c_double_utils::erfc,
93     fn exp_m1(n: c_double) -> c_double = c_double_utils::exp_m1,
94     fn abs_sub(a: c_double, b: c_double) -> c_double = c_double_utils::abs_sub,
95     fn next_after(x: c_double, y: c_double) -> c_double = c_double_utils::next_after,
96     fn frexp(n: c_double, value: &mut c_int) -> c_double = c_double_utils::frexp,
97     fn hypot(x: c_double, y: c_double) -> c_double = c_double_utils::hypot,
98     fn ldexp(x: c_double, n: c_int) -> c_double = c_double_utils::ldexp,
99     fn lgamma(n: c_double, sign: &mut c_int) -> c_double = c_double_utils::lgamma,
100     fn log_radix(n: c_double) -> c_double = c_double_utils::log_radix,
101     fn ln_1p(n: c_double) -> c_double = c_double_utils::ln_1p,
102     fn ilog_radix(n: c_double) -> c_int = c_double_utils::ilog_radix,
103     fn modf(n: c_double, iptr: &mut c_double) -> c_double = c_double_utils::modf,
104     fn round(n: c_double) -> c_double = c_double_utils::round,
105     fn ldexp_radix(n: c_double, i: c_int) -> c_double = c_double_utils::ldexp_radix,
106     fn sinh(n: c_double) -> c_double = c_double_utils::sinh,
107     fn tan(n: c_double) -> c_double = c_double_utils::tan,
108     fn tanh(n: c_double) -> c_double = c_double_utils::tanh,
109     fn tgamma(n: c_double) -> c_double = c_double_utils::tgamma,
110     fn j0(n: c_double) -> c_double = c_double_utils::j0,
111     fn j1(n: c_double) -> c_double = c_double_utils::j1,
112     fn jn(i: c_int, n: c_double) -> c_double = c_double_utils::jn,
113     fn y0(n: c_double) -> c_double = c_double_utils::y0,
114     fn y1(n: c_double) -> c_double = c_double_utils::y1,
115     fn yn(i: c_int, n: c_double) -> c_double = c_double_utils::yn
116 )
117
118 // FIXME (#1433): obtain these in a different way
119
120 // These are not defined inside consts:: for consistency with
121 // the integer types
122
123 pub static radix: uint = 2u;
124
125 pub static mantissa_digits: uint = 53u;
126 pub static digits: uint = 15u;
127
128 pub static epsilon: f64 = 2.2204460492503131e-16_f64;
129
130 pub static min_value: f64 = 2.2250738585072014e-308_f64;
131 pub static max_value: f64 = 1.7976931348623157e+308_f64;
132
133 pub static min_exp: int = -1021;
134 pub static max_exp: int = 1024;
135
136 pub static min_10_exp: int = -307;
137 pub static max_10_exp: int = 308;
138
139 pub static NaN: f64 = 0.0_f64/0.0_f64;
140
141 pub static infinity: f64 = 1.0_f64/0.0_f64;
142
143 pub static neg_infinity: f64 = -1.0_f64/0.0_f64;
144
145 // FIXME (#1999): add is_normal, is_subnormal, and fpclassify
146
147 /* Module: consts */
148 pub mod consts {
149     // FIXME (requires Issue #1433 to fix): replace with mathematical
150     // constants from cmath.
151     /// Archimedes' constant
152     pub static pi: f64 = 3.14159265358979323846264338327950288_f64;
153
154     /// pi/2.0
155     pub static frac_pi_2: f64 = 1.57079632679489661923132169163975144_f64;
156
157     /// pi/4.0
158     pub static frac_pi_4: f64 = 0.785398163397448309615660845819875721_f64;
159
160     /// 1.0/pi
161     pub static frac_1_pi: f64 = 0.318309886183790671537767526745028724_f64;
162
163     /// 2.0/pi
164     pub static frac_2_pi: f64 = 0.636619772367581343075535053490057448_f64;
165
166     /// 2.0/sqrt(pi)
167     pub static frac_2_sqrtpi: f64 = 1.12837916709551257389615890312154517_f64;
168
169     /// sqrt(2.0)
170     pub static sqrt2: f64 = 1.41421356237309504880168872420969808_f64;
171
172     /// 1.0/sqrt(2.0)
173     pub static frac_1_sqrt2: f64 = 0.707106781186547524400844362104849039_f64;
174
175     /// Euler's number
176     pub static e: f64 = 2.71828182845904523536028747135266250_f64;
177
178     /// log2(e)
179     pub static log2_e: f64 = 1.44269504088896340735992468100189214_f64;
180
181     /// log10(e)
182     pub static log10_e: f64 = 0.434294481903251827651128918916605082_f64;
183
184     /// ln(2.0)
185     pub static ln_2: f64 = 0.693147180559945309417232121458176568_f64;
186
187     /// ln(10.0)
188     pub static ln_10: f64 = 2.30258509299404568401799145468436421_f64;
189 }
190
191 impl Num for f64 {}
192
193 #[cfg(not(test))]
194 impl Eq for f64 {
195     #[inline]
196     fn eq(&self, other: &f64) -> bool { (*self) == (*other) }
197     #[inline]
198     fn ne(&self, other: &f64) -> bool { (*self) != (*other) }
199 }
200
201 #[cfg(not(test))]
202 impl ApproxEq<f64> for f64 {
203     #[inline]
204     fn approx_epsilon() -> f64 { 1.0e-6 }
205
206     #[inline]
207     fn approx_eq(&self, other: &f64) -> bool {
208         self.approx_eq_eps(other, &ApproxEq::approx_epsilon::<f64, f64>())
209     }
210
211     #[inline]
212     fn approx_eq_eps(&self, other: &f64, approx_epsilon: &f64) -> bool {
213         (*self - *other).abs() < *approx_epsilon
214     }
215 }
216
217 #[cfg(not(test))]
218 impl Ord for f64 {
219     #[inline]
220     fn lt(&self, other: &f64) -> bool { (*self) < (*other) }
221     #[inline]
222     fn le(&self, other: &f64) -> bool { (*self) <= (*other) }
223     #[inline]
224     fn ge(&self, other: &f64) -> bool { (*self) >= (*other) }
225     #[inline]
226     fn gt(&self, other: &f64) -> bool { (*self) > (*other) }
227 }
228
229 impl Orderable for f64 {
230     /// Returns `NaN` if either of the numbers are `NaN`.
231     #[inline]
232     fn min(&self, other: &f64) -> f64 {
233         cond!(
234             (self.is_NaN())  { *self  }
235             (other.is_NaN()) { *other }
236             (*self < *other) { *self  }
237             _                { *other }
238         )
239     }
240
241     /// Returns `NaN` if either of the numbers are `NaN`.
242     #[inline]
243     fn max(&self, other: &f64) -> f64 {
244         cond!(
245             (self.is_NaN())  { *self  }
246             (other.is_NaN()) { *other }
247             (*self > *other) { *self  }
248             _                { *other }
249         )
250     }
251
252     /// Returns the number constrained within the range `mn <= self <= mx`.
253     /// If any of the numbers are `NaN` then `NaN` is returned.
254     #[inline]
255     fn clamp(&self, mn: &f64, mx: &f64) -> f64 {
256         cond!(
257             (self.is_NaN())   { *self }
258             (!(*self <= *mx)) { *mx   }
259             (!(*self >= *mn)) { *mn   }
260             _                 { *self }
261         )
262     }
263 }
264
265 impl Zero for f64 {
266     #[inline]
267     fn zero() -> f64 { 0.0 }
268
269     /// Returns true if the number is equal to either `0.0` or `-0.0`
270     #[inline]
271     fn is_zero(&self) -> bool { *self == 0.0 || *self == -0.0 }
272 }
273
274 impl One for f64 {
275     #[inline]
276     fn one() -> f64 { 1.0 }
277 }
278
279 #[cfg(not(test))]
280 impl Add<f64,f64> for f64 {
281     #[inline]
282     fn add(&self, other: &f64) -> f64 { *self + *other }
283 }
284 #[cfg(not(test))]
285 impl Sub<f64,f64> for f64 {
286     #[inline]
287     fn sub(&self, other: &f64) -> f64 { *self - *other }
288 }
289 #[cfg(not(test))]
290 impl Mul<f64,f64> for f64 {
291     #[inline]
292     fn mul(&self, other: &f64) -> f64 { *self * *other }
293 }
294 #[cfg(not(test))]
295 impl Div<f64,f64> for f64 {
296     #[inline]
297     fn div(&self, other: &f64) -> f64 { *self / *other }
298 }
299 #[cfg(not(test))]
300 impl Rem<f64,f64> for f64 {
301     #[inline]
302     fn rem(&self, other: &f64) -> f64 { *self % *other }
303 }
304 #[cfg(not(test))]
305 impl Neg<f64> for f64 {
306     fn neg(&self) -> f64 { -*self }
307 }
308
309 impl Signed for f64 {
310     /// Computes the absolute value. Returns `NaN` if the number is `NaN`.
311     #[inline]
312     fn abs(&self) -> f64 { abs(*self) }
313
314     ///
315     /// The positive difference of two numbers. Returns `0.0` if the number is less than or
316     /// equal to `other`, otherwise the difference between`self` and `other` is returned.
317     ///
318     #[inline]
319     fn abs_sub(&self, other: &f64) -> f64 { abs_sub(*self, *other) }
320
321     ///
322     /// # Returns
323     ///
324     /// - `1.0` if the number is positive, `+0.0` or `infinity`
325     /// - `-1.0` if the number is negative, `-0.0` or `neg_infinity`
326     /// - `NaN` if the number is NaN
327     ///
328     #[inline]
329     fn signum(&self) -> f64 {
330         if self.is_NaN() { NaN } else { copysign(1.0, *self) }
331     }
332
333     /// Returns `true` if the number is positive, including `+0.0` and `infinity`
334     #[inline]
335     fn is_positive(&self) -> bool { *self > 0.0 || (1.0 / *self) == infinity }
336
337     /// Returns `true` if the number is negative, including `-0.0` and `neg_infinity`
338     #[inline]
339     fn is_negative(&self) -> bool { *self < 0.0 || (1.0 / *self) == neg_infinity }
340 }
341
342 impl Round for f64 {
343     /// Round half-way cases toward `neg_infinity`
344     #[inline]
345     fn floor(&self) -> f64 { floor(*self) }
346
347     /// Round half-way cases toward `infinity`
348     #[inline]
349     fn ceil(&self) -> f64 { ceil(*self) }
350
351     /// Round half-way cases away from `0.0`
352     #[inline]
353     fn round(&self) -> f64 { round(*self) }
354
355     /// The integer part of the number (rounds towards `0.0`)
356     #[inline]
357     fn trunc(&self) -> f64 { trunc(*self) }
358
359     ///
360     /// The fractional part of the number, satisfying:
361     ///
362     /// ~~~ {.rust}
363     /// assert!(x == trunc(x) + fract(x))
364     /// ~~~
365     ///
366     #[inline]
367     fn fract(&self) -> f64 { *self - self.trunc() }
368 }
369
370 impl Fractional for f64 {
371     /// The reciprocal (multiplicative inverse) of the number
372     #[inline]
373     fn recip(&self) -> f64 { 1.0 / *self }
374 }
375
376 impl Algebraic for f64 {
377     #[inline]
378     fn pow(&self, n: &f64) -> f64 { pow(*self, *n) }
379
380     #[inline]
381     fn sqrt(&self) -> f64 { sqrt(*self) }
382
383     #[inline]
384     fn rsqrt(&self) -> f64 { self.sqrt().recip() }
385
386     #[inline]
387     fn cbrt(&self) -> f64 { cbrt(*self) }
388
389     #[inline]
390     fn hypot(&self, other: &f64) -> f64 { hypot(*self, *other) }
391 }
392
393 impl Trigonometric for f64 {
394     #[inline]
395     fn sin(&self) -> f64 { sin(*self) }
396
397     #[inline]
398     fn cos(&self) -> f64 { cos(*self) }
399
400     #[inline]
401     fn tan(&self) -> f64 { tan(*self) }
402
403     #[inline]
404     fn asin(&self) -> f64 { asin(*self) }
405
406     #[inline]
407     fn acos(&self) -> f64 { acos(*self) }
408
409     #[inline]
410     fn atan(&self) -> f64 { atan(*self) }
411
412     #[inline]
413     fn atan2(&self, other: &f64) -> f64 { atan2(*self, *other) }
414
415     /// Simultaneously computes the sine and cosine of the number
416     #[inline]
417     fn sin_cos(&self) -> (f64, f64) {
418         (self.sin(), self.cos())
419     }
420 }
421
422 impl Exponential for f64 {
423     /// Returns the exponential of the number
424     #[inline]
425     fn exp(&self) -> f64 { exp(*self) }
426
427     /// Returns 2 raised to the power of the number
428     #[inline]
429     fn exp2(&self) -> f64 { exp2(*self) }
430
431     /// Returns the natural logarithm of the number
432     #[inline]
433     fn ln(&self) -> f64 { ln(*self) }
434
435     /// Returns the logarithm of the number with respect to an arbitrary base
436     #[inline]
437     fn log(&self, base: &f64) -> f64 { self.ln() / base.ln() }
438
439     /// Returns the base 2 logarithm of the number
440     #[inline]
441     fn log2(&self) -> f64 { log2(*self) }
442
443     /// Returns the base 10 logarithm of the number
444     #[inline]
445     fn log10(&self) -> f64 { log10(*self) }
446 }
447
448 impl Hyperbolic for f64 {
449     #[inline]
450     fn sinh(&self) -> f64 { sinh(*self) }
451
452     #[inline]
453     fn cosh(&self) -> f64 { cosh(*self) }
454
455     #[inline]
456     fn tanh(&self) -> f64 { tanh(*self) }
457
458     ///
459     /// Inverse hyperbolic sine
460     ///
461     /// # Returns
462     ///
463     /// - on success, the inverse hyperbolic sine of `self` will be returned
464     /// - `self` if `self` is `0.0`, `-0.0`, `infinity`, or `neg_infinity`
465     /// - `NaN` if `self` is `NaN`
466     ///
467     #[inline]
468     fn asinh(&self) -> f64 {
469         match *self {
470             neg_infinity => neg_infinity,
471             x => (x + ((x * x) + 1.0).sqrt()).ln(),
472         }
473     }
474
475     ///
476     /// Inverse hyperbolic cosine
477     ///
478     /// # Returns
479     ///
480     /// - on success, the inverse hyperbolic cosine of `self` will be returned
481     /// - `infinity` if `self` is `infinity`
482     /// - `NaN` if `self` is `NaN` or `self < 1.0` (including `neg_infinity`)
483     ///
484     #[inline]
485     fn acosh(&self) -> f64 {
486         match *self {
487             x if x < 1.0 => Float::NaN(),
488             x => (x + ((x * x) - 1.0).sqrt()).ln(),
489         }
490     }
491
492     ///
493     /// Inverse hyperbolic tangent
494     ///
495     /// # Returns
496     ///
497     /// - on success, the inverse hyperbolic tangent of `self` will be returned
498     /// - `self` if `self` is `0.0` or `-0.0`
499     /// - `infinity` if `self` is `1.0`
500     /// - `neg_infinity` if `self` is `-1.0`
501     /// - `NaN` if the `self` is `NaN` or outside the domain of `-1.0 <= self <= 1.0`
502     ///   (including `infinity` and `neg_infinity`)
503     ///
504     #[inline]
505     fn atanh(&self) -> f64 {
506         0.5 * ((2.0 * *self) / (1.0 - *self)).ln_1p()
507     }
508 }
509
510 impl Real for f64 {
511     /// Archimedes' constant
512     #[inline]
513     fn pi() -> f64 { 3.14159265358979323846264338327950288 }
514
515     /// 2.0 * pi
516     #[inline]
517     fn two_pi() -> f64 { 6.28318530717958647692528676655900576 }
518
519     /// pi / 2.0
520     #[inline]
521     fn frac_pi_2() -> f64 { 1.57079632679489661923132169163975144 }
522
523     /// pi / 3.0
524     #[inline]
525     fn frac_pi_3() -> f64 { 1.04719755119659774615421446109316763 }
526
527     /// pi / 4.0
528     #[inline]
529     fn frac_pi_4() -> f64 { 0.785398163397448309615660845819875721 }
530
531     /// pi / 6.0
532     #[inline]
533     fn frac_pi_6() -> f64 { 0.52359877559829887307710723054658381 }
534
535     /// pi / 8.0
536     #[inline]
537     fn frac_pi_8() -> f64 { 0.39269908169872415480783042290993786 }
538
539     /// 1.0 / pi
540     #[inline]
541     fn frac_1_pi() -> f64 { 0.318309886183790671537767526745028724 }
542
543     /// 2.0 / pi
544     #[inline]
545     fn frac_2_pi() -> f64 { 0.636619772367581343075535053490057448 }
546
547     /// 2.0 / sqrt(pi)
548     #[inline]
549     fn frac_2_sqrtpi() -> f64 { 1.12837916709551257389615890312154517 }
550
551     /// sqrt(2.0)
552     #[inline]
553     fn sqrt2() -> f64 { 1.41421356237309504880168872420969808 }
554
555     /// 1.0 / sqrt(2.0)
556     #[inline]
557     fn frac_1_sqrt2() -> f64 { 0.707106781186547524400844362104849039 }
558
559     /// Euler's number
560     #[inline]
561     fn e() -> f64 { 2.71828182845904523536028747135266250 }
562
563     /// log2(e)
564     #[inline]
565     fn log2_e() -> f64 { 1.44269504088896340735992468100189214 }
566
567     /// log10(e)
568     #[inline]
569     fn log10_e() -> f64 { 0.434294481903251827651128918916605082 }
570
571     /// ln(2.0)
572     #[inline]
573     fn ln_2() -> f64 { 0.693147180559945309417232121458176568 }
574
575     /// ln(10.0)
576     #[inline]
577     fn ln_10() -> f64 { 2.30258509299404568401799145468436421 }
578
579     /// Converts to degrees, assuming the number is in radians
580     #[inline]
581     fn to_degrees(&self) -> f64 { *self * (180.0 / Real::pi::<f64>()) }
582
583     /// Converts to radians, assuming the number is in degrees
584     #[inline]
585     fn to_radians(&self) -> f64 { *self * (Real::pi::<f64>() / 180.0) }
586 }
587
588 impl RealExt for f64 {
589     #[inline]
590     fn lgamma(&self) -> (int, f64) {
591         let mut sign = 0;
592         let result = lgamma(*self, &mut sign);
593         (sign as int, result)
594     }
595
596     #[inline]
597     fn tgamma(&self) -> f64 { tgamma(*self) }
598
599     #[inline]
600     fn j0(&self) -> f64 { j0(*self) }
601
602     #[inline]
603     fn j1(&self) -> f64 { j1(*self) }
604
605     #[inline]
606     fn jn(&self, n: int) -> f64 { jn(n as c_int, *self) }
607
608     #[inline]
609     fn y0(&self) -> f64 { y0(*self) }
610
611     #[inline]
612     fn y1(&self) -> f64 { y1(*self) }
613
614     #[inline]
615     fn yn(&self, n: int) -> f64 { yn(n as c_int, *self) }
616 }
617
618 impl Bounded for f64 {
619     #[inline]
620     fn min_value() -> f64 { 2.2250738585072014e-308 }
621
622     #[inline]
623     fn max_value() -> f64 { 1.7976931348623157e+308 }
624 }
625
626 impl Primitive for f64 {
627     #[inline]
628     fn bits() -> uint { 64 }
629
630     #[inline]
631     fn bytes() -> uint { Primitive::bits::<f64>() / 8 }
632 }
633
634 impl Float for f64 {
635     #[inline]
636     fn NaN() -> f64 { 0.0 / 0.0 }
637
638     #[inline]
639     fn infinity() -> f64 { 1.0 / 0.0 }
640
641     #[inline]
642     fn neg_infinity() -> f64 { -1.0 / 0.0 }
643
644     #[inline]
645     fn neg_zero() -> f64 { -0.0 }
646
647     /// Returns `true` if the number is NaN
648     #[inline]
649     fn is_NaN(&self) -> bool { *self != *self }
650
651     /// Returns `true` if the number is infinite
652     #[inline]
653     fn is_infinite(&self) -> bool {
654         *self == Float::infinity() || *self == Float::neg_infinity()
655     }
656
657     /// Returns `true` if the number is neither infinite or NaN
658     #[inline]
659     fn is_finite(&self) -> bool {
660         !(self.is_NaN() || self.is_infinite())
661     }
662
663     /// Returns `true` if the number is neither zero, infinite, subnormal or NaN
664     #[inline]
665     fn is_normal(&self) -> bool {
666         self.classify() == FPNormal
667     }
668
669     /// Returns the floating point category of the number. If only one property is going to
670     /// be tested, it is generally faster to use the specific predicate instead.
671     fn classify(&self) -> FPCategory {
672         static EXP_MASK: u64 = 0x7ff0000000000000;
673         static MAN_MASK: u64 = 0x000fffffffffffff;
674
675         match (
676             unsafe { ::cast::transmute::<f64,u64>(*self) } & MAN_MASK,
677             unsafe { ::cast::transmute::<f64,u64>(*self) } & EXP_MASK,
678         ) {
679             (0, 0)        => FPZero,
680             (_, 0)        => FPSubnormal,
681             (0, EXP_MASK) => FPInfinite,
682             (_, EXP_MASK) => FPNaN,
683             _             => FPNormal,
684         }
685     }
686
687     #[inline]
688     fn mantissa_digits() -> uint { 53 }
689
690     #[inline]
691     fn digits() -> uint { 15 }
692
693     #[inline]
694     fn epsilon() -> f64 { 2.2204460492503131e-16 }
695
696     #[inline]
697     fn min_exp() -> int { -1021 }
698
699     #[inline]
700     fn max_exp() -> int { 1024 }
701
702     #[inline]
703     fn min_10_exp() -> int { -307 }
704
705     #[inline]
706     fn max_10_exp() -> int { 308 }
707
708     /// Constructs a floating point number by multiplying `x` by 2 raised to the power of `exp`
709     #[inline]
710     fn ldexp(x: f64, exp: int) -> f64 {
711         ldexp(x, exp as c_int)
712     }
713
714     ///
715     /// Breaks the number into a normalized fraction and a base-2 exponent, satisfying:
716     ///
717     /// - `self = x * pow(2, exp)`
718     /// - `0.5 <= abs(x) < 1.0`
719     ///
720     #[inline]
721     fn frexp(&self) -> (f64, int) {
722         let mut exp = 0;
723         let x = frexp(*self, &mut exp);
724         (x, exp as int)
725     }
726
727     ///
728     /// Returns the exponential of the number, minus `1`, in a way that is accurate
729     /// even if the number is close to zero
730     ///
731     #[inline]
732     fn exp_m1(&self) -> f64 { exp_m1(*self) }
733
734     ///
735     /// Returns the natural logarithm of the number plus `1` (`ln(1+n)`) more accurately
736     /// than if the operations were performed separately
737     ///
738     #[inline]
739     fn ln_1p(&self) -> f64 { ln_1p(*self) }
740
741     ///
742     /// Fused multiply-add. Computes `(self * a) + b` with only one rounding error. This
743     /// produces a more accurate result with better performance than a separate multiplication
744     /// operation followed by an add.
745     ///
746     #[inline]
747     fn mul_add(&self, a: f64, b: f64) -> f64 {
748         mul_add(*self, a, b)
749     }
750
751     /// Returns the next representable floating-point value in the direction of `other`
752     #[inline]
753     fn next_after(&self, other: f64) -> f64 {
754         next_after(*self, other)
755     }
756 }
757
758 //
759 // Section: String Conversions
760 //
761
762 ///
763 /// Converts a float to a string
764 ///
765 /// # Arguments
766 ///
767 /// * num - The float value
768 ///
769 #[inline]
770 pub fn to_str(num: f64) -> ~str {
771     let (r, _) = strconv::float_to_str_common(
772         num, 10u, true, strconv::SignNeg, strconv::DigAll);
773     r
774 }
775
776 ///
777 /// Converts a float to a string in hexadecimal format
778 ///
779 /// # Arguments
780 ///
781 /// * num - The float value
782 ///
783 #[inline]
784 pub fn to_str_hex(num: f64) -> ~str {
785     let (r, _) = strconv::float_to_str_common(
786         num, 16u, true, strconv::SignNeg, strconv::DigAll);
787     r
788 }
789
790 ///
791 /// Converts a float to a string in a given radix
792 ///
793 /// # Arguments
794 ///
795 /// * num - The float value
796 /// * radix - The base to use
797 ///
798 /// # Failure
799 ///
800 /// Fails if called on a special value like `inf`, `-inf` or `NaN` due to
801 /// possible misinterpretation of the result at higher bases. If those values
802 /// are expected, use `to_str_radix_special()` instead.
803 ///
804 #[inline]
805 pub fn to_str_radix(num: f64, rdx: uint) -> ~str {
806     let (r, special) = strconv::float_to_str_common(
807         num, rdx, true, strconv::SignNeg, strconv::DigAll);
808     if special { fail!("number has a special value, \
809                       try to_str_radix_special() if those are expected") }
810     r
811 }
812
813 ///
814 /// Converts a float to a string in a given radix, and a flag indicating
815 /// whether it's a special value
816 ///
817 /// # Arguments
818 ///
819 /// * num - The float value
820 /// * radix - The base to use
821 ///
822 #[inline]
823 pub fn to_str_radix_special(num: f64, rdx: uint) -> (~str, bool) {
824     strconv::float_to_str_common(num, rdx, true,
825                            strconv::SignNeg, strconv::DigAll)
826 }
827
828 ///
829 /// Converts a float to a string with exactly the number of
830 /// provided significant digits
831 ///
832 /// # Arguments
833 ///
834 /// * num - The float value
835 /// * digits - The number of significant digits
836 ///
837 #[inline]
838 pub fn to_str_exact(num: f64, dig: uint) -> ~str {
839     let (r, _) = strconv::float_to_str_common(
840         num, 10u, true, strconv::SignNeg, strconv::DigExact(dig));
841     r
842 }
843
844 ///
845 /// Converts a float to a string with a maximum number of
846 /// significant digits
847 ///
848 /// # Arguments
849 ///
850 /// * num - The float value
851 /// * digits - The number of significant digits
852 ///
853 #[inline]
854 pub fn to_str_digits(num: f64, dig: uint) -> ~str {
855     let (r, _) = strconv::float_to_str_common(
856         num, 10u, true, strconv::SignNeg, strconv::DigMax(dig));
857     r
858 }
859
860 impl to_str::ToStr for f64 {
861     #[inline]
862     fn to_str(&self) -> ~str { to_str_digits(*self, 8) }
863 }
864
865 impl num::ToStrRadix for f64 {
866     #[inline]
867     fn to_str_radix(&self, rdx: uint) -> ~str {
868         to_str_radix(*self, rdx)
869     }
870 }
871
872 ///
873 /// Convert a string in base 10 to a float.
874 /// Accepts a optional decimal exponent.
875 ///
876 /// This function accepts strings such as
877 ///
878 /// * '3.14'
879 /// * '+3.14', equivalent to '3.14'
880 /// * '-3.14'
881 /// * '2.5E10', or equivalently, '2.5e10'
882 /// * '2.5E-10'
883 /// * '.' (understood as 0)
884 /// * '5.'
885 /// * '.5', or, equivalently,  '0.5'
886 /// * '+inf', 'inf', '-inf', 'NaN'
887 ///
888 /// Leading and trailing whitespace represent an error.
889 ///
890 /// # Arguments
891 ///
892 /// * num - A string
893 ///
894 /// # Return value
895 ///
896 /// `none` if the string did not represent a valid number.  Otherwise,
897 /// `Some(n)` where `n` is the floating-point number represented by `num`.
898 ///
899 #[inline]
900 pub fn from_str(num: &str) -> Option<f64> {
901     strconv::from_str_common(num, 10u, true, true, true,
902                              strconv::ExpDec, false, false)
903 }
904
905 ///
906 /// Convert a string in base 16 to a float.
907 /// Accepts a optional binary exponent.
908 ///
909 /// This function accepts strings such as
910 ///
911 /// * 'a4.fe'
912 /// * '+a4.fe', equivalent to 'a4.fe'
913 /// * '-a4.fe'
914 /// * '2b.aP128', or equivalently, '2b.ap128'
915 /// * '2b.aP-128'
916 /// * '.' (understood as 0)
917 /// * 'c.'
918 /// * '.c', or, equivalently,  '0.c'
919 /// * '+inf', 'inf', '-inf', 'NaN'
920 ///
921 /// Leading and trailing whitespace represent an error.
922 ///
923 /// # Arguments
924 ///
925 /// * num - A string
926 ///
927 /// # Return value
928 ///
929 /// `none` if the string did not represent a valid number.  Otherwise,
930 /// `Some(n)` where `n` is the floating-point number represented by `[num]`.
931 ///
932 #[inline]
933 pub fn from_str_hex(num: &str) -> Option<f64> {
934     strconv::from_str_common(num, 16u, true, true, true,
935                              strconv::ExpBin, false, false)
936 }
937
938 ///
939 /// Convert a string in an given base to a float.
940 ///
941 /// Due to possible conflicts, this function does **not** accept
942 /// the special values `inf`, `-inf`, `+inf` and `NaN`, **nor**
943 /// does it recognize exponents of any kind.
944 ///
945 /// Leading and trailing whitespace represent an error.
946 ///
947 /// # Arguments
948 ///
949 /// * num - A string
950 /// * radix - The base to use. Must lie in the range [2 .. 36]
951 ///
952 /// # Return value
953 ///
954 /// `none` if the string did not represent a valid number. Otherwise,
955 /// `Some(n)` where `n` is the floating-point number represented by `num`.
956 ///
957 #[inline]
958 pub fn from_str_radix(num: &str, rdx: uint) -> Option<f64> {
959     strconv::from_str_common(num, rdx, true, true, false,
960                              strconv::ExpNone, false, false)
961 }
962
963 impl FromStr for f64 {
964     #[inline]
965     fn from_str(val: &str) -> Option<f64> { from_str(val) }
966 }
967
968 impl num::FromStrRadix for f64 {
969     #[inline]
970     fn from_str_radix(val: &str, rdx: uint) -> Option<f64> {
971         from_str_radix(val, rdx)
972     }
973 }
974
975 #[cfg(test)]
976 mod tests {
977     use f64::*;
978     use prelude::*;
979
980     use num::*;
981     use num;
982     use sys;
983
984     #[test]
985     fn test_num() {
986         num::test_num(10f64, 2f64);
987     }
988
989     #[test]
990     fn test_min() {
991         assert_eq!(1f64.min(&2f64), 1f64);
992         assert_eq!(2f64.min(&1f64), 1f64);
993         assert!(1f64.min(&Float::NaN::<f64>()).is_NaN());
994         assert!(Float::NaN::<f64>().min(&1f64).is_NaN());
995     }
996
997     #[test]
998     fn test_max() {
999         assert_eq!(1f64.max(&2f64), 2f64);
1000         assert_eq!(2f64.max(&1f64), 2f64);
1001         assert!(1f64.max(&Float::NaN::<f64>()).is_NaN());
1002         assert!(Float::NaN::<f64>().max(&1f64).is_NaN());
1003     }
1004
1005     #[test]
1006     fn test_clamp() {
1007         assert_eq!(1f64.clamp(&2f64, &4f64), 2f64);
1008         assert_eq!(8f64.clamp(&2f64, &4f64), 4f64);
1009         assert_eq!(3f64.clamp(&2f64, &4f64), 3f64);
1010         assert!(3f64.clamp(&Float::NaN::<f64>(), &4f64).is_NaN());
1011         assert!(3f64.clamp(&2f64, &Float::NaN::<f64>()).is_NaN());
1012         assert!(Float::NaN::<f64>().clamp(&2f64, &4f64).is_NaN());
1013     }
1014
1015     #[test]
1016     fn test_floor() {
1017         assert_approx_eq!(1.0f64.floor(), 1.0f64);
1018         assert_approx_eq!(1.3f64.floor(), 1.0f64);
1019         assert_approx_eq!(1.5f64.floor(), 1.0f64);
1020         assert_approx_eq!(1.7f64.floor(), 1.0f64);
1021         assert_approx_eq!(0.0f64.floor(), 0.0f64);
1022         assert_approx_eq!((-0.0f64).floor(), -0.0f64);
1023         assert_approx_eq!((-1.0f64).floor(), -1.0f64);
1024         assert_approx_eq!((-1.3f64).floor(), -2.0f64);
1025         assert_approx_eq!((-1.5f64).floor(), -2.0f64);
1026         assert_approx_eq!((-1.7f64).floor(), -2.0f64);
1027     }
1028
1029     #[test]
1030     fn test_ceil() {
1031         assert_approx_eq!(1.0f64.ceil(), 1.0f64);
1032         assert_approx_eq!(1.3f64.ceil(), 2.0f64);
1033         assert_approx_eq!(1.5f64.ceil(), 2.0f64);
1034         assert_approx_eq!(1.7f64.ceil(), 2.0f64);
1035         assert_approx_eq!(0.0f64.ceil(), 0.0f64);
1036         assert_approx_eq!((-0.0f64).ceil(), -0.0f64);
1037         assert_approx_eq!((-1.0f64).ceil(), -1.0f64);
1038         assert_approx_eq!((-1.3f64).ceil(), -1.0f64);
1039         assert_approx_eq!((-1.5f64).ceil(), -1.0f64);
1040         assert_approx_eq!((-1.7f64).ceil(), -1.0f64);
1041     }
1042
1043     #[test]
1044     fn test_round() {
1045         assert_approx_eq!(1.0f64.round(), 1.0f64);
1046         assert_approx_eq!(1.3f64.round(), 1.0f64);
1047         assert_approx_eq!(1.5f64.round(), 2.0f64);
1048         assert_approx_eq!(1.7f64.round(), 2.0f64);
1049         assert_approx_eq!(0.0f64.round(), 0.0f64);
1050         assert_approx_eq!((-0.0f64).round(), -0.0f64);
1051         assert_approx_eq!((-1.0f64).round(), -1.0f64);
1052         assert_approx_eq!((-1.3f64).round(), -1.0f64);
1053         assert_approx_eq!((-1.5f64).round(), -2.0f64);
1054         assert_approx_eq!((-1.7f64).round(), -2.0f64);
1055     }
1056
1057     #[test]
1058     fn test_trunc() {
1059         assert_approx_eq!(1.0f64.trunc(), 1.0f64);
1060         assert_approx_eq!(1.3f64.trunc(), 1.0f64);
1061         assert_approx_eq!(1.5f64.trunc(), 1.0f64);
1062         assert_approx_eq!(1.7f64.trunc(), 1.0f64);
1063         assert_approx_eq!(0.0f64.trunc(), 0.0f64);
1064         assert_approx_eq!((-0.0f64).trunc(), -0.0f64);
1065         assert_approx_eq!((-1.0f64).trunc(), -1.0f64);
1066         assert_approx_eq!((-1.3f64).trunc(), -1.0f64);
1067         assert_approx_eq!((-1.5f64).trunc(), -1.0f64);
1068         assert_approx_eq!((-1.7f64).trunc(), -1.0f64);
1069     }
1070
1071     #[test]
1072     fn test_fract() {
1073         assert_approx_eq!(1.0f64.fract(), 0.0f64);
1074         assert_approx_eq!(1.3f64.fract(), 0.3f64);
1075         assert_approx_eq!(1.5f64.fract(), 0.5f64);
1076         assert_approx_eq!(1.7f64.fract(), 0.7f64);
1077         assert_approx_eq!(0.0f64.fract(), 0.0f64);
1078         assert_approx_eq!((-0.0f64).fract(), -0.0f64);
1079         assert_approx_eq!((-1.0f64).fract(), -0.0f64);
1080         assert_approx_eq!((-1.3f64).fract(), -0.3f64);
1081         assert_approx_eq!((-1.5f64).fract(), -0.5f64);
1082         assert_approx_eq!((-1.7f64).fract(), -0.7f64);
1083     }
1084
1085     #[test]
1086     fn test_asinh() {
1087         assert_eq!(0.0f64.asinh(), 0.0f64);
1088         assert_eq!((-0.0f64).asinh(), -0.0f64);
1089         assert_eq!(Float::infinity::<f64>().asinh(), Float::infinity::<f64>());
1090         assert_eq!(Float::neg_infinity::<f64>().asinh(), Float::neg_infinity::<f64>());
1091         assert!(Float::NaN::<f64>().asinh().is_NaN());
1092         assert_approx_eq!(2.0f64.asinh(), 1.443635475178810342493276740273105f64);
1093         assert_approx_eq!((-2.0f64).asinh(), -1.443635475178810342493276740273105f64);
1094     }
1095
1096     #[test]
1097     fn test_acosh() {
1098         assert_eq!(1.0f64.acosh(), 0.0f64);
1099         assert!(0.999f64.acosh().is_NaN());
1100         assert_eq!(Float::infinity::<f64>().acosh(), Float::infinity::<f64>());
1101         assert!(Float::neg_infinity::<f64>().acosh().is_NaN());
1102         assert!(Float::NaN::<f64>().acosh().is_NaN());
1103         assert_approx_eq!(2.0f64.acosh(), 1.31695789692481670862504634730796844f64);
1104         assert_approx_eq!(3.0f64.acosh(), 1.76274717403908605046521864995958461f64);
1105     }
1106
1107     #[test]
1108     fn test_atanh() {
1109         assert_eq!(0.0f64.atanh(), 0.0f64);
1110         assert_eq!((-0.0f64).atanh(), -0.0f64);
1111         assert_eq!(1.0f64.atanh(), Float::infinity::<f64>());
1112         assert_eq!((-1.0f64).atanh(), Float::neg_infinity::<f64>());
1113         assert!(2f64.atanh().atanh().is_NaN());
1114         assert!((-2f64).atanh().atanh().is_NaN());
1115         assert!(Float::infinity::<f64>().atanh().is_NaN());
1116         assert!(Float::neg_infinity::<f64>().atanh().is_NaN());
1117         assert!(Float::NaN::<f64>().atanh().is_NaN());
1118         assert_approx_eq!(0.5f64.atanh(), 0.54930614433405484569762261846126285f64);
1119         assert_approx_eq!((-0.5f64).atanh(), -0.54930614433405484569762261846126285f64);
1120     }
1121
1122     #[test]
1123     fn test_real_consts() {
1124         assert_approx_eq!(Real::two_pi::<f64>(), 2.0 * Real::pi::<f64>());
1125         assert_approx_eq!(Real::frac_pi_2::<f64>(), Real::pi::<f64>() / 2f64);
1126         assert_approx_eq!(Real::frac_pi_3::<f64>(), Real::pi::<f64>() / 3f64);
1127         assert_approx_eq!(Real::frac_pi_4::<f64>(), Real::pi::<f64>() / 4f64);
1128         assert_approx_eq!(Real::frac_pi_6::<f64>(), Real::pi::<f64>() / 6f64);
1129         assert_approx_eq!(Real::frac_pi_8::<f64>(), Real::pi::<f64>() / 8f64);
1130         assert_approx_eq!(Real::frac_1_pi::<f64>(), 1f64 / Real::pi::<f64>());
1131         assert_approx_eq!(Real::frac_2_pi::<f64>(), 2f64 / Real::pi::<f64>());
1132         assert_approx_eq!(Real::frac_2_sqrtpi::<f64>(), 2f64 / Real::pi::<f64>().sqrt());
1133         assert_approx_eq!(Real::sqrt2::<f64>(), 2f64.sqrt());
1134         assert_approx_eq!(Real::frac_1_sqrt2::<f64>(), 1f64 / 2f64.sqrt());
1135         assert_approx_eq!(Real::log2_e::<f64>(), Real::e::<f64>().log2());
1136         assert_approx_eq!(Real::log10_e::<f64>(), Real::e::<f64>().log10());
1137         assert_approx_eq!(Real::ln_2::<f64>(), 2f64.ln());
1138         assert_approx_eq!(Real::ln_10::<f64>(), 10f64.ln());
1139     }
1140
1141     #[test]
1142     pub fn test_abs() {
1143         assert_eq!(infinity.abs(), infinity);
1144         assert_eq!(1f64.abs(), 1f64);
1145         assert_eq!(0f64.abs(), 0f64);
1146         assert_eq!((-0f64).abs(), 0f64);
1147         assert_eq!((-1f64).abs(), 1f64);
1148         assert_eq!(neg_infinity.abs(), infinity);
1149         assert_eq!((1f64/neg_infinity).abs(), 0f64);
1150         assert!(NaN.abs().is_NaN());
1151     }
1152
1153     #[test]
1154     fn test_abs_sub() {
1155         assert_eq!((-1f64).abs_sub(&1f64), 0f64);
1156         assert_eq!(1f64.abs_sub(&1f64), 0f64);
1157         assert_eq!(1f64.abs_sub(&0f64), 1f64);
1158         assert_eq!(1f64.abs_sub(&-1f64), 2f64);
1159         assert_eq!(neg_infinity.abs_sub(&0f64), 0f64);
1160         assert_eq!(infinity.abs_sub(&1f64), infinity);
1161         assert_eq!(0f64.abs_sub(&neg_infinity), infinity);
1162         assert_eq!(0f64.abs_sub(&infinity), 0f64);
1163         assert!(NaN.abs_sub(&-1f64).is_NaN());
1164         assert!(1f64.abs_sub(&NaN).is_NaN());
1165     }
1166
1167     #[test]
1168     fn test_signum() {
1169         assert_eq!(infinity.signum(), 1f64);
1170         assert_eq!(1f64.signum(), 1f64);
1171         assert_eq!(0f64.signum(), 1f64);
1172         assert_eq!((-0f64).signum(), -1f64);
1173         assert_eq!((-1f64).signum(), -1f64);
1174         assert_eq!(neg_infinity.signum(), -1f64);
1175         assert_eq!((1f64/neg_infinity).signum(), -1f64);
1176         assert!(NaN.signum().is_NaN());
1177     }
1178
1179     #[test]
1180     fn test_is_positive() {
1181         assert!(infinity.is_positive());
1182         assert!(1f64.is_positive());
1183         assert!(0f64.is_positive());
1184         assert!(!(-0f64).is_positive());
1185         assert!(!(-1f64).is_positive());
1186         assert!(!neg_infinity.is_positive());
1187         assert!(!(1f64/neg_infinity).is_positive());
1188         assert!(!NaN.is_positive());
1189     }
1190
1191     #[test]
1192     fn test_is_negative() {
1193         assert!(!infinity.is_negative());
1194         assert!(!1f64.is_negative());
1195         assert!(!0f64.is_negative());
1196         assert!((-0f64).is_negative());
1197         assert!((-1f64).is_negative());
1198         assert!(neg_infinity.is_negative());
1199         assert!((1f64/neg_infinity).is_negative());
1200         assert!(!NaN.is_negative());
1201     }
1202
1203     #[test]
1204     fn test_approx_eq() {
1205         assert!(1.0f64.approx_eq(&1f64));
1206         assert!(0.9999999f64.approx_eq(&1f64));
1207         assert!(1.000001f64.approx_eq_eps(&1f64, &1.0e-5));
1208         assert!(1.0000001f64.approx_eq_eps(&1f64, &1.0e-6));
1209         assert!(!1.0000001f64.approx_eq_eps(&1f64, &1.0e-7));
1210     }
1211
1212     #[test]
1213     fn test_primitive() {
1214         assert_eq!(Primitive::bits::<f64>(), sys::size_of::<f64>() * 8);
1215         assert_eq!(Primitive::bytes::<f64>(), sys::size_of::<f64>());
1216     }
1217
1218     #[test]
1219     fn test_is_normal() {
1220         assert!(!Float::NaN::<f64>().is_normal());
1221         assert!(!Float::infinity::<f64>().is_normal());
1222         assert!(!Float::neg_infinity::<f64>().is_normal());
1223         assert!(!Zero::zero::<f64>().is_normal());
1224         assert!(!Float::neg_zero::<f64>().is_normal());
1225         assert!(1f64.is_normal());
1226         assert!(1e-307f64.is_normal());
1227         assert!(!1e-308f64.is_normal());
1228     }
1229
1230     #[test]
1231     fn test_classify() {
1232         assert_eq!(Float::NaN::<f64>().classify(), FPNaN);
1233         assert_eq!(Float::infinity::<f64>().classify(), FPInfinite);
1234         assert_eq!(Float::neg_infinity::<f64>().classify(), FPInfinite);
1235         assert_eq!(Zero::zero::<f64>().classify(), FPZero);
1236         assert_eq!(Float::neg_zero::<f64>().classify(), FPZero);
1237         assert_eq!(1e-307f64.classify(), FPNormal);
1238         assert_eq!(1e-308f64.classify(), FPSubnormal);
1239     }
1240
1241     #[test]
1242     fn test_ldexp() {
1243         // We have to use from_str until base-2 exponents
1244         // are supported in floating-point literals
1245         let f1: f64 = from_str_hex("1p-123").unwrap();
1246         let f2: f64 = from_str_hex("1p-111").unwrap();
1247         assert_eq!(Float::ldexp(1f64, -123), f1);
1248         assert_eq!(Float::ldexp(1f64, -111), f2);
1249
1250         assert_eq!(Float::ldexp(0f64, -123), 0f64);
1251         assert_eq!(Float::ldexp(-0f64, -123), -0f64);
1252         assert_eq!(Float::ldexp(Float::infinity::<f64>(), -123),
1253                    Float::infinity::<f64>());
1254         assert_eq!(Float::ldexp(Float::neg_infinity::<f64>(), -123),
1255                    Float::neg_infinity::<f64>());
1256         assert!(Float::ldexp(Float::NaN::<f64>(), -123).is_NaN());
1257     }
1258
1259     #[test]
1260     fn test_frexp() {
1261         // We have to use from_str until base-2 exponents
1262         // are supported in floating-point literals
1263         let f1: f64 = from_str_hex("1p-123").unwrap();
1264         let f2: f64 = from_str_hex("1p-111").unwrap();
1265         let (x1, exp1) = f1.frexp();
1266         let (x2, exp2) = f2.frexp();
1267         assert_eq!((x1, exp1), (0.5f64, -122));
1268         assert_eq!((x2, exp2), (0.5f64, -110));
1269         assert_eq!(Float::ldexp(x1, exp1), f1);
1270         assert_eq!(Float::ldexp(x2, exp2), f2);
1271
1272         assert_eq!(0f64.frexp(), (0f64, 0));
1273         assert_eq!((-0f64).frexp(), (-0f64, 0));
1274         assert_eq!(match Float::infinity::<f64>().frexp() { (x, _) => x },
1275                    Float::infinity::<f64>())
1276         assert_eq!(match Float::neg_infinity::<f64>().frexp() { (x, _) => x },
1277                    Float::neg_infinity::<f64>())
1278         assert!(match Float::NaN::<f64>().frexp() { (x, _) => x.is_NaN() })
1279     }
1280 }