| 1 |
@safe unittest |
| 2 |
{ |
| 3 |
import std.math.trigonometry; |
| 4 |
|
| 5 |
import std.math.operations : isClose; |
| 6 |
|
| 7 |
assert(cos(0.0) == 1.0); |
| 8 |
assert(cos(1.0).isClose(0.5403023059)); |
| 9 |
assert(cos(3.0).isClose(-0.9899924966)); |
| 10 |
} |
| 11 |
|
| 12 |
@safe unittest |
| 13 |
{ |
| 14 |
import std.math.trigonometry; |
| 15 |
|
| 16 |
import std.math.constants : PI; |
| 17 |
import std.stdio : writefln; |
| 18 |
|
| 19 |
void someFunc() |
| 20 |
{ |
| 21 |
real x = 30.0; |
| 22 |
auto result = sin(x * (PI / 180)); // convert degrees to radians |
| 23 |
writefln("The sine of %s degrees is %s", x, result); |
| 24 |
} |
| 25 |
} |
| 26 |
|
| 27 |
@safe unittest |
| 28 |
{ |
| 29 |
import std.math.trigonometry; |
| 30 |
|
| 31 |
import std.math.operations : isClose; |
| 32 |
import std.math.traits : isIdentical; |
| 33 |
import std.math.constants : PI; |
| 34 |
import std.math.algebraic : sqrt; |
| 35 |
|
| 36 |
assert(isIdentical(tan(0.0), 0.0)); |
| 37 |
assert(tan(PI).isClose(0, 0.0, 1e-10)); |
| 38 |
assert(tan(PI / 3).isClose(sqrt(3.0))); |
| 39 |
} |
| 40 |
|
| 41 |
@safe unittest |
| 42 |
{ |
| 43 |
import std.math.trigonometry; |
| 44 |
|
| 45 |
import std.math.operations : isClose; |
| 46 |
import std.math.traits : isNaN; |
| 47 |
import std.math.constants : PI; |
| 48 |
|
| 49 |
assert(acos(0.0).isClose(1.570796327)); |
| 50 |
assert(acos(0.5).isClose(PI / 3)); |
| 51 |
assert(acos(PI).isNaN); |
| 52 |
} |
| 53 |
|
| 54 |
@safe unittest |
| 55 |
{ |
| 56 |
import std.math.trigonometry; |
| 57 |
|
| 58 |
import std.math.operations : isClose; |
| 59 |
import std.math.traits : isIdentical, isNaN; |
| 60 |
import std.math.constants : PI; |
| 61 |
|
| 62 |
assert(isIdentical(asin(0.0), 0.0)); |
| 63 |
assert(asin(0.5).isClose(PI / 6)); |
| 64 |
assert(asin(PI).isNaN); |
| 65 |
} |
| 66 |
|
| 67 |
@safe unittest |
| 68 |
{ |
| 69 |
import std.math.trigonometry; |
| 70 |
|
| 71 |
import std.math.operations : isClose; |
| 72 |
import std.math.traits : isIdentical; |
| 73 |
import std.math.constants : PI; |
| 74 |
import std.math.algebraic : sqrt; |
| 75 |
|
| 76 |
assert(isIdentical(atan(0.0), 0.0)); |
| 77 |
assert(atan(sqrt(3.0)).isClose(PI / 3)); |
| 78 |
} |
| 79 |
|
| 80 |
@safe unittest |
| 81 |
{ |
| 82 |
import std.math.trigonometry; |
| 83 |
|
| 84 |
import std.math.operations : isClose; |
| 85 |
import std.math.constants : PI; |
| 86 |
import std.math.algebraic : sqrt; |
| 87 |
|
| 88 |
assert(atan2(1.0, sqrt(3.0)).isClose(PI / 6)); |
| 89 |
} |
| 90 |
|
| 91 |
@safe unittest |
| 92 |
{ |
| 93 |
import std.math.trigonometry; |
| 94 |
|
| 95 |
import std.math.constants : E; |
| 96 |
import std.math.operations : isClose; |
| 97 |
|
| 98 |
assert(cosh(0.0) == 1.0); |
| 99 |
assert(cosh(1.0).isClose((E + 1.0 / E) / 2)); |
| 100 |
} |
| 101 |
|
| 102 |
@safe unittest |
| 103 |
{ |
| 104 |
import std.math.trigonometry; |
| 105 |
|
| 106 |
import std.math.constants : E; |
| 107 |
import std.math.operations : isClose; |
| 108 |
import std.math.traits : isIdentical; |
| 109 |
|
| 110 |
enum sinh1 = (E - 1.0 / E) / 2; |
| 111 |
import std.meta : AliasSeq; |
| 112 |
static foreach (F; AliasSeq!(float, double, real)) |
| 113 |
{ |
| 114 |
assert(isIdentical(sinh(F(0.0)), F(0.0))); |
| 115 |
assert(sinh(F(1.0)).isClose(F(sinh1))); |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
@safe unittest |
| 120 |
{ |
| 121 |
import std.math.trigonometry; |
| 122 |
|
| 123 |
import std.math.operations : isClose; |
| 124 |
import std.math.traits : isIdentical; |
| 125 |
|
| 126 |
assert(isIdentical(tanh(0.0), 0.0)); |
| 127 |
assert(tanh(1.0).isClose(sinh(1.0) / cosh(1.0))); |
| 128 |
} |
| 129 |
|
| 130 |
@safe @nogc nothrow unittest |
| 131 |
{ |
| 132 |
import std.math.trigonometry; |
| 133 |
|
| 134 |
import std.math.traits : isIdentical, isNaN; |
| 135 |
|
| 136 |
assert(isNaN(acosh(0.9))); |
| 137 |
assert(isNaN(acosh(real.nan))); |
| 138 |
assert(isIdentical(acosh(1.0), 0.0)); |
| 139 |
assert(acosh(real.infinity) == real.infinity); |
| 140 |
assert(isNaN(acosh(0.5))); |
| 141 |
} |
| 142 |
|
| 143 |
@safe @nogc nothrow unittest |
| 144 |
{ |
| 145 |
import std.math.trigonometry; |
| 146 |
|
| 147 |
import std.math.traits : isIdentical, isNaN; |
| 148 |
|
| 149 |
assert(isIdentical(asinh(0.0), 0.0)); |
| 150 |
assert(isIdentical(asinh(-0.0), -0.0)); |
| 151 |
assert(asinh(real.infinity) == real.infinity); |
| 152 |
assert(asinh(-real.infinity) == -real.infinity); |
| 153 |
assert(isNaN(asinh(real.nan))); |
| 154 |
} |
| 155 |
|
| 156 |
@safe @nogc nothrow unittest |
| 157 |
{ |
| 158 |
import std.math.trigonometry; |
| 159 |
|
| 160 |
import std.math.traits : isIdentical, isNaN; |
| 161 |
|
| 162 |
assert(isIdentical(atanh(0.0), 0.0)); |
| 163 |
assert(isIdentical(atanh(-0.0),-0.0)); |
| 164 |
assert(isNaN(atanh(real.nan))); |
| 165 |
assert(isNaN(atanh(-real.infinity))); |
| 166 |
assert(atanh(0.0) == 0); |
| 167 |
} |
| 168 |
|