Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C data types problem

  1. Jan 1, 2016 #1
    Hello everyone!
    In C language,does two different variables with different data types but equal values equate each other?
    For example,are these two statements equal:
    Code (C):

    int x=3;
    float y=3.0;
     
    ?
    thanks in advance..
     
    Last edited by a moderator: Jan 1, 2016
  2. jcsd
  3. Jan 1, 2016 #2

    Samy_A

    User Avatar
    Science Advisor
    Homework Helper

    When you compare an int x with a float y (a tricky thing to do), as in
    Code (Text):
    if (x==y)
    the int is implicitly converted to float before the comparison is done.
     
  4. Jan 1, 2016 #3

    Vanadium 50

    User Avatar
    Staff Emeritus
    Science Advisor
    Education Advisor

    One should be very careful about testing two floats for strict equality. If a and b are equal, a/5 and b*0.2 might not be.
     
  5. Jan 1, 2016 #4

    Samy_A

    User Avatar
    Science Advisor
    Homework Helper

    Absolutely.
    Sometimes I had to test for a==b, and it only worked if I tested for abs(a-b) < some sufficiently small number. Don't know if there is a more elegant way to solve this.
     
  6. Jan 1, 2016 #5
    But will they be equal?
    Code (Text):

    #include<conio.h>
    #include<stdio.h>
    int x=3;
    float y=3.0;
    if (x==y)
    {
    printf("X is equal to Y");
    }
    else
    {
    printf("not equal");
    }
    getch();
    }
     
    What will this program return?
     
    Last edited by a moderator: Jan 2, 2016
  7. Jan 1, 2016 #6
    thanks
     
    Last edited by a moderator: Jan 1, 2016
  8. Jan 1, 2016 #7

    jim mcnamara

    User Avatar

    Staff: Mentor

    Equality of floating point datatypes (IEEE-754) is a pain in the butt. Most folks handle this incorrectly when they start to program.

    And yes, what you are talking about is called a 'promotion' - FWIW I do not know why - it looks more like a 'demotion' to me. Doubles and floats are promoted to integers, so as explained above, you run the risk of getting incorrect answers.

    You need to investigate the concept of an ULP and probably the idea behind the fenv.h "functions": fesetround and fegetround. Promoting implictly rounds values.
    This is a technical paper on the subject:
    https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html
    A less mathematically daunting article:
    http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm

    This whole subject is one the main reasons COBOL is still prominent in financial code - you lose precision with floating point. This gives auditors nightmares.

    Floating point specs were updated in IEEE-754-2008, but the changes not seem to be available on most commodity x86 implementations. Fujitsu SPARC 10 cpus have it. Some graphics cards( GPUS ) support it through add-ons like CUDA.
     
  9. Jan 1, 2016 #8

    Samy_A

    User Avatar
    Science Advisor
    Homework Helper

    My C++ compiler returns "X is equal to Y".
    I don't know whether this is compiler dependent.
     
  10. Jan 1, 2016 #9

    Mark44

    Staff: Mentor

    The values stored in x and y do not have equal values, based on the bit patterns that are stored in the two different memory locations.
    Jim, this is not true. Assuming you are talking about the comparison if (x == y), since the value in y is float, the the int value of x is promoted to a float as well, and then the comparison can proceed. doubles and floats are never "promoted" to ints, which would likely cause loss of precision, but in circumstances such as the one below, floating point values can be demoted to integral values.
    Code (C):
    int num = 3.6;
    In the declaration/definition above, the double value 3.6 is truncated to 3 to initialize the variable, num.
     
  11. Jan 1, 2016 #10

    jim mcnamara

    User Avatar

    Staff: Mentor

    @Mark44 Yup - you are correct - I have it backwards. Thanks for the correction.
     
  12. Jan 2, 2016 #11

    Vanadium 50

    User Avatar
    Staff Emeritus
    Science Advisor
    Education Advisor

    It's implementation dependent. My understanding is that if your implementation follows the IEEE-754 standards, it will return "equal". If you replace float y = 3.0 with float y = 3, it should always return equal: it is comparing "the float that 3 gets promoted to" with "the float that 3 gets promoted to", and such should always be true. That number may or may not be the same as the float 3.0. (IEEE says it is)

    Many profs will say "never test floats for strict equality". I think a better way to think about it is "testing floats for strict equality is almost never what you want to do". If you test two floats for equality, you are asking if the two numbers are close enough to each other as to have the same binary representation. So it's not equality, it's a range - and if it's a range, shouldn't the programmer decide what that range should be, and not leave it to the compiler?
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook