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.
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.
11 //! Operations and constants for 32-bits floats (`f32` type)
13 // FIXME: MIN_VALUE and MAX_VALUE literals are parsed as -inf and inf #14353
14 #![allow(overflowing_literals)]
16 #![stable(feature = "rust1", since = "1.0.0")]
21 use num
::FpCategory
as Fp
;
23 #[stable(feature = "rust1", since = "1.0.0")]
24 #[allow(missing_docs)]
25 pub const RADIX
: u32 = 2;
27 #[stable(feature = "rust1", since = "1.0.0")]
28 #[allow(missing_docs)]
29 pub const MANTISSA_DIGITS
: u32 = 24;
30 #[stable(feature = "rust1", since = "1.0.0")]
31 #[allow(missing_docs)]
32 pub const DIGITS
: u32 = 6;
34 #[stable(feature = "rust1", since = "1.0.0")]
35 #[allow(missing_docs)]
36 pub const EPSILON
: f32 = 1.19209290e-07_f32;
38 /// Smallest finite f32 value
39 #[stable(feature = "rust1", since = "1.0.0")]
40 pub const MIN
: f32 = -3.40282347e+38_f32;
41 /// Smallest positive, normalized f32 value
42 #[stable(feature = "rust1", since = "1.0.0")]
43 pub const MIN_POSITIVE
: f32 = 1.17549435e-38_f32;
44 /// Largest finite f32 value
45 #[stable(feature = "rust1", since = "1.0.0")]
46 pub const MAX
: f32 = 3.40282347e+38_f32;
48 #[stable(feature = "rust1", since = "1.0.0")]
49 #[allow(missing_docs)]
50 pub const MIN_EXP
: i32 = -125;
51 #[stable(feature = "rust1", since = "1.0.0")]
52 #[allow(missing_docs)]
53 pub const MAX_EXP
: i32 = 128;
55 #[stable(feature = "rust1", since = "1.0.0")]
56 #[allow(missing_docs)]
57 pub const MIN_10_EXP
: i32 = -37;
58 #[stable(feature = "rust1", since = "1.0.0")]
59 #[allow(missing_docs)]
60 pub const MAX_10_EXP
: i32 = 38;
62 #[stable(feature = "rust1", since = "1.0.0")]
63 #[allow(missing_docs)]
64 pub const NAN
: f32 = 0.0_f32/0.0_f32;
65 #[stable(feature = "rust1", since = "1.0.0")]
66 #[allow(missing_docs)]
67 pub const INFINITY
: f32 = 1.0_f32/0.0_f32;
68 #[stable(feature = "rust1", since = "1.0.0")]
69 #[allow(missing_docs)]
70 pub const NEG_INFINITY
: f32 = -1.0_f32/0.0_f32;
72 /// Basic mathematical constants.
73 #[stable(feature = "rust1", since = "1.0.0")]
75 // FIXME: replace with mathematical constants from cmath.
77 /// Archimedes' constant
78 #[stable(feature = "rust1", since = "1.0.0")]
79 pub const PI
: f32 = 3.14159265358979323846264338327950288_f32;
82 #[stable(feature = "rust1", since = "1.0.0")]
83 pub const FRAC_PI_2
: f32 = 1.57079632679489661923132169163975144_f32;
86 #[stable(feature = "rust1", since = "1.0.0")]
87 pub const FRAC_PI_3
: f32 = 1.04719755119659774615421446109316763_f32;
90 #[stable(feature = "rust1", since = "1.0.0")]
91 pub const FRAC_PI_4
: f32 = 0.785398163397448309615660845819875721_f32;
94 #[stable(feature = "rust1", since = "1.0.0")]
95 pub const FRAC_PI_6
: f32 = 0.52359877559829887307710723054658381_f32;
98 #[stable(feature = "rust1", since = "1.0.0")]
99 pub const FRAC_PI_8
: f32 = 0.39269908169872415480783042290993786_f32;
102 #[stable(feature = "rust1", since = "1.0.0")]
103 pub const FRAC_1_PI
: f32 = 0.318309886183790671537767526745028724_f32;
106 #[stable(feature = "rust1", since = "1.0.0")]
107 pub const FRAC_2_PI
: f32 = 0.636619772367581343075535053490057448_f32;
110 #[stable(feature = "rust1", since = "1.0.0")]
111 pub const FRAC_2_SQRT_PI
: f32 = 1.12837916709551257389615890312154517_f32;
114 #[stable(feature = "rust1", since = "1.0.0")]
115 pub const SQRT_2
: f32 = 1.41421356237309504880168872420969808_f32;
118 #[stable(feature = "rust1", since = "1.0.0")]
119 pub const FRAC_1_SQRT_2
: f32 = 0.707106781186547524400844362104849039_f32;
122 #[stable(feature = "rust1", since = "1.0.0")]
123 pub const E
: f32 = 2.71828182845904523536028747135266250_f32;
126 #[stable(feature = "rust1", since = "1.0.0")]
127 pub const LOG2_E
: f32 = 1.44269504088896340735992468100189214_f32;
130 #[stable(feature = "rust1", since = "1.0.0")]
131 pub const LOG10_E
: f32 = 0.434294481903251827651128918916605082_f32;
134 #[stable(feature = "rust1", since = "1.0.0")]
135 pub const LN_2
: f32 = 0.693147180559945309417232121458176568_f32;
138 #[stable(feature = "rust1", since = "1.0.0")]
139 pub const LN_10
: f32 = 2.30258509299404568401799145468436421_f32;
142 #[unstable(feature = "core_float",
143 reason
= "stable interface is via `impl f{32,64}` in later crates",
147 fn nan() -> f32 { NAN }
150 fn infinity() -> f32 { INFINITY }
153 fn neg_infinity() -> f32 { NEG_INFINITY }
156 fn zero() -> f32 { 0.0 }
159 fn neg_zero() -> f32 { -0.0 }
162 fn one() -> f32 { 1.0 }
164 /// Returns `true` if the number is NaN.
166 fn is_nan(self) -> bool { self != self }
168 /// Returns `true` if the number is infinite.
170 fn is_infinite(self) -> bool
{
171 self == Float
::infinity() || self == Float
::neg_infinity()
174 /// Returns `true` if the number is neither infinite or NaN.
176 fn is_finite(self) -> bool
{
177 !(self.is_nan() || self.is_infinite())
180 /// Returns `true` if the number is neither zero, infinite, subnormal or NaN.
182 fn is_normal(self) -> bool
{
183 self.classify() == Fp
::Normal
186 /// Returns the floating point category of the number. If only one property
187 /// is going to be tested, it is generally faster to use the specific
188 /// predicate instead.
189 fn classify(self) -> Fp
{
190 const EXP_MASK
: u32 = 0x7f800000;
191 const MAN_MASK
: u32 = 0x007fffff;
193 let bits
: u32 = unsafe { mem::transmute(self) }
;
194 match (bits
& MAN_MASK
, bits
& EXP_MASK
) {
196 (_
, 0) => Fp
::Subnormal
,
197 (0, EXP_MASK
) => Fp
::Infinite
,
198 (_
, EXP_MASK
) => Fp
::Nan
,
203 /// Returns the mantissa, exponent and sign as integers.
204 fn integer_decode(self) -> (u64, i16, i8) {
205 let bits
: u32 = unsafe { mem::transmute(self) }
;
206 let sign
: i8 = if bits
>> 31 == 0 { 1 }
else { -1 }
;
207 let mut exponent
: i16 = ((bits
>> 23) & 0xff) as i16;
208 let mantissa
= if exponent
== 0 {
209 (bits
& 0x7fffff) << 1
211 (bits
& 0x7fffff) | 0x800000
213 // Exponent bias + mantissa shift
214 exponent
-= 127 + 23;
215 (mantissa
as u64, exponent
, sign
)
218 /// Computes the absolute value of `self`. Returns `Float::nan()` if the
219 /// number is `Float::nan()`.
221 fn abs(self) -> f32 {
222 unsafe { intrinsics::fabsf32(self) }
225 /// Returns a number that represents the sign of `self`.
227 /// - `1.0` if the number is positive, `+0.0` or `Float::infinity()`
228 /// - `-1.0` if the number is negative, `-0.0` or `Float::neg_infinity()`
229 /// - `Float::nan()` if the number is `Float::nan()`
231 fn signum(self) -> f32 {
235 unsafe { intrinsics::copysignf32(1.0, self) }
239 /// Returns `true` if `self` is positive, including `+0.0` and
240 /// `Float::infinity()`.
242 fn is_sign_positive(self) -> bool
{
243 self > 0.0 || (1.0 / self) == Float
::infinity()
246 /// Returns `true` if `self` is negative, including `-0.0` and
247 /// `Float::neg_infinity()`.
249 fn is_sign_negative(self) -> bool
{
250 self < 0.0 || (1.0 / self) == Float
::neg_infinity()
253 /// Returns the reciprocal (multiplicative inverse) of the number.
255 fn recip(self) -> f32 { 1.0 / self }
258 fn powi(self, n
: i32) -> f32 {
259 unsafe { intrinsics::powif32(self, n) }
262 /// Converts to degrees, assuming the number is in radians.
264 fn to_degrees(self) -> f32 { self * (180.0f32 / consts::PI) }
266 /// Converts to radians, assuming the number is in degrees.
268 fn to_radians(self) -> f32 {
269 let value
: f32 = consts
::PI
;
270 self * (value
/ 180.0f32)