-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
num.toStringAsFixed() can return '-0.000' #42472
Comments
Is this a bug, @lrhn ? |
Doesn't seem like a bug. There is a difference between 0.0 and -0.0, and Whether it's desirable is a different question. We can't compare with JavaScript behavior because their double x = -0.0;
String s = String.format("%.3f", x);
System.out.println(s); // Prints "-0.000" or C# var x = -0.0;
var s = String.Format("{0:0.000}", x);
Console.WriteLine(s); // prints "0.000" so there isn't consistency. |
I just want to record the fact that although I can see where @lrhn is coming I do disagree with the conclusion and believe this is a bug and that it should be re-examined. I appreciate that in the implementation of dart-lang there is a difference between 0.00 and -0.00, however it is a universally agreed fact of all mathematics that 0.00 and -0.00 are identical. I don't think it's right that dart-langs opinion of numbers trumps all known mathematical understanding. I think @lrhn might be looking at this too much from the PoV of a dart-lang dev and missing the bigger picture - which is completely understandable because it happens to everyone, even the best devs. C# matches the behaviour I think is expected, Java doesn't and people want workarounds, and JavaScript chops off the trailing 0s, but never the less also drops the '-'. So as far as I can see, all three of these examples point to the correct behaviour being what I have suggested. I'm not sure why you both @lrhn and @kevmoo still came to the conclusion that this shouldn't be resolved. Writing this off as 'undesirable' and not a 'bug' is just playing with words, in my opinion. Unless there is a really good reason why this can't be fixed (i.e. major effort/rewrite/refactor required) I think it should be resolved. It's going to cause pain for many devs for years to come, and I can't think of a single use-case where it would be desirable. If I'm missing somthing (i.e. an important use-case where formatting a number as '-0.00' is deisable) then please do enlighten me - I'm completly open to changing my mind! I hope you can have another look at this. I appreciate your time 👍 |
It seems that a lot of the languages are preserving the sign when formatting minus zero, see the table: rust-lang/rfcs#1074 (comment) Dart is by no means unique here.
It's not really Dart specific - this is just how floating point numbers work. Note that floating point numbers don't behave like mathematical real numbers, despite being called |
Thanks for explaining this @mraleph. I think the issue is that I assumed toStringAsFixed() should provided a Going forward I think it would really nice if:
Do you have a view on this? Should I create new issues for these things? I've read the CONTRIBUTING.md but would still like some guidance before I do anything. Cheers |
The real question here is which use-cases It's not for round-tripping double values through It's not for rounding (by round-tripping through So, it's most likely for displaying, and in that case, the distinction between -0.0 and 0.0 is likely not important. The next question is then whether anyone would be broken by actually changing it. Most likely not, displaying a -0.0 is rare. |
That was my initial impression also - that toStringAsFixed() was intended and used for display purposes. So based on this new information it sounds like it would be reasonable and useful to remove the If we are worried about it being a breaking change then could we include it in a more major release? But I do agree with @lrhn that it will probably not be break anything of any significance. I'm sure it will break someone's wacky setup somewhere (i.e. some crazy debug workflow where they log numbers using Is there anything I can do to help further the issue? Cheers |
Based on final conclusion this issue should be reopened. |
I am not in favor of changing The phrase 'for display purposes' is underspecified, since it tells us nothing about the audience. For a technical audience (historically the audience for floating-point calculations) you probably want a negative sign when the value is negative but rounded to zero to fit the specified precision. Any change to A new method can be implemented as a static extension method. I wanted to say a better approach for 'display purposes' its to embrace the audience and provide output customized for the audience. |
Version
Dart VM version: 2.8.4 (stable) (Wed Jun 3 12:26:04 2020 +0200) on "macos_x64"
Description
Calling num.toStringAsFixed() can return "-0.0000". Surely we never want to format a string as minus 0?
Steps to reproduce
num.parse("-1.4921397450962104e-13").toStringAsFixed(4)
The text was updated successfully, but these errors were encountered: