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