Hi
Is this true when an int or a float is converted into a bool, "0" or "0.0" is converted into "0" and anything else (e.g. 2, 9, -1, 1.9, 0.0003, 0.4, -0.8) is converted into "1". Could you please confirm this? Thanks.
10 replies to this topic
#1
Posted 17 May 2011 - 04:45 PM
I'm an outright beginner, learning C++. Using Win XP Pro and Code::Blocks. Be nice to me, please.:)
|
|
|
#2
Posted 17 May 2011 - 06:03 PM
Which language are you talking about? C and C++ handle this differently.
#3
Posted 17 May 2011 - 06:18 PM
C++
I'm an outright beginner, learning C++. Using Win XP Pro and Code::Blocks. Be nice to me, please.:)
#4
Posted 17 May 2011 - 06:34 PM
You need to understand that 0.0 is not 0. The former is a floating point numeric and is not automatically cast to zero by assigning it to a boolean type. 0.0 will therefor become true not false.
Any value other than 0 (of any integral type, i.e. char, short, integer or long) will be true and you can assume this. -1, 0.0, 1.0 are all true.
Any value other than 0 (of any integral type, i.e. char, short, integer or long) will be true and you can assume this. -1, 0.0, 1.0 are all true.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
#5
Posted 17 May 2011 - 06:42 PM
Alexander said:
You need to understand that 0.0 is not 0. The former is a floating point numeric and is not automatically cast to zero by assigning it to a boolean type. 0.0 will therefor become true not false.
Any value other than 0 (of any integral type, i.e. char, short, integer or long) will be true and you can assume this. -1, 0.0, 1.0 are all true.
Any value other than 0 (of any integral type, i.e. char, short, integer or long) will be true and you can assume this. -1, 0.0, 1.0 are all true.
Thanks. Sorry for asking this if the answer is already too obvious! Non-integral type will also be true such as 0.0001, -0.09.
Regards
Jackson
I'm an outright beginner, learning C++. Using Win XP Pro and Code::Blocks. Be nice to me, please.:)
#6
Posted 18 May 2011 - 01:15 AM
jackson6612 said:
Thanks. Sorry for asking this if the answer is already too obvious! Non-integral type will also be true such as 0.0001, -0.09.
Is my statement correct? Please let me know. Thanks.
I'm an outright beginner, learning C++. Using Win XP Pro and Code::Blocks. Be nice to me, please.:)
#7
Posted 18 May 2011 - 01:38 PM
jackson6612 said:
Is my statement correct? Please let me know. Thanks.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
#8
Posted 18 May 2011 - 08:20 PM
In addition c does not have a bool data type and uses int with 0 and non zero values in all places such as if for and while.
C++ does have a bool type that has been referred to above.
Also you can write code such as
to verify it yourself. It is casting a float to a bool and checking resulting value.
C++ does have a bool type that has been referred to above.
Also you can write code such as
float a = -0.009;
if((bool)a == false)
printf("false\n");
else
printf("true\n");
to verify it yourself. It is casting a float to a bool and checking resulting value.
#9
Posted 19 May 2011 - 06:48 AM
fayyazlodhi said:
In addition c does not have a bool data type and uses int with 0 and non zero values in all places such as if for and while.
C++ does have a bool type that has been referred to above.
Also you can write code such as
to verify it yourself. It is casting a float to a bool and checking resulting value.
C++ does have a bool type that has been referred to above.
Also you can write code such as
float a = -0.009;
if((bool)a == false)
printf("false\n");
else
printf("true\n");
to verify it yourself. It is casting a float to a bool and checking resulting value.
Hi Fayyaz
I think you have your code in C. I have changed it into C++. There was no need to convert "a" into bool in the if condition because the if statement already uses the bool type for comparison and automatic conversion takes place. Do I have it correct? Please let me know. Thanks a lot.
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
float a = -0.009;
[B] if(a == false)[/B]
cout << "false\n";
else
cout << "true\n";
system("pause");
return 0;
}
I'm an outright beginner, learning C++. Using Win XP Pro and Code::Blocks. Be nice to me, please.:)
#10
Posted 19 May 2011 - 10:21 AM
yes you are right. I only added the cast to make it more explicit so that you know what is happening by seeing it in code rather than knowing that "in an if condition it automatically converts into bool".
#11
Posted 19 May 2011 - 10:28 AM
Thank you for letting me know this, Fayyaz.
I'm an outright beginner, learning C++. Using Win XP Pro and Code::Blocks. Be nice to me, please.:)
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









