cos(x)
1 import std.math; 2 // f(x) = cos(x), f'(x) = -sin(x) 3 auto x = dual(0.0, 1.0); 4 auto result = cos(x); 5 assert(approxEqual(result.re, 1.0)); // cos(0) = 1 6 assert(approxEqual(result.du, 0.0)); // -sin(0) = 0 7 8 x = dual(PI / 2.0, 1.0); 9 result = cos(x); 10 assert(approxEqual(result.re, 0.0)); // cos(π/2) = 0 11 assert(approxEqual(result.du, -1.0)); // -sin(π/2) = -1 12 13 // f(x) = cos(3x), f'(x) = -3sin(3x) 14 x = dual(5.0, 1.0); 15 result = cos(3 * x); 16 assert(approxEqual(result.re, std.math.cos(15.0))); 17 assert(approxEqual(result.du, -3.0 * std.math.sin(15.0)));
Cosinus function on dual numbers.