I need this (float != double)

This is a discussion on I need this (float != double) within the C Programming forums, part of the General Programming Boards category; Code: #include<stdio.h> void main(void) { float f=0.7; if(f==.7) printf("c"); else printf("d"); } Here I am getting the output as :d ...

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    10

    I need this (float != double)

    Code:
    #include<stdio.h>
    void main(void)
    {
    	float  f=0.7;
    	if(f==.7)
    	   printf("c");
    	else
    	   printf("d");
    
    }
    Here I am getting the output as :d instead of c ......
    If anybody have idea of this , kindly explain me why it becomes like that.??

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    	if(f==.7)
    you are comparing the float f with the double literal .7.

    Code:
    	
        float f = 0.7f;
        if(f==.7f)
    should give you the result you expect.
    You should read about floatingpoint numbers and why it is never a good idea to compare them. If you have to you need to prepare for rounding errors.
    Kurt

    edit: I very much agree with qazah's reply
    Last edited by ZuK; 08-19-2006 at 04:18 AM.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I was going to answer, I had it all typed up. Then I decided that it wasn't actually Urgent after all!


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094

  5. #5
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    As Zuk mentioned, you might want to read up on floating point numbers so you can understand some of the peculiarities of comparing them with each other. Being the nice guy that I am (I am trying to steal the "Mr. Nice" award from Quzah this year.. ^__^) here are some links for you. Lengthy reads to be sure, but the info is by no means useless.

    Sun Microsystem's Numerical Computation Guide. (In particular, be sure to read, 'What Every Computer Scientist Should Know About Floating-Point Arithmetic.'

    The C-FAQ on floating point numbers.

    Steve Hollasch's IEEE Standard 754 Floating Point Numbers

    Comparing floating point numbers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Copying 2-d arrays
    By Holtzy in forum C++ Programming
    Replies: 11
    Last Post: 03-14-2008, 03:44 PM
  2. Need help with program
    By HAssan in forum C Programming
    Replies: 8
    Last Post: 06-10-2007, 08:05 PM
  3. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  4. Im stuck....
    By dAzed in forum C++ Programming
    Replies: 8
    Last Post: 10-11-2004, 04:50 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM

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