float f=0.7; f < 0.7 f<0.7f

R

rahulsinner

hi everyone,

main()
{
float f=0.7;
if ( f< 0.7)
printf("C");
else
printf("C++");
}


main()
{
float f=0.7;
if(f < 0.7f)
printf("C");
else
printf("C++");
}

The first code snippet prints C and the second one prints C++.Can
anyone tell me the reason for it
 
V

Vladimir S. Oka

(e-mail address removed) opined:
hi everyone,

main()
{
float f=0.7;
if ( f< 0.7)
printf("C");
else
printf("C++");
}


main()
{
float f=0.7;
if(f < 0.7f)
printf("C");
else
printf("C++");
}

The first code snippet prints C and the second one prints C++.Can
anyone tell me the reason for it

No they don't (or at least not necessarily).

a) you do not include <stdio.h> so `printf` is unknown
b) you do not terminate output with '\n'
c) it's `int main(void)`
d) you're missing `return 0;` (or whatever value)

However, your problem is in the fact that undecorated floating point
constants, like `0.7` are of type `double`, by appending 'f', you make
them `float`. Now, if sizes of your `float`s and `double`s are
different representation of 0.7 in them may differ. Adding:

printf("%20.10f %20.10f\n",0.7, 0.7f);

to your code (after fixing the rest) I get (gcc 4.0.3pre, SUSE 10.0):

0.7000000000 0.6999999881

The morale is: if you're going to use floating point numbers in your
applications, carefully study their limitations, and your particular
implementation. Also look into C Standard to see what are the minumum
requirements for a conforming implementation. This advice applies
equally to any programming language you choose.

HTH

--
if (argc > 1 && strcmp(argv[1], "-advice") == 0) {
printf("Don't Panic!\n");
exit(42);
}
(Arnold Robbins in the LJ of February '95, describing RCS)

<http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>
 
V

Vladimir S. Oka

Vladimir S. Oka opined:
(e-mail address removed) opined:


No they don't (or at least not necessarily).

a) you do not include <stdio.h> so `printf` is unknown
b) you do not terminate output with '\n'
c) it's `int main(void)`
d) you're missing `return 0;` (or whatever value)

However, your problem is in the fact that undecorated floating point
constants, like `0.7` are of type `double`, by appending 'f', you
make them `float`. Now, if sizes of your `float`s and `double`s are
different representation of 0.7 in them may differ. Adding:

printf("%20.10f %20.10f\n",0.7, 0.7f);

to your code (after fixing the rest) I get (gcc 4.0.3pre, SUSE 10.0):

0.7000000000 0.6999999881

The morale is: if you're going to use floating point numbers in your
applications, carefully study their limitations, and your particular
implementation. Also look into C Standard to see what are the minumum
requirements for a conforming implementation. This advice applies
equally to any programming language you choose.

Oh yes, do not use type `float` it is too small for any serious (and
many less serious) uses. Use `double` instead. And, if I may add, if
you can avoid it, do not use floating point at all.
 
R

rahulsinner

thanx for the advice.i didn't include <stdio.h> n left certain
formattings because i thought keeping the code to the min. will help
you people get to the problem immediately.

anyways next time onwards i will post codes which purely complies with
the std.

I know that real constants are "double" in c if not followed by a f but
i saw that question in an aptitude test n that baffled me.

anyways thnx for ur help.
 
J

John Bode

hi everyone,

main()
{
float f=0.7;
if ( f< 0.7)
printf("C");
else
printf("C++");
}


main()
{
float f=0.7;
if(f < 0.7f)
printf("C");
else
printf("C++");
}

The first code snippet prints C and the second one prints C++.Can
anyone tell me the reason for it

0.7 cannot be represented exactly in a 32-bit float (assuming a
normalized 23-bit mantissa): the closest you can get without going over
is 0.699981632. It takes 29 bits (again, assuming a normalized
manitssa) to represent 0.7 exactly (provided I've done the math right).


So the first test succeeds, because f (0.699981632) is indeed strictly
less than 0.7. The second test fails, because f (0.699981632) is not
strictly less than 0.7f (0.699981632).
 
W

Walter Roberson

John Bode said:
0.7 cannot be represented exactly in a 32-bit float (assuming a
normalized 23-bit mantissa): the closest you can get without going over
is 0.699981632. It takes 29 bits (again, assuming a normalized
manitssa) to represent 0.7 exactly (provided I've done the math right).

I think you must have done the math wrong.

111 into 1010 goes 1, remainder 11. Quotant so far: 0.1
Shift the remainder, yielding 110
111 into 110 goes 0, remainder 110. Quotant so far: 0.10
Shift the remainder, yielding 1100
111 into 1100 goes 1, remainder 101. Quotant so far: 0.101
Shift the remainder, yielding 1010
This is the same value as we started with, so we have a repeating fraction.

Therefore 0.7 (decimal) is 0.101,101,101... repeated infinitely.
There is no exact and finite base 2 representation of 0.7 (decimal)
 
J

John Bode

Walter said:
I think you must have done the math wrong.

111 into 1010 goes 1, remainder 11. Quotant so far: 0.1
Shift the remainder, yielding 110
111 into 110 goes 0, remainder 110. Quotant so far: 0.10
Shift the remainder, yielding 1100
111 into 1100 goes 1, remainder 101. Quotant so far: 0.101
Shift the remainder, yielding 1010
This is the same value as we started with, so we have a repeating fraction.

Therefore 0.7 (decimal) is 0.101,101,101... repeated infinitely.
There is no exact and finite base 2 representation of 0.7 (decimal)

Yeah, I mucked it up after 29 terms (serves me right for not
recognizing the pattern). Because my brain is fundamentally damaged,
I chose to do it the hard way and sum powers of 2 until I got close
(which gave me a bit pattern of 0.101001100110011...; the bit pattern
0.101101101... gets me 0.714, btw).

Even so, the point still holds; the value of the float representation
of 0.7 is strictly less than the value of the double representation of
0.7, whereas the value of the float representation of 0.7 is not
strictly less than the value of the float representation of 0.7.
 
V

Vladimir S. Oka

(e-mail address removed) opined:
thanx for the advice.i didn't include <stdio.h> n left certain
formattings because i thought keeping the code to the min. will help
you people get to the problem immediately.

anyways next time onwards i will post codes which purely complies
with the std.

I know that real constants are "double" in c if not followed by a f
but i saw that question in an aptitude test n that baffled me.

anyways thnx for ur help.

You're welcome, but please also *do* read the link in my sig. Quoting
context is as important, as providing minimal compilable source
(obviously unless you have a problem that it won't compile in the
first place).
 
K

Kufa

I know it's a bit out of place, but still
c) it's `int main(void)`

it is implementation dependent, int main(void) or int main(int,char**)
are only suggested by the norm.
 
V

Vladimir S. Oka

Kufa said:
I know it's a bit out of place, but still


it is implementation dependent, int main(void) or int main(int,char**)
are only suggested by the norm.

Do not snip attribution lines (the ones saying "Kufa wrote:"). I said
the above.

It is not "only suggested by the norm". The only two forms of `main()`
allowed by the Standard (or, if you want, the only two required to be
supported by all conforming implementations) are:

int main(void)
int main(int argc, char *argv[])

Some prefer the latter as:

int main(int argc, char **argv)

It's a matter of taste, really.

An implementation is allowed to implement, as long as it documents it,
a different sort of `main()`, and that is "implementation dependent".
However, it's not portable.

As this is comp.lang.c (you did find out what its about, didn't you?),
even if DS9K implemented:

double main(float pi)

it would be off-topic, and non-Standard...
 

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,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top