Changeset 1553
- Timestamp:
- 05/25/10 04:03:09 (15 years ago)
- Files:
-
- trunk/phobos/std/math.d (modified) (8 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/phobos/std/math.d
r1513 r1553 307 307 * 308 308 * $(TABLE_SV 309 309 * $(TR $(TH x) $(TH cos(x)) $(TH invalid?)) 310 310 * $(TR $(TD $(NAN)) $(TD $(NAN)) $(TD yes) ) 311 311 * $(TR $(TD $(PLUSMN)$(INFIN)) $(TD $(NAN)) $(TD yes) ) 312 312 * ) 313 313 * Bugs: 314 314 * Results are undefined if |x| >= $(POWER 2,64). 315 315 */ 316 316 317 pure nothrow real cos(real x); /* intrinsic */317 @safe pure nothrow real cos(real x); /* intrinsic */ 318 318 319 319 /*********************************** 320 320 * Returns sine of x. x is in radians. 321 321 * 322 322 * $(TABLE_SV 323 323 * $(TR $(TH x) $(TH sin(x)) $(TH invalid?)) 324 324 * $(TR $(TD $(NAN)) $(TD $(NAN)) $(TD yes)) 325 325 * $(TR $(TD $(PLUSMN)0.0) $(TD $(PLUSMN)0.0) $(TD no)) 326 326 * $(TR $(TD $(PLUSMNINF)) $(TD $(NAN)) $(TD yes)) 327 327 * ) 328 328 * Bugs: 329 329 * Results are undefined if |x| >= $(POWER 2,64). 330 330 */ 331 331 332 pure nothrow real sin(real x); /* intrinsic */332 @safe pure nothrow real sin(real x); /* intrinsic */ 333 333 334 334 335 335 /*********************************** 336 336 * sine, complex and imaginary 337 337 * 338 338 * sin(z) = sin(z.re)*cosh(z.im) + cos(z.re)*sinh(z.im)i 339 339 * 340 340 * If both sin($(THETA)) and cos($(THETA)) are required, 341 341 * it is most efficient to use expi($(THETA)). 342 342 */ … … 387 387 * Returns tangent of x. x is in radians. 388 388 * 389 389 * $(TABLE_SV 390 390 * $(TR $(TH x) $(TH tan(x)) $(TH invalid?)) 391 391 * $(TR $(TD $(NAN)) $(TD $(NAN)) $(TD yes)) 392 392 * $(TR $(TD $(PLUSMN)0.0) $(TD $(PLUSMN)0.0) $(TD no)) 393 393 * $(TR $(TD $(PLUSMNINF)) $(TD $(NAN)) $(TD yes)) 394 394 * ) 395 395 */ 396 396 397 pure nothrow real tan(real x)397 @trusted pure nothrow real tan(real x) 398 398 { 399 399 version(Naked_D_InlineAsm_X86) { 400 400 asm 401 401 { 402 402 fld x[EBP] ; // load theta 403 403 fxam ; // test for oddball values 404 404 fstsw AX ; 405 405 sahf ; 406 406 jc trigerr ; // x is NAN, infinity, or empty 407 407 // 387's can handle denormals … … 798 798 assert(isNaN(atanh(real.nan))); 799 799 assert(isNaN(atanh(-real.infinity))); 800 800 } 801 801 802 802 /***************************************** 803 803 * Returns x rounded to a long value using the current rounding mode. 804 804 * If the integer value of x is 805 805 * greater than long.max, the result is 806 806 * indeterminate. 807 807 */ 808 pure nothrow long rndtol(real x); /* intrinsic */808 @safe pure nothrow long rndtol(real x); /* intrinsic */ 809 809 810 810 811 811 /***************************************** 812 812 * Returns x rounded to a long value using the FE_TONEAREST rounding mode. 813 813 * If the integer value of x is 814 814 * greater than long.max, the result is 815 815 * indeterminate. 816 816 */ 817 817 extern (C) real rndtonl(real x); 818 818 … … 820 820 * Compute square root of x. 821 821 * 822 822 * $(TABLE_SV 823 823 * $(TR $(TH x) $(TH sqrt(x)) $(TH invalid?)) 824 824 * $(TR $(TD -0.0) $(TD -0.0) $(TD no)) 825 825 * $(TR $(TD $(LT)0.0) $(TD $(NAN)) $(TD yes)) 826 826 * $(TR $(TD +$(INFIN)) $(TD +$(INFIN)) $(TD no)) 827 827 * ) 828 828 */ 829 829 830 pure nothrow830 @safe pure nothrow 831 831 { 832 832 float sqrt(float x); /* intrinsic */ 833 833 double sqrt(double x); /* intrinsic */ /// ditto 834 834 real sqrt(real x); /* intrinsic */ /// ditto 835 835 } 836 836 837 837 pure nothrow creal sqrt(creal z) 838 838 { 839 839 creal c; 840 840 real x,y,w,r; … … 1378 1378 1379 1379 alias core.stdc.math.FP_ILOGB0 FP_ILOGB0; 1380 1380 alias core.stdc.math.FP_ILOGBNAN FP_ILOGBNAN; 1381 1381 1382 1382 1383 1383 /******************************************* 1384 1384 * Compute n * 2$(SUP exp) 1385 1385 * References: frexp 1386 1386 */ 1387 1387 1388 pure nothrow real ldexp(real n, int exp); /* intrinsic */1388 @safe pure nothrow real ldexp(real n, int exp); /* intrinsic */ 1389 1389 1390 1390 unittest { 1391 1391 assert(ldexp(1, -16384) == 0x1p-16384L); 1392 1392 assert(ldexp(1, -16382) == 0x1p-16382L); 1393 1393 int x; 1394 1394 real n = frexp(0x1p-16384L, x); 1395 1395 assert(n==0.5L); 1396 1396 assert(x==-16383); 1397 1397 assert(ldexp(n, x)==0x1p-16384L); 1398 1398 … … 1572 1572 1573 1573 /******************************* 1574 1574 * Returns |x| 1575 1575 * 1576 1576 * $(TABLE_SV 1577 1577 * $(TR $(TH x) $(TH fabs(x))) 1578 1578 * $(TR $(TD $(PLUSMN)0.0) $(TD +0.0) ) 1579 1579 * $(TR $(TD $(PLUSMN)$(INFIN)) $(TD +$(INFIN)) ) 1580 1580 * ) 1581 1581 */ 1582 pure nothrow real fabs(real x); /* intrinsic */1582 @safe pure nothrow real fabs(real x); /* intrinsic */ 1583 1583 1584 1584 1585 1585 /*********************************************************************** 1586 1586 * Calculates the length of the 1587 1587 * hypotenuse of a right-angled triangle with sides of length x and y. 1588 1588 * The hypotenuse is the value of the square root of 1589 1589 * the sums of the squares of x and y: 1590 1590 * 1591 1591 * sqrt($(POW x, 2) + $(POW y, 2)) 1592 1592 * … … 1771 1771 real nearbyint(real x) { return core.stdc.math.nearbyintl(x); } 1772 1772 1773 1773 /********************************** 1774 1774 * Rounds x to the nearest integer value, using the current rounding 1775 1775 * mode. 1776 1776 * If the return value is not equal to x, the FE_INEXACT 1777 1777 * exception is raised. 1778 1778 * $(B nearbyint) performs 1779 1779 * the same operation, but does not set the FE_INEXACT exception. 1780 1780 */ 1781 pure nothrow real rint(real x); /* intrinsic */1781 @safe pure nothrow real rint(real x); /* intrinsic */ 1782 1782 1783 1783 /*************************************** 1784 1784 * Rounds x to the nearest integer value, using the current rounding 1785 1785 * mode. 1786 1786 * 1787 1787 * This is generally the fastest method to convert a floating-point number 1788 1788 * to an integer. Note that the results from this function 1789 1789 * depend on the rounding mode, if the fractional part of x is exactly 0.5. 1790 1790 * If using the default rounding mode (ties round to even integers) 1791 1791 * lrint(4.5) == 4, lrint(5.5)==6. … … 3628 3628 alias isFinite isfinite; 3629 3629 alias isNormal isnormal; 3630 3630 alias isSubnormal issubnormal; 3631 3631 alias isInfinity isinf; 3632 3632 3633 3633 /* ********************************** 3634 3634 * Building block functions, they 3635 3635 * translate to a single x87 instruction. 3636 3636 */ 3637 3637 3638 pure nothrow real yl2x(real x, real y); // y * log2(x)3639 pure nothrow real yl2xp1(real x, real y); // y * log2(x + 1)3638 @safe pure nothrow real yl2x(real x, real y); // y * log2(x) 3639 @safe pure nothrow real yl2xp1(real x, real y); // y * log2(x + 1) 3640 3640 3641 3641 unittest 3642 3642 { 3643 3643 version (INLINE_YL2X) 3644 3644 { 3645 3645 assert(yl2x(1024, 1) == 10); 3646 3646 assert(yl2xp1(1023, 1) == 10); 3647 3647 } 3648 3648 }
