← Back to index
|
Original Bugzilla link
Bug 16055 – double.stringof is not precise
Status
NEW
Severity
normal
Priority
P3
Component
dmd
Product
D
Version
D2
Platform
All
OS
Linux
Creation time
2016-05-22T02:34:26Z
Last change time
2024-12-13T18:47:55Z
Assigned to
No Owner
Creator
thomas.bockman
Moved to GitHub: dmd#19129 →
Comments
Comment #0
by thomas.bockman — 2016-05-22T02:34:26Z
The D front-end uses two fewer digits than needed for lossless conversion of floating-point values to strings: --- module example; import std.conv, std.format, std.math, std.meta, std.stdio; void main() { foreach (F; AliasSeq!(real, double, float)) { writeln(F.stringof); const allDig = cast(int)ceil(log(pow(2.0L, F.mant_dig - 1)) / log(10.0L) + 1); assert(allDig == (F.dig + 2)); writefln("\tallDig: %s", allDig); enum F mostDecDig = 1 + F.epsilon; // 16 for double: FAIL assert(to!F(format("%." ~ to!string(F.dig) ~ "g", mostDecDig)) != mostDecDig); // 17 for double: FAIL assert(to!F(format("%." ~ to!string(F.dig + 1) ~ "g", mostDecDig)) != mostDecDig); // 18 for double: PASS enum goodSpec = "%." ~ to!string(F.dig + 2) ~ "g"; static if (!is(F == real)) // to!real(string) is not precise! assert(to!F(format(goodSpec, mostDecDig)) == mostDecDig); // This is a very subtle issue; even the D frontend gets it wrong! assert(mostDecDig != mixin(mostDecDig.stringof)); writefln("\t" ~ goodSpec ~ " != %s", mostDecDig, mostDecDig.stringof); } } --- DPaste:
https://dpaste.dzfl.pl/1bd14e5c3f83#line-25
Comment #1
by robert.schadek — 2024-12-13T18:47:55Z
THIS ISSUE HAS BEEN MOVED TO GITHUB
https://github.com/dlang/dmd/issues/19129
DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB