Thread: help needed in floating point

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    2

    help needed in floating point

    I have 2 problems in C regarding floating point, please help :

    Q1
    Output of the following code- C.
    Code:
    main()
    {
      float a=0.7;
      if(a<0.7)
       printf("C");
      else 
       printf("C++);
    }
    But if we change the if condition from a<0.7 to a<0.7f we get output- C++.

    Q2
    The following code gives runtime error- Floating point formats not linked. When does this error occur in a program.
    Code:
    main()
    {
       struct emp
       {
       char name[20];
       float sal;
       };
       struct emp e[10];
       int i;
       for(i=0;i<=9;i++)
       scanf("%s %f",e[i]name,&e[i].sal);
    }

  2. #2
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    for your first question, i believe when you make it: a < 0.7f for the conditional, its taking the ascii value of f and combining it with 0.7. possibly, but you really have no reason to add the 'f' after 0.7
    Registered Linux User #380033. Be counted: http://counter.li.org

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    2
    No it is not like that if u write 0.7 then it will be taken as double and if u write 0.7f then it will be taken as a float value.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by akshayabc
    Q2
    The following code gives runtime error- Floating point formats not linked. When does this error occur in a program.
    Code:
    main()
    {
       struct emp
       {
       char name[20];
       float sal;
       };
       struct emp e[10];
       int i;
       for(i=0;i<=9;i++)
       scanf("%s %f",e[i]name,&e[i].sal);
    }
    http://cboard.cprogramming.com/showt...171#post322171
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    Floats/doubles are NOT capable of precisely representing many decimal values, hence your comparisons can fail. You need to test for a range.

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > But if we change the if condition from a<0.7 to a<0.7f we get output- C++.
    http://cboard.cprogramming.com/showt...light=goldberg

    > The following code gives runtime error- Floating point formats not linked
    This may mean you're using some old DOS compiler which in an effort to minimise the amount of space a program used (when people still cared about such things), makes a rather too optimistic assumption about the need for floating point support.
    Modern compilers do not suffer from this problem.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. For the numerical recipes in C types!
    By Smattacus in forum C Programming
    Replies: 5
    Last Post: 10-28-2008, 07:57 PM
  2. Help realy needed
    By ZohebN in forum C++ Programming
    Replies: 2
    Last Post: 04-08-2008, 09:37 AM
  3. checking for floating point number
    By ssharish2005 in forum C Programming
    Replies: 6
    Last Post: 10-18-2005, 08:14 PM
  4. Floating point numbers in a binary file
    By frenchfry164 in forum C++ Programming
    Replies: 6
    Last Post: 07-31-2003, 10:04 AM
  5. 2 questions about floating point and %
    By ams80 in forum C Programming
    Replies: 2
    Last Post: 08-14-2002, 10:55 AM