std.format: format floats and doubles with %g / %G / %s#7804
std.format: format floats and doubles with %g / %G / %s#7804berni44 wants to merge 5 commits intodlang:masterfrom
Conversation
…with %g, %G and %s.
|
Thanks for your pull request and interest in making D better, @berni44! We are looking forward to reviewing it, and you should be hearing from a maintainer soon.
Please see CONTRIBUTING.md for more information. If you have addressed all reviews or aren't sure how to proceed, don't hesitate to ping us with a simple comment. Bugzilla references
Testing this PR locallyIf you don't have a local development environment setup, you can use Digger to test this PR: dub run digger -- build "master + phobos#7804" |
|
I expected somewhat, that these unittests would fail on some processors, probably those with Finding a way around this, is difficult. The selection between What happens in these situations is, that it prints the correct number, just the decision between scientific and "normal" representation is wrong (i. E. One solution, although I do not know if you could accept it, would be to replace the unittest with a little bit more relaxed one, like this: What do you think? Any ideas, how to get around this problem? PS: Maybe it's possible to check the result with a regex and call the correct function in case the result is wrong... I'll have to sleep on it. |
6fe5710 to
a9d5c90
Compare
|
To get around that problem, I replaced the calls to Unfortunately freebsd32 and win32 still fail on one unittest
{
double a = 0.00009999995;
double b = 0.00000000005;
writefln!"%.80f"(a);
writefln!"%.80f"(0.0001 - b);
assert(!(a < 0.0001 - b));
}The two Because fixing this bug is beyond this PR (*) I commented out the offending unittest. (*) It would mean to add support for soft floats, or at least the parts, that are needed for the calculations in PS: I think this is good to go now. |
|
@RazvanN7 Could you remove the WIP label please? I can't do so... |
There was a problem hiding this comment.
I haven't thoroughly reviewed this, but there are some things that I noticed at a cursory glance:
- please do not mix refactorings (even if they are small) with code additions. There are some places where
a-bis transformed toa - b. Try to keep the diff as small as possible. - is there any way that this PR can be broken in several smaller PRs? It is very hard to review it as is. For example, is it possible to incrementally add the modifications to the
printFloatEandprintFloatFfunctions and then make a PR that addsprintFloatG? That would make the reviewers job a lot easier. - Is there an alternative to using
if(is_g)everywhere? This is effectively adding a lot of branches and might negatively impact performance. Normally, the format string should be known at compile time and thereforestatic ifscould be used, but I don't know exactly if this applies in this situation.
You're right. I probably did this, because I was asked to do so in the %f PR. Anyway, I'll open a separate PR for that.
Yes, I think so. But I'll start with adding
I avoided them inside of loops which means, the impact should be little.
Well that's not always true. But we do not need the format string here, to make the decision static, we only need to know if we are processing %e/%f or %g, which we do know. I'm closing this, to keep it as a reference, and start new PRs. |
|
Thanks @berni44 ! I look forward to the split PRs. |
There where mainly three things I had to do to implement %g:
As usual I ran lots of external tests comparing the current output with the new one: All floats; all combinations of flags/rounding mode/selection of number,width and precision; several billion random samples. Aside from known bugs in the old implementation no differences where found. (I also ran the tests for %e and %f to make sure I didn't mess up anything there.)