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. Visual Studio
  4. cycle

cycle

Scheduled Pinned Locked Moved Visual Studio
4 Posts 4 Posters 2 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.
  • G Offline
    G Offline
    gulnazt
    wrote on last edited by
    #1

    why in the cycle for (double i=-1; i<=1; i=i+0.1) the 0 is equal -1.38778e-016

    for(double i=-1; i<=1;i=i+0.1)
    

    cout<<i<<"\n";

    result:

    -1
    -0.9
    -0.8
    -0.7
    -0.6
    -0.5
    -0.4
    -0.3
    -0.2
    -0.1
    -1.38778e-016
    0.1
    0.2
    0.3
    0.4
    0.5
    0.6
    0.7
    0.8
    0.9
    1

    Richard Andrew x64R B 2 Replies Last reply
    0
    • G gulnazt

      why in the cycle for (double i=-1; i<=1; i=i+0.1) the 0 is equal -1.38778e-016

      for(double i=-1; i<=1;i=i+0.1)
      

      cout<<i<<"\n";

      result:

      -1
      -0.9
      -0.8
      -0.7
      -0.6
      -0.5
      -0.4
      -0.3
      -0.2
      -0.1
      -1.38778e-016
      0.1
      0.2
      0.3
      0.4
      0.5
      0.6
      0.7
      0.8
      0.9
      1

      Richard Andrew x64R Offline
      Richard Andrew x64R Offline
      Richard Andrew x64
      wrote on last edited by
      #2

      That is because floating point numbers are not exact. I don't have a good link handy, but if you google "floating point numbers" you'll find a number of articles on the subject. EDIT: Here's an article that talks about the issue: The trouble with rounding floating point numbers • The Register[^]

      The difficult we do right away... ...the impossible takes slightly longer.

      Z 1 Reply Last reply
      0
      • Richard Andrew x64R Richard Andrew x64

        That is because floating point numbers are not exact. I don't have a good link handy, but if you google "floating point numbers" you'll find a number of articles on the subject. EDIT: Here's an article that talks about the issue: The trouble with rounding floating point numbers • The Register[^]

        The difficult we do right away... ...the impossible takes slightly longer.

        Z Offline
        Z Offline
        ZurdoDev
        wrote on last edited by
        #3

        +5

        There are only 10 types of people in the world, those who understand binary and those who don't.

        1 Reply Last reply
        0
        • G gulnazt

          why in the cycle for (double i=-1; i<=1; i=i+0.1) the 0 is equal -1.38778e-016

          for(double i=-1; i<=1;i=i+0.1)
          

          cout<<i<<"\n";

          result:

          -1
          -0.9
          -0.8
          -0.7
          -0.6
          -0.5
          -0.4
          -0.3
          -0.2
          -0.1
          -1.38778e-016
          0.1
          0.2
          0.3
          0.4
          0.5
          0.6
          0.7
          0.8
          0.9
          1

          B Offline
          B Offline
          BenScharbach
          wrote on last edited by
          #4

          In C#, you can use this formula to account for the floats issues with zero; (Math.Abs(priorDegreePosition - degreePosition) < double.Epsilon) As the other writers mentioned, this number of -1.38778e-016, is pretty close to zero. If you were to move the decimal over the 16 place-holders, it would round to zero. Therefore, it sounds more of a formatting issue you are dealing with in your C++ example. In my statement above, C# has the smallest value for doubles/floats in the Epsilon.

          Ben Scharbach Temporalwars.Com YouTube:Ben Scharbach

          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