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: $(WEB www.boost.org/LICENSE_1_0.txt, Boost License 1.0). 23 * Authors: $(WEB digitalmars.com, Walter Bright), 24 * Don Clugston 25 */ 26 module core.math; 27 28 public: 29 @nogc: 30 31 /*********************************** 32 * Returns cosine of x. x is in radians. 33 * 34 * $(TABLE_SV 35 * $(TR $(TH x) $(TH cos(x)) $(TH invalid?)) 36 * $(TR $(TD $(NAN)) $(TD $(NAN)) $(TD yes) ) 37 * $(TR $(TD $(PLUSMN)$(INFIN)) $(TD $(NAN)) $(TD yes) ) 38 * ) 39 * Bugs: 40 * Results are undefined if |x| >= $(POWER 2,64). 41 */ 42 43 real cos(real x) @safe pure nothrow; /* intrinsic */ 44 45 /*********************************** 46 * Returns sine of x. x is in radians. 47 * 48 * $(TABLE_SV 49 * $(TR $(TH x) $(TH sin(x)) $(TH invalid?)) 50 * $(TR $(TD $(NAN)) $(TD $(NAN)) $(TD yes)) 51 * $(TR $(TD $(PLUSMN)0.0) $(TD $(PLUSMN)0.0) $(TD no)) 52 * $(TR $(TD $(PLUSMNINF)) $(TD $(NAN)) $(TD yes)) 53 * ) 54 * Bugs: 55 * Results are undefined if |x| >= $(POWER 2,64). 56 */ 57 58 real sin(real x) @safe pure nothrow; /* intrinsic */ 59 60 /***************************************** 61 * Returns x rounded to a long value using the current rounding mode. 62 * If the integer value of x is 63 * greater than long.max, the result is 64 * indeterminate. 65 */ 66 long rndtol(real x) @safe pure nothrow; /* intrinsic */ 67 68 69 /***************************************** 70 * Returns x rounded to a long value using the FE_TONEAREST rounding mode. 71 * If the integer value of x is 72 * greater than long.max, the result is 73 * indeterminate. 74 */ 75 extern (C) real rndtonl(real x); 76 77 /*************************************** 78 * Compute square root of x. 79 * 80 * $(TABLE_SV 81 * $(TR $(TH x) $(TH sqrt(x)) $(TH invalid?)) 82 * $(TR $(TD -0.0) $(TD -0.0) $(TD no)) 83 * $(TR $(TD $(LT)0.0) $(TD $(NAN)) $(TD yes)) 84 * $(TR $(TD +$(INFIN)) $(TD +$(INFIN)) $(TD no)) 85 * ) 86 */ 87 88 @safe pure nothrow 89 { 90 float sqrt(float x); /* intrinsic */ 91 double sqrt(double x); /* intrinsic */ /// ditto 92 real sqrt(real x); /* intrinsic */ /// ditto 93 } 94 95 /******************************************* 96 * Compute n * 2$(SUPERSCRIPT exp) 97 * References: frexp 98 */ 99 100 real ldexp(real n, int exp) @safe pure nothrow; /* intrinsic */ 101 102 unittest { 103 static if (real.mant_dig == 113) 104 { 105 assert(ldexp(1, -16384) == 0x1p-16384L); 106 assert(ldexp(1, -16382) == 0x1p-16382L); 107 } 108 else static if (real.mant_dig == 106) 109 { 110 assert(ldexp(1, 1023) == 0x1p1023L); 111 assert(ldexp(1, -1022) == 0x1p-1022L); 112 assert(ldexp(1, -1021) == 0x1p-1021L); 113 } 114 else static if (real.mant_dig == 64) 115 { 116 assert(ldexp(1, -16384) == 0x1p-16384L); 117 assert(ldexp(1, -16382) == 0x1p-16382L); 118 } 119 else static if (real.mant_dig == 53) 120 { 121 assert(ldexp(1, 1023) == 0x1p1023L); 122 assert(ldexp(1, -1022) == 0x1p-1022L); 123 assert(ldexp(1, -1021) == 0x1p-1021L); 124 } 125 else 126 assert(false, "Only 128bit, 80bit and 64bit reals expected here"); 127 } 128 129 /******************************* 130 * Returns |x| 131 * 132 * $(TABLE_SV 133 * $(TR $(TH x) $(TH fabs(x))) 134 * $(TR $(TD $(PLUSMN)0.0) $(TD +0.0) ) 135 * $(TR $(TD $(PLUSMN)$(INFIN)) $(TD +$(INFIN)) ) 136 * ) 137 */ 138 real fabs(real x) @safe pure nothrow; /* intrinsic */ 139 140 /********************************** 141 * Rounds x to the nearest integer value, using the current rounding 142 * mode. 143 * If the return value is not equal to x, the FE_INEXACT 144 * exception is raised. 145 * $(B nearbyint) performs 146 * the same operation, but does not set the FE_INEXACT exception. 147 */ 148 real rint(real x) @safe pure nothrow; /* intrinsic */ 149 150 /*********************************** 151 * Building block functions, they 152 * translate to a single x87 instruction. 153 */ 154 155 real yl2x(real x, real y) @safe pure nothrow; // y * log2(x) 156 real yl2xp1(real x, real y) @safe pure nothrow; // y * log2(x + 1) 157 158 unittest 159 { version(INLINE_YL2X)160 version (INLINE_YL2X) 161 { 162 assert(yl2x(1024, 1) == 10); 163 assert(yl2xp1(1023, 1) == 10); 164 } 165 } 166 167