|x|
1 import std.math: approxEqual; 2 // f(x) = |x|, f'(x) = signum(x) 3 auto x = dual(2.0, 1.0); 4 auto result = abs(x); 5 assert(approxEqual(result.re, 2.0)); // f(2) = 2 6 assert(approxEqual(result.du, 1.0)); // f'(2) = 1 7 8 x = dual(-2.0, 1.0); 9 result = abs(x); 10 assert(approxEqual(result.re, 2.0)); // f(-2) = 2 11 assert(approxEqual(result.du, -1.0)); // f'(-2) = -1 12 13 // because floating point numbers have -0 and +0 f'(x) is defined for x = 0 14 x = dual(0.0, 1.0); 15 result = abs(x); 16 assert(approxEqual(result.du, 1.0)); 17 x = dual(-0.0, 1.0); 18 result = abs(x); 19 assert(approxEqual(result.du, -1.0));
Abs function on dual numbers.