|
| 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 32-bits floats (`f32` type) |
| 12 | +
|
| 13 | +use cmp::{Eq, Ord}; |
| 14 | +use default::Default; |
| 15 | +use intrinsics; |
| 16 | +use num::{Zero, One, Bounded, Signed, Num, Primitive}; |
| 17 | +use ops::{Add, Sub, Mul, Div, Rem, Neg}; |
| 18 | + |
| 19 | +pub static RADIX: uint = 2u; |
| 20 | + |
| 21 | +pub static MANTISSA_DIGITS: uint = 24u; |
| 22 | +pub static DIGITS: uint = 6u; |
| 23 | + |
| 24 | +pub static EPSILON: f32 = 1.19209290e-07_f32; |
| 25 | + |
| 26 | +/// Smallest finite f32 value |
| 27 | +pub static MIN_VALUE: f32 = -3.40282347e+38_f32; |
| 28 | +/// Smallest positive, normalized f32 value |
| 29 | +pub static MIN_POS_VALUE: f32 = 1.17549435e-38_f32; |
| 30 | +/// Largest finite f32 value |
| 31 | +pub static MAX_VALUE: f32 = 3.40282347e+38_f32; |
| 32 | + |
| 33 | +pub static MIN_EXP: int = -125; |
| 34 | +pub static MAX_EXP: int = 128; |
| 35 | + |
| 36 | +pub static MIN_10_EXP: int = -37; |
| 37 | +pub static MAX_10_EXP: int = 38; |
| 38 | + |
| 39 | +pub static NAN: f32 = 0.0_f32/0.0_f32; |
| 40 | +pub static INFINITY: f32 = 1.0_f32/0.0_f32; |
| 41 | +pub static NEG_INFINITY: f32 = -1.0_f32/0.0_f32; |
| 42 | + |
| 43 | +/// Various useful constants. |
| 44 | +pub mod consts { |
| 45 | + // FIXME: replace with mathematical constants from cmath. |
| 46 | + |
| 47 | + // FIXME(#5527): These constants should be deprecated once associated |
| 48 | + // constants are implemented in favour of referencing the respective members |
| 49 | + // of `Float`. |
| 50 | + |
| 51 | + /// Archimedes' constant |
| 52 | + pub static PI: f32 = 3.14159265358979323846264338327950288_f32; |
| 53 | + |
| 54 | + /// pi * 2.0 |
| 55 | + pub static PI_2: f32 = 6.28318530717958647692528676655900576_f32; |
| 56 | + |
| 57 | + /// pi/2.0 |
| 58 | + pub static FRAC_PI_2: f32 = 1.57079632679489661923132169163975144_f32; |
| 59 | + |
| 60 | + /// pi/3.0 |
| 61 | + pub static FRAC_PI_3: f32 = 1.04719755119659774615421446109316763_f32; |
| 62 | + |
| 63 | + /// pi/4.0 |
| 64 | + pub static FRAC_PI_4: f32 = 0.785398163397448309615660845819875721_f32; |
| 65 | + |
| 66 | + /// pi/6.0 |
| 67 | + pub static FRAC_PI_6: f32 = 0.52359877559829887307710723054658381_f32; |
| 68 | + |
| 69 | + /// pi/8.0 |
| 70 | + pub static FRAC_PI_8: f32 = 0.39269908169872415480783042290993786_f32; |
| 71 | + |
| 72 | + /// 1.0/pi |
| 73 | + pub static FRAC_1_PI: f32 = 0.318309886183790671537767526745028724_f32; |
| 74 | + |
| 75 | + /// 2.0/pi |
| 76 | + pub static FRAC_2_PI: f32 = 0.636619772367581343075535053490057448_f32; |
| 77 | + |
| 78 | + /// 2.0/sqrt(pi) |
| 79 | + pub static FRAC_2_SQRTPI: f32 = 1.12837916709551257389615890312154517_f32; |
| 80 | + |
| 81 | + /// sqrt(2.0) |
| 82 | + pub static SQRT2: f32 = 1.41421356237309504880168872420969808_f32; |
| 83 | + |
| 84 | + /// 1.0/sqrt(2.0) |
| 85 | + pub static FRAC_1_SQRT2: f32 = 0.707106781186547524400844362104849039_f32; |
| 86 | + |
| 87 | + /// Euler's number |
| 88 | + pub static E: f32 = 2.71828182845904523536028747135266250_f32; |
| 89 | + |
| 90 | + /// log2(e) |
| 91 | + pub static LOG2_E: f32 = 1.44269504088896340735992468100189214_f32; |
| 92 | + |
| 93 | + /// log10(e) |
| 94 | + pub static LOG10_E: f32 = 0.434294481903251827651128918916605082_f32; |
| 95 | + |
| 96 | + /// ln(2.0) |
| 97 | + pub static LN_2: f32 = 0.693147180559945309417232121458176568_f32; |
| 98 | + |
| 99 | + /// ln(10.0) |
| 100 | + pub static LN_10: f32 = 2.30258509299404568401799145468436421_f32; |
| 101 | +} |
| 102 | + |
| 103 | +impl Ord for f32 { |
| 104 | + #[inline] |
| 105 | + fn lt(&self, other: &f32) -> bool { (*self) < (*other) } |
| 106 | + #[inline] |
| 107 | + fn le(&self, other: &f32) -> bool { (*self) <= (*other) } |
| 108 | + #[inline] |
| 109 | + fn ge(&self, other: &f32) -> bool { (*self) >= (*other) } |
| 110 | + #[inline] |
| 111 | + fn gt(&self, other: &f32) -> bool { (*self) > (*other) } |
| 112 | +} |
| 113 | +impl Eq for f32 { |
| 114 | + #[inline] |
| 115 | + fn eq(&self, other: &f32) -> bool { (*self) == (*other) } |
| 116 | +} |
| 117 | + |
| 118 | +impl Num for f32 {} |
| 119 | + |
| 120 | +impl Default for f32 { |
| 121 | + #[inline] |
| 122 | + fn default() -> f32 { 0.0 } |
| 123 | +} |
| 124 | + |
| 125 | +impl Primitive for f32 {} |
| 126 | + |
| 127 | +impl Zero for f32 { |
| 128 | + #[inline] |
| 129 | + fn zero() -> f32 { 0.0 } |
| 130 | + |
| 131 | + /// Returns true if the number is equal to either `0.0` or `-0.0` |
| 132 | + #[inline] |
| 133 | + fn is_zero(&self) -> bool { *self == 0.0 || *self == -0.0 } |
| 134 | +} |
| 135 | + |
| 136 | +impl One for f32 { |
| 137 | + #[inline] |
| 138 | + fn one() -> f32 { 1.0 } |
| 139 | +} |
| 140 | + |
| 141 | +#[cfg(not(test))] |
| 142 | +impl Add<f32,f32> for f32 { |
| 143 | + #[inline] |
| 144 | + fn add(&self, other: &f32) -> f32 { *self + *other } |
| 145 | +} |
| 146 | + |
| 147 | +#[cfg(not(test))] |
| 148 | +impl Sub<f32,f32> for f32 { |
| 149 | + #[inline] |
| 150 | + fn sub(&self, other: &f32) -> f32 { *self - *other } |
| 151 | +} |
| 152 | + |
| 153 | +#[cfg(not(test))] |
| 154 | +impl Mul<f32,f32> for f32 { |
| 155 | + #[inline] |
| 156 | + fn mul(&self, other: &f32) -> f32 { *self * *other } |
| 157 | +} |
| 158 | + |
| 159 | +#[cfg(not(test))] |
| 160 | +impl Div<f32,f32> for f32 { |
| 161 | + #[inline] |
| 162 | + fn div(&self, other: &f32) -> f32 { *self / *other } |
| 163 | +} |
| 164 | + |
| 165 | +#[cfg(not(test))] |
| 166 | +impl Rem<f32,f32> for f32 { |
| 167 | + #[inline] |
| 168 | + fn rem(&self, other: &f32) -> f32 { |
| 169 | + extern { fn fmodf(a: f32, b: f32) -> f32; } |
| 170 | + unsafe { fmodf(*self, *other) } |
| 171 | + } |
| 172 | +} |
| 173 | + |
| 174 | +#[cfg(not(test))] |
| 175 | +impl Neg<f32> for f32 { |
| 176 | + #[inline] |
| 177 | + fn neg(&self) -> f32 { -*self } |
| 178 | +} |
| 179 | + |
| 180 | +impl Signed for f32 { |
| 181 | + /// Computes the absolute value. Returns `NAN` if the number is `NAN`. |
| 182 | + #[inline] |
| 183 | + fn abs(&self) -> f32 { |
| 184 | + unsafe { intrinsics::fabsf32(*self) } |
| 185 | + } |
| 186 | + |
| 187 | + /// The positive difference of two numbers. Returns `0.0` if the number is |
| 188 | + /// less than or equal to `other`, otherwise the difference between`self` |
| 189 | + /// and `other` is returned. |
| 190 | + #[inline] |
| 191 | + fn abs_sub(&self, other: &f32) -> f32 { |
| 192 | + extern { fn fdimf(a: f32, b: f32) -> f32; } |
| 193 | + unsafe { fdimf(*self, *other) } |
| 194 | + } |
| 195 | + |
| 196 | + /// # Returns |
| 197 | + /// |
| 198 | + /// - `1.0` if the number is positive, `+0.0` or `INFINITY` |
| 199 | + /// - `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY` |
| 200 | + /// - `NAN` if the number is NaN |
| 201 | + #[inline] |
| 202 | + fn signum(&self) -> f32 { |
| 203 | + if self != self { NAN } else { |
| 204 | + unsafe { intrinsics::copysignf32(1.0, *self) } |
| 205 | + } |
| 206 | + } |
| 207 | + |
| 208 | + /// Returns `true` if the number is positive, including `+0.0` and `INFINITY` |
| 209 | + #[inline] |
| 210 | + fn is_positive(&self) -> bool { *self > 0.0 || (1.0 / *self) == INFINITY } |
| 211 | + |
| 212 | + /// Returns `true` if the number is negative, including `-0.0` and `NEG_INFINITY` |
| 213 | + #[inline] |
| 214 | + fn is_negative(&self) -> bool { *self < 0.0 || (1.0 / *self) == NEG_INFINITY } |
| 215 | +} |
| 216 | + |
| 217 | +impl Bounded for f32 { |
| 218 | + // NOTE: this is the smallest non-infinite f32 value, *not* MIN_VALUE |
| 219 | + #[inline] |
| 220 | + fn min_value() -> f32 { -MAX_VALUE } |
| 221 | + |
| 222 | + #[inline] |
| 223 | + fn max_value() -> f32 { MAX_VALUE } |
| 224 | +} |
0 commit comments