program problem...

This is a discussion on program problem... within the C Programming forums, part of the General Programming Boards category; I have this small C program and I am unable to locate the mistake. Can someone help me locate it? ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    29

    program problem...

    I have this small C program and I am unable to locate the mistake. Can someone help me locate it?
    Code:
    #include<stdio.h>
     int main()
          {
               float a=0.0,b=0.0;
               float a1=0.0;
               a1=1.05;
               b=0.05;
               printf("\na1=%f\tb=%f\n",a1,b);
               a=a1-1.0;
               printf("\na=%f\tb=%f\n",a,b);
               if(a<b)
                  printf("\nHELLO:b is greater than a\n");
               else
                  printf("\ncondition is not satisfied");
               printf("\n");
           }
    The program is supposed to print "Condition is not satisfired", but instead the program gives "HELLO:b is greater than a"

    Hope someone can help me locate the error.

    Thanks in advance.
    Last edited by ashesh; 06-10-2005 at 04:59 AM. Reason: indenting...
    C.B.Ashesh,
    Hyderabad, India

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You mean other than the fact that you forgot how to indent, and that you've neglected to pay attention to your compiler's warnings?


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

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    29
    Oops.. sorry abt that...was in a hurry when I posted that. Will take care next time and indent.

    There were no warning given by the compiler. I executed the program on Fedora 1 gcc compiler and there were no warnings or error messages.
    C.B.Ashesh,
    Hyderabad, India

  4. #4
    FOX
    Join Date
    May 2005
    Posts
    188
    Just a recommendation...
    Code:
    alias gcc="gcc -Wall -Wextra -ansi -pedantic"

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    > Hope someone can help me locate the error.
    Yeah, your assumption that floating point numbers are totally accurate is the problem.

    Mathematically, you expect a and b to be the same, but in floating point calculations, there is always a small degree of error.
    So what you think might be a == b could result in any of these conditions being true
    a < b
    a == b
    a > b

    http://www.physics.ohio-state.edu/~d...point_math.pdf
    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.

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    29
    Thanks Salem. It turns out that the assumption was only the problem. When I printed up to 20 digits after the decimal point, I could see the difference in the two numbers.
    C.B.Ashesh,
    Hyderabad, India

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multi Thread Program Problem
    By ZNez in forum C Programming
    Replies: 1
    Last Post: 01-03-2009, 10:10 AM
  2. Program Termination Problem
    By dacbo in forum C Programming
    Replies: 3
    Last Post: 01-23-2006, 01:34 AM
  3. Inheritance and Dynamic Memory Program Problem
    By goron350 in forum C++ Programming
    Replies: 1
    Last Post: 07-02-2005, 02:38 PM
  4. Replies: 20
    Last Post: 06-12-2005, 11:53 PM

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