Open MateuszKlatecki opened 1 month ago
This issue might be the similar to https://github.com/nanoframework/Home/issues/1401 where the double is being cast to a float and that causes a loss of precision.
Duplicate with #1429 See that specific issue for the answer. I will close this issue.
@Ellerbach I must disagree that this is not a bug. While it is understood that floating-point arithmetic can introduce small errors due to the way numbers are represented in binary, the numbers 14 and 92 can be exactly represented in the IEEE 754 standard used for floating-point arithmetic. Therefore, when converted to a string, they should retain their exact integer representation without any introduced imprecision.
Even (as you wrote) less smart languages like Python can deal with the numbers 14 or 92 but nanoFramework not.
Another thing that makes something wrong is that this code
double d = 14d;
Console.WriteLine($"{d}");
will print 13.999999999999999
and the use of float, a less precise variable,
float f = 14f;
Console.WriteLine($"{f}");
already works properly
Additionally, I looked around the code and there is even a test that checks this https://github.com/nanoframework/CoreLibrary/blob/main/Tests/NFUnitTestArithmetic/UnitTestFormat.cs#L189 but the selected number in the test does not cause a problem. Replacing -1234 with -14 would cause this test to fail.
OK, reopening the issue. That say, it depends of the device precision. Double is always available, but if not natively supported, will automatically fall back to float.
Actually, that's the other way around: float is always available. Double is natively supported if the platform has DP hardware support (or emulated). Rational for this is explained in our docs here
Target name(s)
ALL
Firmware version
newest
Was working before? On which version?
I think it worked better a year ago
Device capabilities
No response
Description
Converting double to string does not work properly. For example, 14 is converted to 13.999999999999999, 92 to 91.999999999999993.
I am aware of how floating point numbers work and that some cannot be written without error, but the numbers 14 or 92 are not like that.
How to reproduce
Run this program:
Program prints:
Expected behaviour
Program should print:
Screenshots
No response
Aditional information
No response