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