PDA

View Full Version : Interpret This Line of code


Fess
10-19-2005, 03:59 PM
Hello,

I need some help on how to read this code. I'm learn MEL but have not come across this syntax as yet.

This is taken from the avalanche.mb fluid file's expression that came with Maya.

$i & $zres are both integer variables.

float $v = $vscale * (float)$i/(float)($zres);

Set float variable $v to the value of $vscale multiplied by..............??????

Why is "float" in parentheses? What does this mean?

Some form of automatic variable type conversion?


Confused
Fess

twidup
10-19-2005, 04:12 PM
from someone else :

The (float) are casting an otherwise-non-float value into a float. In the instance above, if both of the variables $i, and $zres are integers, then the result of $i/$zres would be an integer, even if the true result would be a float. By casting at leaast one of the arguments, it is forcing the compiler to do this math in full floating point mode, rather than in integer mode.

EigenPuff
10-19-2005, 06:18 PM
... The reason why casting to a (float) would be useful is that if you divide by an integer, you can't get fractional values - 0.5, 0.75, etc. In integer math, 1/2 = 3/4 = 99/100 = 0, which can be really annoying. In this case, $v would have been probably always be 0 or $vscale - no gradiation between the two - if the writer hadn't cast the expression to floating point math.

Fess
10-20-2005, 02:13 PM
twidup, EigenPuff...

Thanks for the explaination.

Fess

CGTalk Moderation
10-20-2005, 02:13 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.