Hijacked: What's my number?

This is a discussion on Hijacked: What's my number? within the C Programming forums, part of the General Programming Boards category; ok.. anybody here to answer dis one... Code: Code: #include<stdio.h> void main() { float a = 0.7 if(0.7 > a ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    4

    Hijacked: What's my number?

    ok.. anybody here to answer dis one...

    Code:
    Code:
    #include<stdio.h>
    void main()
    {
    float a = 0.7
    
    if(0.7 > a )
      printf("HELLO");
    else
      printf("BEllo");
    }
    and d output is HELLO......!!!!!!!!!!?????????????




    Code:
    #include<stdio.h>
    void main()
    {
    float a = 0.8
    
    if(0.8 > a )
      printf("HELLO");
    else
      printf("BEllo");
    }
    d output is BEllo...........!!!!!!!!!?????????????

    any reason..?

    i executed dese two programs on TURBO C++4.5

  2. #2
    Registered User
    Join Date
    Jan 2010
    Posts
    104
    the output fr the 0.7 one should be "bello" too

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    and d output is HELLO......!!!!!!!!!!?????????????
    d output is BEllo...........!!!!!!!!!?????????????
    False. There is no output for either of them. Both programs dont compile because its not syntactically correct. So how could you possibly know the output?

    ok.. anybody here to answer dis one...
    i executed dese two programs on TURBO C++4.5
    If you want people to take you seriously, stop making up words.

    Anyway, try to cast the numbers to their respective type when doing the comparison and you should get the correct output, like
    Code:
    if ( (float) 0.42 < foo)
    As for the actual topic of the thread, as others have said, and I fully agree on and recommend learning, the most useful math in computer science and "programming" would be discrete math.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,335
    <<< Snipped from Knowing my math >>>
    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
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,335
    How To Ask Questions The Smart Way

    > if(0.7 > a )
    0.7 is a double constant
    a is a float
    BOTH are an approximation of the true value of 0.7

    In detail, you might find that the double is 0.699999999998, whereas the float is only 0.99999998.
    Neither are actually EQUAL to 0.7, the double just happens to be a little bit closer (and thus a very little bit bigger).

    Floating Point
    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
    Feb 2010
    Posts
    57

    Re: Hijacked: What's my number?

    Float is a single precision number. But double has double precision.
    And it has difference in storage format.
    float has 4 bytes but double has 8 bytes.

    Try to differentiate with the printf itself.
    Code:
    printf("Double (0.7):%0.12f\n",0.7);
    printf("Float  (0.7):%0.6f\n",0.7);
    printf("Double (0.8):%0.12f\n",0.8);
    printf("Double (0.8):%0.6f\n",0.8);
    And for me that I could see the difference in 0.17f that may be different in other OS. ( I am using Linux ).
    And It takes the approximate number to check that values in the condition.

    if you used constants while assigning the values it is good always, because it can solve many problems like this.

    Code:
    float a = 0.7f;
    or you can use this constant while checking the condition.

    Code:
    if(0.7f > a )
      printf("HELLO\n");
    else
      printf("BEllo\n");

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help getting program print out the digit in words
    By cosmiccomputing in forum C Programming
    Replies: 26
    Last Post: 04-24-2008, 08:28 AM
  2. Need help with this compiler error
    By Evangeline in forum C Programming
    Replies: 7
    Last Post: 04-05-2008, 09:27 AM
  3. Prime number program problem
    By Guti14 in forum C Programming
    Replies: 11
    Last Post: 08-06-2004, 04:25 AM
  4. parsing a number
    By juancardenas in forum C Programming
    Replies: 1
    Last Post: 02-19-2003, 12:10 PM
  5. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 04:00 PM

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