float

G

Gurikar

Hello,
Can any one tell me is the code below correct.

#include<iostream.h>
int main()
{
int i = 1;
float f = i / 2;
if(f)
cout<<"HI";
else
cout<<"Hello";
}

Does the above correct. Initially i felt output should be
unpredictable..Am i right..But when i tried in my compiler(VC7),,its
printing Hello..Is this right...Why????
 
S

Suman

Gurikar said:
Hello,
Can any one tell me is the code below correct.

#include<iostream.h>
/* get modern!Use #include said:
int main()
/*
** you would be better served using int main(void), if you are
** not going to use the arguments passed to main)
*/
{
int i = 1;
float f = i / 2;
Here what goes on under the hood is: i i.e. integer 1 is
divided by integer 2, which is zero, in integer arithmetic,
and the result is then stored to a float f.
Here value of float is zero, so the if condition fails, and
you get the else branch executed.

However, you are really lucky, as relational operators on floats
are not likely to give you correct result, when you do not take
into account a small tolerance.This is because of the way
floating point numbers(which are rarely exactly representable
using a finite number of bits) are internally stored as a bit pattern.
cout<<"HI";
else
cout<<"Hello";
/* int main(), expects you to give back an int */
return 0;
}

Does the above correct.
Depends on what you mean by *correct*.
Initially i felt output should be
unpredictable..Am i right..But when i tried in my compiler(VC7),,its
printing Hello..Is this right...Why????
If you want to print "Hello", use something like:

float f = (float)i / 2; /* or */
float f = i / 2.0f; /* or */
float f = (float)i / 2.0f; /* or */

and change the if (cond...) to
if ( fabs(f) < EPSILON ) /* we are sufficiently close to
zero(?)*/
where EPSILON maybe
const float EPSILON = 0.0000001f;
HTH,
Suman.
 
A

Arne Claus

#include said:
int main()
{
int i = 1;
float f = i / 2;
if(f)
cout<<"HI";
else
cout<<"Hello";
}

Does the above correct. Initially i felt output should be
unpredictable..Am i right..But when i tried in my compiler(VC7),,its
printing Hello..Is this right...Why????

Well ... i is an int and 2 is an int, that makes i/2 an int with value
0 (because 1/2 = 0.5 with .5 truncated).
Now you assign 0 to float f, making f 0.
After this you test if f is true (aka. sth. other than 0) and as f is 0
(aka false) the output "Hello" is correct.

Arne
 
L

Lionel B

Suman said:
/*
** you would be better served using int main(void), if you are
** not going to use the arguments passed to main)
*/

Yuk - disagree. "int main()" is visually explicit - "main" takes no parameters - while "int main(void)" to me hints at
some mysterious parameter called "void".
Here what goes on under the hood is: i i.e. integer 1 is
divided by integer 2, which is zero, in integer arithmetic,
and the result is then stored to a float f.
Here value of float is zero, so the if condition fails, and
you get the else branch executed.

However, you are really lucky, as relational operators on floats
are not likely to give you correct result, when you do not take
into account a small tolerance.This is because of the way
floating point numbers(which are rarely exactly representable
using a finite number of bits) are internally stored as a bit pattern.

It is true that "in general" absolute floating point comparisons are a Bad Thing; however, I suspect that comparison
with zero is ok - in the sense of giving predictable results - provided that (as in the OP's case) the object(s) of the
comparison has been *explicitly set to zero*, as opposed to being the result of a calculation. Thus:

double x = 0.0;

(x == 0.0)

will predictably evaluate to true, while:

double y = 3.456;
double x = y-3.456;

(x == 0.0)

will be UB. Please take with a pinch of salt; I cannot quote any standard to back me up on this.
/* int main(), expects you to give back an int */
return 0;
Depends on what you mean by *correct*.

To the OP: How do you know it isn't unpredictable? ;-)
If you want to print "Hello", use something like:

float f = (float)i / 2; /* or */
float f = i / 2.0f; /* or */
float f = (float)i / 2.0f; /* or */

(Yuk! C style casts).

Nonsense. They all print "HI", since they all evaluate to 0.5f so that (f) is true.
 
S

Suman

Lionel said:
Yuk - disagree. "int main()" is visually explicit - "main" takes no parameters - while "int main(void)" to me hints at
some mysterious parameter called "void".
Sorry, void *does not* mean *some mysterious parameter*.It means, *no
parameter*. *int main()* is plain bad, it tells you nothing, only that
main takes unspecified parameters.

[snipped...]
(Yuk! C style casts).
But they do the job, and it might just be easier to understand than
static_cast said:
Nonsense. They all print "HI", since they all evaluate to 0.5f so that (f) is true.
Yep! I got the HI and Hello wrong.

Regards,
Suman.
 
M

msalters

Suman schreef:
Sorry, void *does not* mean *some mysterious parameter*.It means, *no
parameter*. *int main()* is plain bad, it tells you nothing, only that
main takes unspecified parameters.

Look at the group. You're confusing C and C++. In C++, () means no
parameters and (void) is a deprecated old form that will be removed
(it's there to keep old C code compiling).

There is *no* way to specify in C++ that a function takes unspecified
parameters (not even ...)

Regards,
Michiel Salters
 
V

velthuijsen

Sorry, void *does not* mean *some mysterious parameter*.It means, *no
parameter*. *int main()* is plain bad, it tells you nothing, only that
main takes unspecified parameters.

You are confusing C and C++.
In C you would be correct but this is not the case in C++.
Just try the following:
int Foo()
{
return 0;
}

int main()
{
Foo(); // OK
Foo(1) // Compiler error
return 0;
}

The compiler should complain in the form of: Function does not take 1
argument
even though I just followed what you claimed that it would take
unspecified parameters.
Oh and if that is not enough I tried it also with a recursive call to
main(), same result.
 
G

Geo

Suman said:
Sorry, void *does not* mean *some mysterious parameter*.It means, *no
parameter*. *int main()* is plain bad, it tells you nothing, only that
main takes unspecified parameters.

No it doesn't, this is C++, a function with an empty parameter list
takes NO arguments, you are confused by a different language called C
!!!!
 
L

Lionel B

Suman said:
Sorry, void *does not* mean *some mysterious parameter*.

What part of "hints at" did you not understand?
It means, *no parameter*.

.... in the C programming language.
*int main()* is plain bad, it tells you nothing, only that
main takes unspecified parameters.

Wrong. It means no parameters and is standard C++.
[snipped...]
(Yuk! C style casts).
But they do the job, and it might just be easier to understand than
static_cast<typename> operator.N'est-ce pas?

Easier for whom? C programmers? Anyway, aren't you the one who admonished the OP to "get modern"...!?
 
S

Suman

Lionel said:
Suman said:
Sorry, void *does not* mean *some mysterious parameter*.

What part of "hints at" did you not understand?
It means, *no parameter*.

... in the C programming language.
*int main()* is plain bad, it tells you nothing, only that
main takes unspecified parameters.

Wrong. It means no parameters and is standard C++.
[snipped...]
float f = (float)i / 2; /* or */
float f = i / 2.0f; /* or */
float f = (float)i / 2.0f; /* or */

(Yuk! C style casts).
But they do the job, and it might just be easier to understand than
static_cast<typename> operator.N'est-ce pas?

Easier for whom? C programmers? Anyway, aren't you the one who admonished the OP to "get modern"...!?
Point well taken, post refreseher! Thanks.
 
A

Andre Kostur

Suman schreef:


Look at the group. You're confusing C and C++. In C++, () means no
parameters and (void) is a deprecated old form that will be removed
(it's there to keep old C code compiling).

There is *no* way to specify in C++ that a function takes unspecified
parameters (not even ...)

Huh? Ellipsis is still legal in C++... you just can't pass objects through
it.
 
J

Jack Klein

Sorry, void *does not* mean *some mysterious parameter*.It means, *no
parameter*. *int main()* is plain bad, it tells you nothing, only that
main takes unspecified parameters.

In both C and C++, empty parentheses in the DEFINITION of a function
indicate that it takes no parameters. There is absolutely no
ambiguity in either language.

In C, this definition would provide a declaration, but not a
prototype, for the defined function.

C++ does not actually have the concept of a function prototype. The
word 'prototype' is not defined in the C++ standard, although it does
slip in a few times, mostly in defining the concept of what C calls
prototype scope.

In C++, there is no way to declare a function accepts unspecified
parameters. Empty parentheses and (void) are exactly equivalent in
C++ function declarations.
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top