Folks, I know we can use unsigned/signed int and char to define our own Boolean type in C. We can either use 0 or 1 to distinguish FALSE or TRUE or use TRUE as anything which is non-zero.

Why can't I use float data type to define my own boolean type?

Can I use the zero and no zero concept using float and define my boolean type using it?

Recommended Answers

All 5 Replies

>Why can't I use float data type to define my own boolean type?
One reason might be because the comparisons are trickier. Depending on how you set this up, it could involve fudge factor comparisons, which are completely unnecessary when it comes to the concept of booleans.

>Can I use the zero and no zero concept using float and define my boolean type using it?
Yes. However, if you plan on converting arbitrary float values to this boolean type, zero may not be totally zero. You have to take rounding errors into consideration.

i'm sorry, but why would anyone WANT to use floats as a boolean type?

that's like taking the most basic thing in the world, and purposely making it complicated and unreliable.

That's the most ridiculous I've ever heard: using a float to define a boolean LOL :P

  1. It's harder to accomplish (but it isn't impossible :))
  2. A float uses much more memory than a char (and you don't need all this memory)

i'm sorry, but why would anyone WANT to use floats as a boolean type?

that's like taking the most basic thing in the world, and purposely making it complicated and unreliable.

I think maybe the poster is just wanting to test a float for true/false?

say

float ftru = 3.15;
int raNum = rand();
ftru *= raNum;
if(ftru)        //not really what I'd do, but I've seen similar.
                 // if(ftru*raNum){}
                 // would be much better.
{
}

>if(ftru)
>//not really what I'd do, but I've seen similar.
>// if(ftru*raNum){}
>// would be much better.
Why would it be much better?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.