Bug 23677 – log1p Documentation Doesn't Match Implementation
Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P4
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2023-02-07T16:23:52Z
Last change time
2023-03-20T13:26:56Z
Assigned to
No Owner
Creator
John Hall
Comments
Comment #0 by john.michael.hall — 2023-02-07T16:23:52Z
`std.math.exponential.log1p`'s documentation says that it is 1) more accurate for small values of x and 2) that it is a conversation of the CEPHES library.
1) It is only more accurate if a) INLINE_YL2X is true (which is likely only using the hard-ware implementation on DMD), and b) you are using either reals or it is ctfe. If neither of these are true, then the function is basically calling log(1+x), so there is no improvement in accuracy (so for floats and doubles without CTFE, it is not any different).
2) The CEPHES library has an implementation of log1p [1] using doubles that uses a Taylor expansion. The version here is not using it. The only thing it has similar is the check for a value near to 1 and special casing it and even then the special casing is different.
I would recommend bringing the documentation in line with the implementation by describing the accuracy of the function more accurately and removing the references to CEPHES for this one.
[1] https://github.com/jeremybarnes/cephes/blob/master/cprob/unity.c
Comment #1 by john.michael.hall — 2023-02-27T15:45:37Z
It looks like my previous comment about CEPHES is in error. What is actually happening is that the logImpl function has the CEPHES polynomials that are used for log1p (at least I know it's true for doubles, I don't see them in the CEPHES code for floats/long double, but I assume it is right).
In other words, the phobos log function seems to try to be more accurate for values close to 1 in a way that is different from other programming languages (which is why log1p is typically included as a separate function).
I'm still showing some funky results for log1p(1.0e-15) vs. log1p(1.0e-16) compared to when log1p did the calculation at real accuracy.
Comment #2 by john.michael.hall — 2023-02-27T22:50:00Z
I double-checked and log1p doesn't provide the same accuracy as the CEPHES implementation for doubles.
Comment #3 by john.michael.hall — 2023-03-20T13:26:56Z
By the time of the 2.102.2 release, the precision was properly increased for log1p.