Page MenuHome

OSL: Vector Math Node modulo uses wrong function
Closed, ResolvedPublicBUG


OSL should use fmod instead of mod. This applies only to vector node not regular math node. mod always returns positive values. This is evident when using negative numbers, see attached blend.

To quote OSL manual.

type fmod (type a, type b)
type mod (type a, type b)
The fmod() function returns the floating-point remainder of a=b, i.e., is the floating-point
equivalent of the integer % operator. It is nearly identical to the C or C++ fmod function,
except that in OSL, fmod(a,0) returns 0, rather than NaN. Note that if a < 0, the return
value will be negative.
The mod() function returns a-b*floor(a=b), which will always be a positive number or
zero. As an example, fmod(-0.25,1.0) = -0.25, but mod(-0.25,1.0) = 0.75. For
positive a they return the same value.


Changing mod here:

else if (type == "modulo") {
  Vector = mod(Vector1, Vector2);

to fmod will fix the bug but may break files that use OSL.

else if (type == "modulo") {
  Vector = fmod(Vector1, Vector2);

There is a case to add both versions of this function. I can provide patch to fix issue and another patch to add mod too.

Patch here: D6704