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 / C++ / MFC
  4. tolerance :o)

tolerance :o)

Scheduled Pinned Locked Moved C / C++ / MFC
question
4 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.
  • M Offline
    M Offline
    MemLeak
    wrote on last edited by
    #1

    Hi, I have a function bool equal(double val) { if(val == 0.0) return true; else return false; } if val = 0.0; function will return true if val = 0.0000000000000001; function will return false Is there a way to set a tolerance so that 0.0 == 0.000000000000001 -> true? Thanks Everything's beautiful if you look at it long enough...

    K B 2 Replies Last reply
    0
    • M MemLeak

      Hi, I have a function bool equal(double val) { if(val == 0.0) return true; else return false; } if val = 0.0; function will return true if val = 0.0000000000000001; function will return false Is there a way to set a tolerance so that 0.0 == 0.000000000000001 -> true? Thanks Everything's beautiful if you look at it long enough...

      K Offline
      K Offline
      kochhar
      wrote on last edited by
      #2

      Sure, you can try something like: bool equal(double val) { if(fabs(val) < 0.00000000001) return true; else return false; }

      1 Reply Last reply
      0
      • M MemLeak

        Hi, I have a function bool equal(double val) { if(val == 0.0) return true; else return false; } if val = 0.0; function will return true if val = 0.0000000000000001; function will return false Is there a way to set a tolerance so that 0.0 == 0.000000000000001 -> true? Thanks Everything's beautiful if you look at it long enough...

        B Offline
        B Offline
        Baris Kurtlutepe
        wrote on last edited by
        #3

        Hi, Look into the msdn article Why Floating-Point Numbers May Lose Precision there's a neat piece of code with an example equality comparison macro with support for tolerance.

        J 1 Reply Last reply
        0
        • B Baris Kurtlutepe

          Hi, Look into the msdn article Why Floating-Point Numbers May Lose Precision there's a neat piece of code with an example equality comparison macro with support for tolerance.

          J Offline
          J Offline
          John M Drescher
          wrote on last edited by
          #4

          Thanks for the link. It looks like a good and efficient solution. John

          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