power Math node clipping negative numbers
Closed, ResolvedPublic

Description

When given a negative number the math.power node returns 0.
Might have to do with this block of code in blender/source/blender/nodes/shader/nodes/node_shader_math.c
case 10: /* Power */
{
/* Don't want any imaginary numbers... */
if ( in[0]->vec[0] >= 0 )
out[0]->vec[0] = pow(in[0]->vec[0], in[1]->vec[0]);
else
out[0]->vec[0] = 0.0;
}

Details

Type
Bug

Negative power values can give imaginary numbers, except when you raise it for integer values.
That feature was added in compositor long ago, will commit same for the shader node.

Testing it...

This is the new code, same as used for compositor. I'm not so sure about the usability of it... but for straight integer powers it's working fine.

/* Only raise negative numbers by full integers */
if ( in[0]->vec[0] >= 0 ) {
out[0]->vec[0] = pow(in[0]->vec[0], in[1]->vec[0]);
}
else {
float y_mod_1 = fmod(in[1]->vec[0], 1);

/* if input value is not nearly an integer, fall back to zero, nicer than straight rounding */
if (y_mod_1 > 0.999f || y_mod_1 < 0.001f) {
out[0]->vec[0] = powf(in[0]->vec[0], floorf(in[1]->vec[0] + 0.5f));
}
else {
out[0]->vec[0] = 0.0f;
}
}

Ton Roosendaal (ton) closed this task as Resolved.Nov 5 2012, 2:43 PM