[Q] C Question

Discussion in 'C Programming' started by Ketan Parikh, Aug 27, 2003.

  1. Ketan Parikh

    Ketan Parikh Guest

    Hey everybody,

    How are u?

    I am new this group.

    I have question regarding following C program.

    main()
    {
    float k=0.7;
    if (k < 0.7)
    printf("Hey");
    else
    printf("Hello");
    }

    the o/p : "Hey" (surprise me!!)

    I also work with other number like 0.1,0.2 ....0.9

    But only 0.7 and 0.9 gives o/p as "Hey"
    and all others gives o/p as "Hello"

    So what would be reason for this?

    You can mail me at :

    Have a nice time

    Jay Hind.
     
    Ketan Parikh, Aug 27, 2003
    #1
    1. Advertising

  2. On Wed, 27 Aug 2003, Ketan Parikh wrote:
    >
    > Hey everybody,
    >
    > How are u?


    Delicious! How are v?

    > I am new this group.
    >
    > I have question regarding following C program.
    >
    > main()
    > {
    > float k=0.7;
    > if (k < 0.7)
    > printf("Hey");
    > else
    > printf("Hello");
    > }
    >
    > the [output is] "Hey" (surprise me!!)
    >
    > I also work with other number like 0.1,0.2 ....0.9
    >
    > But only 0.7 and 0.9 gives o/p as "Hey"
    > and all others gives o/p as "Hello"
    >
    > So what would be reason for this?


    Perhaps you are mistaken. Post some correct code, and the
    output from that code, and somebody may tell you why the
    output is what it is. For example, you might write:

    % cat test.c

    #include <stdio.h>
    int main(void)
    {
    float k = 0.7;
    if (k < 0.7) printf("Unexpected!\n");
    return 0;
    }

    % gcc -o test test.c
    % ./test
    Unexpected!
    %

    and then we could tell you that "floats are not real numbers,
    they are approximations; go read a textbook or an online
    tutorial; use Google to find them."

    On the other hand, you could write

    % cat test.c

    #include <stdio.h>
    int main(void)
    {
    float k = 0.9;
    if (k < 0.7) printf("Unexpected!\n");
    return 0;
    }

    % gcc -o test test.c
    % ./test
    Unexpected!
    %

    and then we could flame you for posting bogus results,
    and start a whole sub-thread arguing about which C
    compiler you're using, and whether this could ever
    actually happen in real life, and so on.

    > You can mail me at :


    Maybe I will, someday.

    -Arthur
     
    Arthur J. O'Dwyer, Aug 27, 2003
    #2
    1. Advertising

  3. Ketan Parikh

    Tim Hagan Guest

    Ketan Parikh wrote:
    >
    > Hey everybody,


    Hey! You posted this question two days ago as "A Question about floating point
    represanation". The answer is still the same as it was then: Read the FAQ.

    --
    Tim Hagan
     
    Tim Hagan, Aug 27, 2003
    #3
  4. Ketan Parikh

    Tim Hagan Guest

    "Arthur J. O'Dwyer" wrote:
    >
    > On Wed, 27 Aug 2003, Ketan Parikh wrote:
    > >
    > > Hey everybody,
    > >
    > > How are u?

    >
    > Delicious! How are v?
    >
    > > I am new this group.
    > >
    > > I have question regarding following C program.
    > >
    > > main()
    > > {
    > > float k=0.7;
    > > if (k < 0.7)
    > > printf("Hey");
    > > else
    > > printf("Hello");
    > > }
    > >
    > > the [output is] "Hey" (surprise me!!)
    > >
    > > I also work with other number like 0.1,0.2 ....0.9
    > >
    > > But only 0.7 and 0.9 gives o/p as "Hey"
    > > and all others gives o/p as "Hello"
    > >
    > > So what would be reason for this?

    >
    > Perhaps you are mistaken. Post some correct code, and the
    > output from that code, and somebody may tell you why the
    > output is what it is. For example, you might write:
    >
    > % cat test.c
    >
    > #include <stdio.h>
    > int main(void)
    > {
    > float k = 0.7;
    > if (k < 0.7) printf("Unexpected!\n");
    > return 0;
    > }
    >
    > % gcc -o test test.c
    > % ./test
    > Unexpected!
    > %
    >
    > and then we could tell you that "floats are not real numbers,
    > they are approximations; go read a textbook or an online
    > tutorial; use Google to find them."
    >
    > On the other hand, you could write
    >
    > % cat test.c
    >
    > #include <stdio.h>
    > int main(void)
    > {
    > float k = 0.9;
    > if (k < 0.7) printf("Unexpected!\n");

    ^^^
    I don't think this is what he meant. That should be 0.9.

    > return 0;
    > }
    >
    > % gcc -o test test.c
    > % ./test
    > Unexpected!
    > %
    >
    > and then we could flame you for posting bogus results,


    Wait! Don't light that match yet ...

    > and start a whole sub-thread arguing about which C
    > compiler you're using, and whether this could ever
    > actually happen in real life, and so on.


    The OP appears to be using the same value for k and the number it is compared
    against. In other words, when he compares k=0.7 with 0.7 or k=0.9 with 0.9, he
    gets one result ("Hey"). With other values he gets the other result ("Hello").

    He needs to learn about floating point representations and comparisons in a
    binary system. The FAQ is a good place to start:

    http://www.eskimo.com/~scs/C-faq/top.html

    --
    Tim Hagan
     
    Tim Hagan, Aug 27, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sean
    Replies:
    1
    Views:
    605
    Cowboy \(Gregory A. Beamer\)
    Oct 20, 2003
  2. =?Utf-8?B?UnlhbiBTbWl0aA==?=

    Quick Question - Newby Question

    =?Utf-8?B?UnlhbiBTbWl0aA==?=, Feb 14, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    670
    Iain Norman
    Feb 16, 2005
  3. =?Utf-8?B?YW5kcmV3MDA3?=

    question row filter (more of sql query question)

    =?Utf-8?B?YW5kcmV3MDA3?=, Oct 5, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    955
    Scott Allen
    Oct 6, 2005
  4. Philip Meyer
    Replies:
    0
    Views:
    430
    Philip Meyer
    Nov 30, 2003
  5. Bit Byte
    Replies:
    1
    Views:
    865
    Teemu Keiski
    Jan 28, 2007
Loading...

Share This Page