c# stupid severe issue loop !
-
wtf is hapening ? for(float i=-5.0f ; i<5.0f ; i+=0.1f) Console.WriteLine(i+"\n"); // with while float r=-5.0f; while (r < 5.0f) { Console.WriteLine(r+"\n");; r += 0.1f; } //output at -4.5 //-4,9 //-4,8 //-4,7 //-4,6 //-4,5 //-4,400001 <--- this is bullshit makes me angry :mad:
-
wtf is hapening ? for(float i=-5.0f ; i<5.0f ; i+=0.1f) Console.WriteLine(i+"\n"); // with while float r=-5.0f; while (r < 5.0f) { Console.WriteLine(r+"\n");; r += 0.1f; } //output at -4.5 //-4,9 //-4,8 //-4,7 //-4,6 //-4,5 //-4,400001 <--- this is bullshit makes me angry :mad:
It's floating point data: it's stored in binary, not decimal, and 0.1 decimal is not a "precise" value in floating point format: 15. Floating Point Arithmetic: Issues and Limitations[^] If you format your output to two decimal places, it'll always look the same:
for (float i = -5.0f; i < 5.0f; i += 0.1f) { Console.WriteLine($"{i:0.00}"); }
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!
-
wtf is hapening ? for(float i=-5.0f ; i<5.0f ; i+=0.1f) Console.WriteLine(i+"\n"); // with while float r=-5.0f; while (r < 5.0f) { Console.WriteLine(r+"\n");; r += 0.1f; } //output at -4.5 //-4,9 //-4,8 //-4,7 //-4,6 //-4,5 //-4,400001 <--- this is bullshit makes me angry :mad:
-
wtf is hapening ? for(float i=-5.0f ; i<5.0f ; i+=0.1f) Console.WriteLine(i+"\n"); // with while float r=-5.0f; while (r < 5.0f) { Console.WriteLine(r+"\n");; r += 0.1f; } //output at -4.5 //-4,9 //-4,8 //-4,7 //-4,6 //-4,5 //-4,400001 <--- this is bullshit makes me angry :mad:
-
Isawyouoo wrote:
0.1f
The bullshit is right here. There is no float with the exact value of 0.1, so the nice-looking results are at best misleading.
-
so what is the best way to make the loop generate those numbers correctly? just by i.ToString("0.00")
That doesn't affect the actual number, just the string representation of it. Read the links Richard and I gave you.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!
-
wtf is hapening ? for(float i=-5.0f ; i<5.0f ; i+=0.1f) Console.WriteLine(i+"\n"); // with while float r=-5.0f; while (r < 5.0f) { Console.WriteLine(r+"\n");; r += 0.1f; } //output at -4.5 //-4,9 //-4,8 //-4,7 //-4,6 //-4,5 //-4,400001 <--- this is bullshit makes me angry :mad:
Did you even stop to think about the problem before you lost your mind? How many floating point numbers are there in any range of values? Infinite. How do you represent an infinite number of values in a finite number of bits? You don't, but to get the largest usable range possible, you have to make sacrifices and put up with limits on precision. ...and you just ran into one of those limits. Seriously, read those links and your expand your understanding instead of getting pissed over something you don't understand.
Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
Dave Kreskowiak -
so what is the best way to make the loop generate those numbers correctly? just by i.ToString("0.00")
Depends on what you want with them. If just printing them with fewer decimals is good enough, great. Or maybe it's an occasional for decimal floating point (in C# with the handy `decimal` type, though a bit slow), or decimal fixed point (store eg -49 instead of -4.9, and handle the details of the arithmetic manually).
-
Did you even stop to think about the problem before you lost your mind? How many floating point numbers are there in any range of values? Infinite. How do you represent an infinite number of values in a finite number of bits? You don't, but to get the largest usable range possible, you have to make sacrifices and put up with limits on precision. ...and you just ran into one of those limits. Seriously, read those links and your expand your understanding instead of getting pissed over something you don't understand.
Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
Dave Kreskowiak