how can 0.00665 not equal 0.00665??!!

This is a discussion on how can 0.00665 not equal 0.00665??!! within the C++ Programming forums, part of the General Programming Boards category; OK, I must be missing something. How can 0.00665 not equal 0.00665? I hard code mytemp = 0.00665; then i ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    20

    Question how can 0.00665 not equal 0.00665??!!

    OK, I must be missing something. How can 0.00665 not equal 0.00665?

    I hard code mytemp = 0.00665;
    then i read in with cin >> searchValue to be 0.00665

    then I test to see if they are equal ( both are double ) and
    low and behold the logic says that mytemp is < searchValue!!!!

    Even in debug both numbers are identical yet they fail as equal in the logic test:
    if ( mytemp == searchValue)...

    I will attach the .cpp file.

    If I hard code both values into the code then they are equal.

    What in the world could be causing this? I have spent all day looking at this and I am at a loss.

    Thanks,
    Susan
    Attached Files Attached Files

  2. #2
    Seņor Member
    Join Date
    Jan 2002
    Posts
    561
    When I run it, the program says that they are equal. I'm using Dev C++ 4. Must be something wrong with your computer. Try restarting it, and if that doesn't work, try kicking it really hard. That always does the trick.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    20
    I was afraid of just the answer you gave! I rebooted and still had the same error. Does this mean that my compiler is corrupted in some way and I have to reinstall my compiler?
    Susan

  4. #4
    Seņor Member
    Join Date
    Jan 2002
    Posts
    561
    Hmm, I don't know what to do except to beat the crap out of your computer. Nice, firm kicks to the computer usualy do the trick for me!

  5. #5
    Registered User Invincible's Avatar
    Join Date
    Feb 2002
    Posts
    210
    Ok ... don't trash your comp just yet. I recompiled this program in visual C++ and had the same problem.

    After much frustration I determined the problem.

    Keep in mind i'm a noobie, but after ripping the program apart and writing one exactly like it of my own, I could find no difference between your program and mine. Except mine ran correctly.

    Then I noticed the one difference. I was not using a namespace in my program.

    So I removed the namespace and replaced <iostream> with <iostream.h>, recompiled your program and it ran just fine.

    I don't know much about how namespaces work, so eh...

    But I hope this helps.
    "The mind, like a parachute, only functions when open."

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    20
    interesting. don't quite know what to make of your observations. i'll make the similar changes and see what develops.

    Well, i get the same as you, it works.

    guess we need some name space specialist to explain what is going on.

    Much thanks for giving me some of my sanity back!

    susan
    Last edited by Susan; 02-10-2002 at 01:06 AM.

  7. #7
    Registered User Invincible's Avatar
    Join Date
    Feb 2002
    Posts
    210

    Talking

    And thanks for sharing your insanity

    I actually learned the while loop on the fly.

    Yes it would be nice to have namespaces explained in detail. But my best guess is that the namespace treated searchValue as 2 seperate instances of the same variable name.

    Just a guess.
    Last edited by Invincible; 02-10-2002 at 01:15 AM.
    "The mind, like a parachute, only functions when open."

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    > OK, I must be missing something. How can 0.00665 not equal 0.00665?
    You cannot compare floats for equality (EVER)

    floats are approximations to actual numbers, so whilst you might see only 0.00665, what the machine is actually trying to compare is 0.00665500001 and 0.00665500002
    This is why they appear to be different.

    You can do two things
    1. multiply both floats by (in this example) 10000 and assigning the results to integer variables, so you end up with comparing 665 with 665 (which will always work)

    2. subtract the two floats, and check that the difference is small, like so
    if ( fabs(a-b) < 0.00001 ) {
    &nbsp;&nbsp;&nbsp;&nbsp;// close enough
    }

  9. #9
    Registered User Invincible's Avatar
    Join Date
    Feb 2002
    Posts
    210
    er...

    that's interesting math

    are floats and doubles the same?

    does that really apply to this situation, neither of the values were a result of any computation

    one was hard coded and the other was user input, so i'm assuming the cpu will take them at face value

    why did it work when the namespace was removed?
    "The mind, like a parachute, only functions when open."

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    20
    well, i did that, sort of. my mistake perhaps was that I returned the value back to float after I rounded to the precision I wanted.

    I have another post by susan that is a few below this one and the topic is on rounding floating point numbers.

    my floating points, while i view them in debug, both *look* like
    0.006650000000
    both are exactly the same.
    yet when they are logically compared something is amiss.

    // interpolating a sorted array
    tryHere = lb + ( ( searchVal - dvlb ) / ( dvub - dvlb ) ) * ( ub - lb );

    // my attempt to round
    temp = ( array [ tryHere ] * dmill );
    iarrayRounded = int ( floor ( temp + 0.50 ))
    arrayRounded = iarrayRounded / 100000.0; // back to float

    I will try and convert to int and compare that way.

    Tried it with integers and of course it works just fine. Feel dumb but at least i can go to bed and get some rest.

    My family thanks you, as do I,
    susan


    susan
    Last edited by Susan; 02-10-2002 at 01:38 AM.

  11. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    > are floats and doubles the same?
    doubles have many more bits
    Look in float.h

    > one was hard coded and the other was user input
    User input involves computation - lots of multiply by 10.0 and add another digit kinda stuff

    > why did it work when the namespace was removed
    Pass

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking if strings are equal
    By jcafaro10 in forum C++ Programming
    Replies: 4
    Last Post: 01-24-2009, 10:53 AM
  2. setting equal to NULL
    By jsrig88 in forum C++ Programming
    Replies: 6
    Last Post: 02-28-2008, 03:25 PM
  3. Equal compare
    By George2 in forum C Programming
    Replies: 1
    Last Post: 11-10-2007, 04:26 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. set string array equal variable
    By WaterNut in forum C++ Programming
    Replies: 3
    Last Post: 06-29-2004, 05:02 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21