Page 1 of 1

Driving me crazy...

Posted: Thu Sep 18, 2003 5:24 pm
by jayr517
I have the following code:

Code: Select all

define(X_MAX, 4); // data type integer

$x = 4.0; // data type double

if ($x == X_MAX) {
    print("true<BR>");
&#125;
else
    print("false<BR>");
This is always evaluating to false unless I cast $x to an integer. I thought only === checked the types??? Even if I cast X_MAX to a double, it evaluates to false. Is there a way around this without casting $x to an integer?

Thanks in advance.
J

Posted: Thu Sep 18, 2003 5:59 pm
by JAM

Code: Select all

// you should wrap the defined name with single or double quotes
define('FOO', 4);
$x = 4.0; // data type double 

// I added an extra type check here...
if (is_int($x)) { 
 // same as yours, but shorter. just as example...
    echo ($x == FOO ? 'true<br>' : 'false<br>'); 
} else {
    echo 'Not an integer...<br>';
}

Posted: Thu Sep 18, 2003 6:19 pm
by jayr517
I am wrapping the define name in quotes...I just forgot to put that in the code...oops.

Thanks for the input, but how does this address the issue I'm having? I can't understand why $x == X_MAX evaluates to false when $x is 4.0 and X_MAX is 4.

I get the same "false" result, even if I use a regular variable, instead of a constant. I've tried casting the constant and regular variable to a float, but I still get a false result. The only way I get a "true" result is if I cast $x to and integer, which I don't want to do. I want to compare a float to an integer and have it work the way it should. It doesn't make any sense and I'm going nuts over what should be such a simple thing!

Posted: Thu Sep 18, 2003 6:35 pm
by JAM
Well... As this example shows, setting $x to a double/float doesn't have to produce the wanted result...

Code: Select all

$x = 4.0;
    echo $x; // returns 4
    // but this verifies it...
    if (is_float($x)) { echo 'yes'; }
    else { echo 'no'; }
So far, if noone else have abetter solution, checking with is_int(), is_float etc. migh be a way...
Interesting topic though.

Posted: Thu Sep 18, 2003 7:01 pm
by jayr517
You're right...regardless of whether the datatype is float or integer, 4.0 returns 4, and 4 returns 4 (obviously). In my example above, the ONLY difference between $x and X_MAX is the type. It should evaluate correctly and it's not.

Straight from the PHP manual:
$a == $b - Equal - TRUE if $a is equal to $b.
$a === $b - Identical - TRUE if $a is equal to $b, and they are of the same type. (PHP 4 only)

(I'm using PHP4)

doesn't make any sense.
:(

Posted: Thu Sep 18, 2003 7:37 pm
by Unipus
Your problem is elsewhere. I just created a file with your code, and it evaluates TRUE for me.

Posted: Thu Sep 18, 2003 7:54 pm
by jayr517
Here's the section of code...

Code: Select all

if ($this->startSide == "vertical") {

                $this->width = Y_MAX - $this->startCoordinate->getY();
                $this->height = $this->width * (tan(deg2rad($this->angleStart)));
                $counter = 0;

                $wrkMaxX = X_MAX;
                $wrkMaxY = Y_MAX;

                $xType = gettype($this->startCoordinate->getX());
                $yType = gettype($this->startCoordinate->getY());

                settype($wrkMaxX, $xType);
                settype($wrkMaxY, $yType);

                print("Data type for \$this->startCoordinate->getX() = "
                      .gettype($this->startCoordinate->getX())
                      ."<BR>Data type for \$wrkMaxX = "
                      .gettype($wrkMaxX)." "."<BR>");

                print("Value of \$wrkMaxX = ".$wrkMaxX
                      ."<BR>Value of \$this->startCoordinate-getX() = "
                      .$this->startCoordinate->getX()."<BR>");

                if ($this->startCoordinate->getX() <= $wrkMaxX)
                    print "true<BR>";
                else
                    print ($this->startCoordinate->getX()
                           ." is NOT less than or equal to ".$wrkMaxX." ?!?!<BR>");

                while ($this->startCoordinate->getX() <= $wrkMaxX &&
                       $this->startCoordinate->getX() >= 0 &&
                       $this->startCoordinate->getY() <= $wrkMaxY &&
                       $this->startCoordinate->getY() >= 0) {

                    $this->vector[$counter] = $this->startCoordinate;
                    $width  = $this->width - DECREMENT_VALUE;
                    $height = $width * tan(deg2rad($this->angleStart));

                    $xDiff = $this->height - $height;
                    $yDiff = $this->width  - $width;

                    if ($this->priorStart->getX() <= $this->startCoordinate->getX()){
                        $newX = $this->startCoordinate->getX() - $xDiff;
                    }
                    else
                        $newX = $this->startCoordinate->getX() + $xDiff;

                    if ($this->priorStart->getY() <= $this->startCoordinate->getY()) {
                        $newY = $this->startCoordinate->getY() + $yDiff;
                    }
                    else
                        $newY = $this->startCoordinate->getY() - $yDiff;

                    $this->startCoordinate->setX($newX);
                    $this->startCoordinate->setY($newY);
                    $counter++;
                }
            }
Here's the result I get...

Code: Select all

Data type for $this-&gt;startCoordinate-&gt;getX() = double
Data type for $wrkMaxX = double 
Value of $wrkMaxX = 4
Value of $this-&gt;startCoordinate-getX() = 4
4 is NOT less than or equal to 4 ?!?!
Can you see anything in this section of code that I'm doing wrong???

Admin Edit: The PHP tags are your friend. =)

Posted: Fri Sep 19, 2003 1:39 pm
by m3rajk
i'm not sure where it is. tyr the [ php] tags. they do a better job when it's in php