QString method number does not remove sign when rounding double value
-
When you use QString::number(double n, char format, int precision) with 'f' formats - method needs to round value after precision number. For example:
double value = 0.47 QString valueStr = QString::number(value, 'f', 1); // valueStr = 0.45
But when you work with value, that rounded into zero, this method 'forget' about sign. It is important, because we can display this string value, it is not just math variable, where +0 = -0. For example:
double value = -0.02 QString valueStr = QString::number(value, 'f', 1); // valueStr = -0.0
Is it bug or I do not understand something?
-
I guess it's debatable. I can see how it might not be desirable in some cases.
To me this is a feature. The value is clearly negative, so even though you cut down precision it's nice that this is still reflected. Besides, type double implements IEEE standard which has both positive and negative zeros. -
Hi @zonman,
I believe it's expected behaviour. Maybe have a quick read of https://en.wikipedia.org/wiki/Signed_zero
The IEEE 754 standard for floating-point arithmetic (presently used by most computers and programming languages that support floating-point numbers) requires both +0 and −0.
...
Negatively signed zero echoes the mathematical analysis concept of approaching 0 from below as a one-sided limit, which may be denoted by x → 0−, x → 0−, or x → ↑0.
...
the inclusion of signed zero in IEEE 754 makes it much easier to achieve numerical accuracy in some critical problems,[1] in particular when computing with complex elementary functions.[2]Pretty interesting topic, I reckon :)
Cheers.