| 1 |
// Written in the D programming language. |
| 2 |
|
| 3 |
/** |
| 4 |
* Builtin mathematical intrinsics |
| 5 |
* |
| 6 |
* Source: $(DRUNTIMESRC core/_math.d) |
| 7 |
* Macros: |
| 8 |
* TABLE_SV = <table border="1" cellpadding="4" cellspacing="0"> |
| 9 |
* <caption>Special Values</caption> |
| 10 |
* $0</table> |
| 11 |
* |
| 12 |
* NAN = $(RED NAN) |
| 13 |
* SUP = <span style="vertical-align:super;font-size:smaller">$0</span> |
| 14 |
* POWER = $1<sup>$2</sup> |
| 15 |
* PLUSMN = ± |
| 16 |
* INFIN = ∞ |
| 17 |
* PLUSMNINF = ±∞ |
| 18 |
* LT = < |
| 19 |
* GT = > |
| 20 |
* |
| 21 |
* Copyright: Copyright Digital Mars 2000 - 2011. |
| 22 |
* License: $(HTTP www.boost.org/LICENSE_1_0.txt, Boost License 1.0). |
| 23 |
* Authors: $(HTTP digitalmars.com, Walter Bright), |
| 24 |
* Don Clugston |
| 25 |
*/ |
| 26 |
module core.math; |
| 27 |
|
| 28 |
public: |
| 29 |
@nogc: |
| 30 |
nothrow: |
| 31 |
@safe: |
| 32 |
|
| 33 |
pure: |
| 34 |
/*********************************** |
| 35 |
* Returns cosine of x. x is in radians. |
| 36 |
* |
| 37 |
* $(TABLE_SV |
| 38 |
* $(TR $(TH x) $(TH cos(x)) $(TH invalid?)) |
| 39 |
* $(TR $(TD $(NAN)) $(TD $(NAN)) $(TD yes) ) |
| 40 |
* $(TR $(TD $(PLUSMN)$(INFIN)) $(TD $(NAN)) $(TD yes) ) |
| 41 |
* ) |
| 42 |
* Bugs: |
| 43 |
* Results are undefined if |x| >= $(POWER 2,64). |
| 44 |
*/ |
| 45 |
|
| 46 |
float cos(float x); /* intrinsic */ |
| 47 |
double cos(double x); /* intrinsic */ /// ditto |
| 48 |
real cos(real x); /* intrinsic */ /// ditto |
| 49 |
|
| 50 |
/*********************************** |
| 51 |
* Returns sine of x. x is in radians. |
| 52 |
* |
| 53 |
* $(TABLE_SV |
| 54 |
* $(TR $(TH x) $(TH sin(x)) $(TH invalid?)) |
| 55 |
* $(TR $(TD $(NAN)) $(TD $(NAN)) $(TD yes)) |
| 56 |
* $(TR $(TD $(PLUSMN)0.0) $(TD $(PLUSMN)0.0) $(TD no)) |
| 57 |
* $(TR $(TD $(PLUSMNINF)) $(TD $(NAN)) $(TD yes)) |
| 58 |
* ) |
| 59 |
* Bugs: |
| 60 |
* Results are undefined if |x| >= $(POWER 2,64). |
| 61 |
*/ |
| 62 |
|
| 63 |
float sin(float x); /* intrinsic */ |
| 64 |
double sin(double x); /* intrinsic */ /// ditto |
| 65 |
real sin(real x); /* intrinsic */ /// ditto |
| 66 |
|
| 67 |
/***************************************** |
| 68 |
* Returns x rounded to a long value using the current rounding mode. |
| 69 |
* If the integer value of x is |
| 70 |
* greater than long.max, the result is |
| 71 |
* indeterminate. |
| 72 |
*/ |
| 73 |
|
| 74 |
long rndtol(float x); /* intrinsic */ |
| 75 |
long rndtol(double x); /* intrinsic */ /// ditto |
| 76 |
long rndtol(real x); /* intrinsic */ /// ditto |
| 77 |
|
| 78 |
/*************************************** |
| 79 |
* Compute square root of x. |
| 80 |
* |
| 81 |
* $(TABLE_SV |
| 82 |
* $(TR $(TH x) $(TH sqrt(x)) $(TH invalid?)) |
| 83 |
* $(TR $(TD -0.0) $(TD -0.0) $(TD no)) |
| 84 |
* $(TR $(TD $(LT)0.0) $(TD $(NAN)) $(TD yes)) |
| 85 |
* $(TR $(TD +$(INFIN)) $(TD +$(INFIN)) $(TD no)) |
| 86 |
* ) |
| 87 |
*/ |
| 88 |
|
| 89 |
float sqrt(float x); /* intrinsic */ |
| 90 |
double sqrt(double x); /* intrinsic */ /// ditto |
| 91 |
real sqrt(real x); /* intrinsic */ /// ditto |
| 92 |
|
| 93 |
/******************************************* |
| 94 |
* Compute n * 2$(SUPERSCRIPT exp) |
| 95 |
* References: frexp |
| 96 |
*/ |
| 97 |
|
| 98 |
float ldexp(float n, int exp); /* intrinsic */ |
| 99 |
double ldexp(double n, int exp); /* intrinsic */ /// ditto |
| 100 |
real ldexp(real n, int exp); /* intrinsic */ /// ditto |
| 101 |
|
| 102 |
unittest { |
| 103 |
static if (real.mant_dig == 113) |
| 104 |
{ |
| 105 |
assert(ldexp(1.0L, -16384) == 0x1p-16384L); |
| 106 |
assert(ldexp(1.0L, -16382) == 0x1p-16382L); |
| 107 |
} |
| 108 |
else static if (real.mant_dig == 106) |
| 109 |
{ |
| 110 |
assert(ldexp(1.0L, 1023) == 0x1p1023L); |
| 111 |
assert(ldexp(1.0L, -1022) == 0x1p-1022L); |
| 112 |
assert(ldexp(1.0L, -1021) == 0x1p-1021L); |
| 113 |
} |
| 114 |
else static if (real.mant_dig == 64) |
| 115 |
{ |
| 116 |
assert(ldexp(1.0L, -16384) == 0x1p-16384L); |
| 117 |
assert(ldexp(1.0L, -16382) == 0x1p-16382L); |
| 118 |
} |
| 119 |
else static if (real.mant_dig == 53) |
| 120 |
{ |
| 121 |
assert(ldexp(1.0L, 1023) == 0x1p1023L); |
| 122 |
assert(ldexp(1.0L, -1022) == 0x1p-1022L); |
| 123 |
assert(ldexp(1.0L, -1021) == 0x1p-1021L); |
| 124 |
} |
| 125 |
else |
| 126 |
assert(false, "Only 128bit, 80bit and 64bit reals expected here"); |
| 127 |
} |
| 128 |
|
| 129 |
/******************************* |
| 130 |
* Compute the absolute value. |
| 131 |
* $(TABLE_SV |
| 132 |
* $(TR $(TH x) $(TH fabs(x))) |
| 133 |
* $(TR $(TD $(PLUSMN)0.0) $(TD +0.0) ) |
| 134 |
* $(TR $(TD $(PLUSMN)$(INFIN)) $(TD +$(INFIN)) ) |
| 135 |
* ) |
| 136 |
* It is implemented as a compiler intrinsic. |
| 137 |
* Params: |
| 138 |
* x = floating point value |
| 139 |
* Returns: |x| |
| 140 |
* References: equivalent to `std.math.fabs` |
| 141 |
*/ |
| 142 |
@safe pure nothrow @nogc |
| 143 |
{ |
| 144 |
float fabs(float x); |
| 145 |
double fabs(double x); /// ditto |
| 146 |
real fabs(real x); /// ditto |
| 147 |
} |
| 148 |
|
| 149 |
/********************************** |
| 150 |
* Rounds x to the nearest integer value, using the current rounding |
| 151 |
* mode. |
| 152 |
* If the return value is not equal to x, the FE_INEXACT |
| 153 |
* exception is raised. |
| 154 |
* $(B nearbyint) performs |
| 155 |
* the same operation, but does not set the FE_INEXACT exception. |
| 156 |
*/ |
| 157 |
float rint(float x); /* intrinsic */ |
| 158 |
double rint(double x); /* intrinsic */ /// ditto |
| 159 |
real rint(real x); /* intrinsic */ /// ditto |
| 160 |
|
| 161 |
/*********************************** |
| 162 |
* Building block functions, they |
| 163 |
* translate to a single x87 instruction. |
| 164 |
*/ |
| 165 |
// y * log2(x) |
| 166 |
float yl2x(float x, float y); /* intrinsic */ |
| 167 |
double yl2x(double x, double y); /* intrinsic */ /// ditto |
| 168 |
real yl2x(real x, real y); /* intrinsic */ /// ditto |
| 169 |
// y * log2(x +1) |
| 170 |
float yl2xp1(float x, float y); /* intrinsic */ |
| 171 |
double yl2xp1(double x, double y); /* intrinsic */ /// ditto |
| 172 |
real yl2xp1(real x, real y); /* intrinsic */ /// ditto |
| 173 |
|
| 174 |
unittest |
| 175 |
{ |
| 176 |
version (INLINE_YL2X) |
| 177 |
{ |
| 178 |
assert(yl2x(1024.0L, 1) == 10); |
| 179 |
assert(yl2xp1(1023.0L, 1) == 10); |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
/************************************* |
| 184 |
* Round argument to a specific precision. |
| 185 |
* |
| 186 |
* D language types specify only a minimum precision, not a maximum. The |
| 187 |
* `toPrec()` function forces rounding of the argument `f` to the precision |
| 188 |
* of the specified floating point type `T`. |
| 189 |
* The rounding mode used is inevitably target-dependent, but will be done in |
| 190 |
* a way to maximize accuracy. In most cases, the default is round-to-nearest. |
| 191 |
* |
| 192 |
* Params: |
| 193 |
* T = precision type to round to |
| 194 |
* f = value to convert |
| 195 |
* Returns: |
| 196 |
* f in precision of type `T` |
| 197 |
*/ |
| 198 |
T toPrec(T:float)(float f) { pragma(inline, false); return f; } |
| 199 |
/// ditto |
| 200 |
T toPrec(T:float)(double f) { pragma(inline, false); return cast(T) f; } |
| 201 |
/// ditto |
| 202 |
T toPrec(T:float)(real f) { pragma(inline, false); return cast(T) f; } |
| 203 |
/// ditto |
| 204 |
T toPrec(T:double)(float f) { pragma(inline, false); return f; } |
| 205 |
/// ditto |
| 206 |
T toPrec(T:double)(double f) { pragma(inline, false); return f; } |
| 207 |
/// ditto |
| 208 |
T toPrec(T:double)(real f) { pragma(inline, false); return cast(T) f; } |
| 209 |
/// ditto |
| 210 |
T toPrec(T:real)(float f) { pragma(inline, false); return f; } |
| 211 |
/// ditto |
| 212 |
T toPrec(T:real)(double f) { pragma(inline, false); return f; } |
| 213 |
/// ditto |
| 214 |
T toPrec(T:real)(real f) { pragma(inline, false); return f; } |
| 215 |
|
| 216 |
@safe unittest |
| 217 |
{ |
| 218 |
// Test all instantiations work with all combinations of float. |
| 219 |
float f = 1.1f; |
| 220 |
double d = 1.1; |
| 221 |
real r = 1.1L; |
| 222 |
f = toPrec!float(f + f); |
| 223 |
f = toPrec!float(d + d); |
| 224 |
f = toPrec!float(r + r); |
| 225 |
d = toPrec!double(f + f); |
| 226 |
d = toPrec!double(d + d); |
| 227 |
d = toPrec!double(r + r); |
| 228 |
r = toPrec!real(f + f); |
| 229 |
r = toPrec!real(d + d); |
| 230 |
r = toPrec!real(r + r); |
| 231 |
|
| 232 |
// Comparison tests. |
| 233 |
bool approxEqual(T)(T lhs, T rhs) |
| 234 |
{ |
| 235 |
return fabs((lhs - rhs) / rhs) <= 1e-2 || fabs(lhs - rhs) <= 1e-5; |
| 236 |
} |
| 237 |
|
| 238 |
enum real PIR = 0xc.90fdaa22168c235p-2; |
| 239 |
enum double PID = 0x1.921fb54442d18p+1; |
| 240 |
enum float PIF = 0x1.921fb6p+1; |
| 241 |
static assert(approxEqual(toPrec!float(PIR), PIF)); |
| 242 |
static assert(approxEqual(toPrec!double(PIR), PID)); |
| 243 |
static assert(approxEqual(toPrec!real(PIR), PIR)); |
| 244 |
static assert(approxEqual(toPrec!float(PID), PIF)); |
| 245 |
static assert(approxEqual(toPrec!double(PID), PID)); |
| 246 |
static assert(approxEqual(toPrec!real(PID), PID)); |
| 247 |
static assert(approxEqual(toPrec!float(PIF), PIF)); |
| 248 |
static assert(approxEqual(toPrec!double(PIF), PIF)); |
| 249 |
static assert(approxEqual(toPrec!real(PIF), PIF)); |
| 250 |
|
| 251 |
assert(approxEqual(toPrec!float(PIR), PIF)); |
| 252 |
assert(approxEqual(toPrec!double(PIR), PID)); |
| 253 |
assert(approxEqual(toPrec!real(PIR), PIR)); |
| 254 |
assert(approxEqual(toPrec!float(PID), PIF)); |
| 255 |
assert(approxEqual(toPrec!double(PID), PID)); |
| 256 |
assert(approxEqual(toPrec!real(PID), PID)); |
| 257 |
assert(approxEqual(toPrec!float(PIF), PIF)); |
| 258 |
assert(approxEqual(toPrec!double(PIF), PIF)); |
| 259 |
assert(approxEqual(toPrec!real(PIF), PIF)); |
| 260 |
} |