Topic: Dnt Use floating point numbers as boolean

PHP provides a couple of constants especially for use as Booleans: TRUE and FALSE, which
can be used like so:

if (TRUE)
print(“This will always print<BR>”);
else
print(“This will never print<BR>”);

Here are the rules for determine the “truth” of any value not already of the Boolean type:
** If the value is a number, it is false if exactly equal to zero and true otherwise.
** If the value is a string, it is false if the string is empty (has zero characters) or is the
string “0”, and is true otherwise.
** Values of type NULL are always false.
**If the value is a compound type (an array or an object), it is false if it contains no other
values, and it is true otherwise. For an object, containing a value means having a
member variable that has been assigned a value.
**Valid resources are true (although some functions that return resources when they are
successful will return FALSE when unsuccessful).

But dont use floating point  as boolean…..coz floating point number has some rounding errors…run the following code:

<?php

$floatbool = sqrt(2.0) * sqrt(2.0) - 2.0;
if ($floatbool)
print(“Floating-point Booleans are dangerous!<BR>”);
else{
print(“It worked … this time.<BR>”);}
print(“The actual value is $floatbool<BR>”);

?>

The variable $floatbool is set to the result of subtracting two from the square of the square
root of two—the result of this calculation should be equal to zero, which means that
$floatbool is false. Instead, the browser output we get is:
Floating-point Booleans are dangerous!
The actual value is 4.4408920985006E-16.

The value of $floatbool is very close to 0.0, but it is nonzero and, therefore, unexpectedly
true. Integers are much safer in a Boolean role—as long as their arithmetic happens only with
other integers and stays within integral sizes, they should not be subject to rounding errors……

Galib

Re: Dnt Use floating point numbers as boolean

thinking sleeping thumbs_up

http://tareq.wedevs.com/test/sig/blog_update.php
http://tareq.wedevs.com/test/sig/twitt_update.php