Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. c# stupid severe issue loop !

c# stupid severe issue loop !

Scheduled Pinned Locked Moved C#
csharphelpquestion
9 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    Isawyouoo
    wrote on last edited by
    #1

    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:

    OriginalGriffO L D 4 Replies Last reply
    0
    • I Isawyouoo

      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:

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Isawyouoo wrote:

      his is bullshit makes me angry

      No it is not bullshit, and your anger should be directed elsewhere. Please read What Every Computer Scientist Should Know About Floating-Point Arithmetic[^].

      1 Reply Last reply
      0
      • I Isawyouoo

        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:

        OriginalGriffO Offline
        OriginalGriffO Offline
        OriginalGriff
        wrote on last edited by
        #3

        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!

        "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

        1 Reply Last reply
        0
        • I Isawyouoo

          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:

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          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.

          I 1 Reply Last reply
          0
          • L Lost User

            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.

            I Offline
            I Offline
            Isawyouoo
            wrote on last edited by
            #5

            so what is the best way to make the loop generate those numbers correctly? just by i.ToString("0.00")

            OriginalGriffO L 2 Replies Last reply
            0
            • I Isawyouoo

              so what is the best way to make the loop generate those numbers correctly? just by i.ToString("0.00")

              OriginalGriffO Offline
              OriginalGriffO Offline
              OriginalGriff
              wrote on last edited by
              #6

              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!

              "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

              1 Reply Last reply
              0
              • I Isawyouoo

                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:

                D Offline
                D Offline
                Dave Kreskowiak
                wrote on last edited by
                #7

                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

                I 1 Reply Last reply
                0
                • I Isawyouoo

                  so what is the best way to make the loop generate those numbers correctly? just by i.ToString("0.00")

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  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).

                  1 Reply Last reply
                  0
                  • D Dave Kreskowiak

                    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

                    I Offline
                    I Offline
                    Isawyouoo
                    wrote on last edited by
                    #9

                    thank god you people, I've lost my mind one moment, I was gonna throw my laptop from the window :laugh:

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups