Why is std.math slower than the C baseline?

jmh530 john.michael.hall at gmail.com
Thu Jun 4 14:39:45 UTC 2020


On Thursday, 4 June 2020 at 14:14:22 UTC, Andrei Alexandrescu 
wrote:
> D should just use the C functions when they offer better speed.
>
> https://www.reddit.com/r/programming/comments/gvuy59/a_look_at_chapel_d_and_julia_using_kernel_matrix/fsr4w5o/

Below is a typical example of a std.math implementation for a 
trig function. It casts everything to real to improve accuracy. 
This doesn't explain everything, but it's a general strategy in 
std.math to prefer accuracy over speed.

real cosh(real x) @safe pure nothrow @nogc
{
     //  cosh = (exp(x)+exp(-x))/2.
     // The naive implementation works correctly.
     const real y = exp(x);
     return (y + 1.0/y) * 0.5;
}

/// ditto
double cosh(double x) @safe pure nothrow @nogc { return 
cosh(cast(real) x); }

/// ditto
float cosh(float x) @safe pure nothrow @nogc  { return 
cosh(cast(real) x); }


More information about the Digitalmars-d mailing list