Jump to content

converting int, float, char into bool

- - - - -

  • Please log in to reply
10 replies to this topic

#1
jackson6612

jackson6612

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts
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.
I'm an outright beginner, learning C++. Using Win XP Pro and Code::Blocks. Be nice to me, please.:)

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
Which language are you talking about? C and C++ handle this differently.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
jackson6612

jackson6612

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts
C++
I'm an outright beginner, learning C++. Using Win XP Pro and Code::Blocks. Be nice to me, please.:)

#4
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
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.
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.

#5
jackson6612

jackson6612

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts

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.

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
jackson6612

jackson6612

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts

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
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200

jackson6612 said:

Is my statement correct? Please let me know. Thanks.
Yes, integral types would represent only whole numbers (i.e. no fractions).
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.

#8
fayyazlodhi

fayyazlodhi

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 403 posts
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


    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
jackson6612

jackson6612

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts

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


    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
fayyazlodhi

fayyazlodhi

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 403 posts
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
jackson6612

jackson6612

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts
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