Modulus in SeExpr

Hello! So I’ve been messing with the SeExpr Checkerboard script and I’ve noticed something.

You see that there’s $xodd = ($xnum % 2 > 1); and $yodd = ($ynum % 2 > 1);. You know that it’s impossible for x % 2 to be bigger than 1. Yet when I replace the > with a >=, nothing changes in the script.

How? Or maybe the output changes and I just didn’t notice?

Thank you!

in SeExpr modulo always converts input values to floats.

float fmod ( float x, float y) - remainder of x / y (also available as '%' operator)

so …

$xodd = ($xnum % 2 > 1);
$yodd = ($ynum % 2 > 1);

# is same as

$xodd = ($xnum % 2.0 > 1.0);
$yodd = ($ynum % 2.0 > 1.0);

and so left over fraction can be greater than 1.0 (ie. (3.7777 % 2.0) == 1.7777)

/AkiR

But what about a number like 3? As I said before, when I change the operator > to >= nothing happens. Why?

hmm… maybe you are thinking that u and v are pixel/texel coordinates (integers)?

if you create simple SeExpr, you will notice that u and v are floats in range [0.0, 1.0] (NOT pixel coordinates)

# u is in range [0.0 .. 1.0] -> from canvas [left_border .. right_border]
# v is in range [0.0 .. 1.0] -> from canvas [top_border .. bottom_border]

$u_remainder = (5.0 * $u) % 2.0;
$v_remainder = (5.0 * $v) % 2.0;

$color = [$u_remainder > 1.0, $v_remainder > 1.0, 0.0];
$color

so change of > to >= changes checker board cell splitting in ultra rare cases by one pixel (but most of the time it will not change anything, it would only change the case where pixel is exactly at infinitely thin cell splitting line)

/AkiR

1 Like

Ohhhhh… Thanks so much!