[Q] C Question

K

Ketan Parikh

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 : (e-mail address removed)

Have a nice time

Jay Hind.
 
A

Arthur J. O'Dwyer

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 : (e-mail address removed)

Maybe I will, someday.

-Arthur
 
T

Tim Hagan

Ketan said:
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.
 
T

Tim Hagan

Arthur J. O'Dwyer said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top