PDA

View Full Version : if ( 0 == 0 ) not working


HapG
06-24-2008, 08:23 PM
when does Zero not equal Zero?

//get pivot
float $jX = `getAttr ($trans+".sx")`; // Result: 0 //

if( 0 != $jX )
{
print "passed\n";

}


In the following code, the attribute value is 0, but the "if" statement decides $jX and 0 aren't equal.
Is it because getAttr returns a double instead of a float? If so, how do I force $jX to be a float?

Keilun
06-24-2008, 08:29 PM
You never want to compare floating point values like that. Floating point values are imprecise and can contain a small residual value that may be truncated by MEL (but still carry its value).

Compare your float with a tolerance value:

eg.

global proc int isEqual( float $v1, float $v2 )
{
int $isEqual = 0;
float $tol = 0.00001;
if( abs($v1-$v2) < $tol )
$isEqual = 1;
return $isEqual;
}

CGTalk Moderation
06-24-2008, 08:29 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.