PDA

View Full Version : float 0.357 is not equal 0.357


butCherHeLL
02-12-2006, 06:24 PM
displayRGBColor -c background 0.357 0.357 0.357;

float $c[] = `displayRGBColor -q background`;

print $c[0];

if ($c[0]==0.357){print("this will never print");}




what am I doing wrong ? what is the solution

butCherHeLL
02-12-2006, 06:28 PM
by another mean

displayRGBColor -c background 0.357 0.357 0.357;

float $c[] = `displayRGBColor -q background`;

print $c[0];//prints 0.357

if ($c[0]!=0.357){print("this will print.however $c[0] seems 0.357");}


what is the correct iteration for the point sence ?

what am I doing wrong. and one have a solid solution ?

butCherHeLL
02-12-2006, 07:13 PM
I found a solution with clamp function.

here is the code I am sharing.and waiting for other comments



//More Stable Toggle Background Color:

global proc moreStableToggleBgColor()
{
float $c[] = `displayRGBColor -q background`;
int $aas =clamp(0,1000,$c[0]*1000);
if ($aas==0){displayRGBColor -c background 0.357 0.357 0.357;}
else
if ($aas==356){displayRGBColor -c background 0.631 0.631 0.631;}
else
if ($aas==630){displayRGBColor -c background 1 1 1;}
else
if ($aas==1000){displayRGBColor -c background 0 0 0;}

string $panel = `getPanel -withFocus`;
modelEditor -e -grid 0 $panel;
}

Sphere♥
02-12-2006, 09:42 PM
Floating point numbers in computing are approximations since they don't have an exact binary representation. The problem you describe happens in all computers even with high precision floats. I'm not sure about MEL, but In programming, to test equality, what we generally do is declare an error tolerance, then test the difference against this, for example:

EPSILON = 0.00001 // tolerance
//...
if (abs(FloatA - FloatB) < EPSILON) {
// considered equal
}

sam_g
02-15-2006, 11:07 PM
floatEq is listed as a Maya command, but it doesn't seem to exist in the program.
'Cannot find procedure "floatEq"'
Perhaps it has been deprecated?

It is supposed to be a mel construct, so it's probably exactly what Sphere wrote.

Sam

CGTalk Moderation
02-15-2006, 11:07 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.