std.math log and family

Joe jma at freedomcircle.com
Wed Oct 31 00:48:03 UTC 2018


I've discovered that the 'log' function as well several similar 
functions are only defined as taking a real argument and 
returning a real, unlike most other std.math functions, which 
have triple definitions (also supporting double and float types). 
This created a problem when I tried compiling code like the 
following:

---
import std.math;

alias double function(double) Funcptr;

Funcptr sinptr = &sin;
Funcptr tanptr = &tan;
Funcptr logptr = &log;
---

dmd (and ldc2) report

test.d(7): Error: cannot implicitly convert expression & log of 
type real function(real x) pure nothrow @nogc @safe to double 
function(double)

[ldc2 also reports
test.d(6): Error: cannot implicitly convert expression & tan of 
type real function(real x) pure nothrow @nogc @trusted to double 
function(double)
but apparently this is an LDC-only problem]

I'd like to know if the lack of double/float versions of 'log', 
'log10', etc. are intentional, i.e., there's some rationale 
behind it, or an oversight.  I'd also like to know the 
proper/best way to deal with the error, considering the real code 
embeds the 'Funcptr's in an array of structs.  Is it better to 
write a function 'mylog' that internally casts the argument and 
the return value, or is there some safe way to cast away the 
'log' function at the point of initializing the array?

Also, is it preferable to post the first question 
(intentional/oversight) in the Phobos forum? And what is the 
preferred way of reporting the LDC problem, via GitHub?


More information about the Digitalmars-d-learn mailing list