Floating Values acting weird.

This is a discussion on Floating Values acting weird. within the C Programming forums, part of the General Programming Boards category; Code: #include<stdio.h> #include<conio.h> void main() { float x=0.1; if(x==0.1) printf("If"); else printf("Else"); getch(); } I have tried values 0.1,0.2,0.3,0.35,0.3000025. None ...

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    14

    Floating Values acting weird.

    Code:
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    float x=0.1;
    if(x==0.1)
    printf("If");
    else
    printf("Else");
    getch();
    }
    I have tried values 0.1,0.2,0.3,0.35,0.3000025. None seem to print the word "If", they print "Else".

    Could somebody help me with the reason for the same.

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    0.1 can be stored internally as 0.099999
    or 0.100001 for example

    you can never be sure...

    so if you really need to compare floats do something like
    Code:
    if(abs(x-y)<delta)
    where delta is small enough for considering to float numbers as the same
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Dec 2006
    Posts
    14
    Vart i do agree with the part of 0.1 being stored that way, but why is it so and even if it is so. If it stores 0.1 as 0.099999 or 0.1000001 then why does it not compare it also that way?

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,496
    The problems began with void main.

    Then this happened - http://c-faq.com/fp/strangefp.html
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    'cause in the statement x == 0.1
    x is a float and 0.1 is double
    so they can be stored as 0.999999 and 0.999999999999999
    then both converted to double will give different values...

    for example...
    as I said - you never know how the float value is stored internally
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,496
    > if(x==0.1)
    Because the left hand side is a float, and the right hand side is a double, and in the float to double conversion, you get a slightly different approx of 0.1, one which is different.

    if ( x == 0.1f )
    Might just work, but as stated, you should never compare floats for equality.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Dec 2006
    Posts
    14
    Code:
    else
    printf("%f,x");
    getch();
    }
    If i do this it returns the value of x as 0.100000.

    As per ur link salem, i understand that the processor's registers and the memory store values differently. So is there no way perfect way for equating floats?

    Vart do u mean to say that the rvalue of the if statement is by default a "double"?

  8. #8
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    a constant is by default a double
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,496
    > So is there no way perfect way for equating floats?
    Floats and doubles are always approximations.

    Find the David Goldberg, ``What Every Computer Scientist Should Know about Floating-Point Arithmetic''
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #10
    Registered User
    Join Date
    Dec 2006
    Posts
    14
    Thx guys u actually genuinely solved a query which i was banging my head for, I am from India and we have teachers with bookish knowledge so wouldnt expect an answer from them.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading values from a file
    By megastar in forum C Programming
    Replies: 4
    Last Post: 06-25-2007, 02:08 AM
  2. Replies: 1
    Last Post: 02-03-2005, 02:33 AM
  3. Floating point operand?
    By warny_maelstrom in forum C Programming
    Replies: 6
    Last Post: 03-04-2004, 11:26 AM
  4. char[] acting weird
    By Leeman_s in forum C++ Programming
    Replies: 3
    Last Post: 06-09-2003, 06:45 PM
  5. n00b needs help with api graphics acting very weird
    By CheeseWeaver in forum Windows Programming
    Replies: 2
    Last Post: 03-18-2003, 02:15 PM

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