POV-Ray : Newsgroups : povray.newusers : What does the 'mod' operator do? : What does the 'mod' operator do? Server Time
10 Nov 2025 18:58:48 EST (-0500)
  What does the 'mod' operator do?  
From: Kenneth
Date: 9 Nov 2025 21:55:00
Message: <web.691152e3c7653640e83955656e066e29@news.povray.org>
I am new to POV-ray's 'mod' operator-- and the meaning of 'modulo' itself. I
have seen it used in many newsgroup code examples-- and there are probably
numerous newsgroup posts asking questions about it through the years-- but I
never really understood what it does or what to expect as the result, due to the
rather terse explanation in the docs:

mod(A,B)
Value of A modulo B. Returns the remainder after the integer division of A/B.
Formula is mod=((A/B)-int(A/B))*B.

It has never been quite clear to me what 'integer division' means there-- since
the supplied A and B values could conceivably be typical floats and not just
integers. Did it mean initially truncating the fractional parts of
float values >= 1.0? And I assumed that the 'remainder' would be the
*fractional* part of a division of the two integers -- like 30/7 = 4.2857... ,
with .2857 being the remainder. At least, this was my rather fuzzy
interpretation of the docs...without actually paying attention to the math
formula there :-(

So, rather than allowing my existential crisis to continue, I finally got
curious enough to actually try USING it, along with #debug:
       mod(30,7)
The result is... 2.  What the...??  Since this obviously didn't agree with my
assumptions, I had some learning to do...which led me down an interesting
Wikipedia path to various interrelated topics:
      modulo
      Euclidean arithmetic
      modular arithmetic
Plus Google's now-ubiquitous A.I. 'summaries',  as well as some videos by
mathematicians with their own (arcane!) discussions of what 'modulo' means.

So my mod result of '2' finally made sense:  7 goes into 30 four times-- with
a remainder of 2! Simple. And I finally took the time to plug these A and B
values into the docs' equation, working it out piece by piece. :-D
Unfortunately, I'm still a bit puzzled by the docs' written description.

Armed with my newly-found confidence and quasi-knowledge, I tried a trickier
example...not really knowing what to expect:
      mod(27.44, 7.97)
The #debug result is...  3.53      a float and not just an integer(!)

An even trickier example:
      mod(0.87, 0.59)
The result is 0.28        just a fractional float value.

Those remainder results are correct of course-- but how do these two examples
jibe with the docs' interpretation of 'integer division'? Interestingly, from
reading the Wikipedia entry about 'modulo' (with a little help from 'Euclidean
arithmetic'), it seems that the strictest meaning of the modulo operator
involves the use of integers and only integers-- for the A and B values *and*
the result. So the meaning of POV-ray's documentation becomes a bit clearer. But
another interpretation in the 'modulo' topic...if I understand it
correctly...puts no restriction on the values re: floats vs. integers--
"depending on the programming language used."

In any case, POV-ray's 'mod' can obviously work with float values too...and
fractions...at least when running in Windows. So IMO the docs are either
outdated or too 'strict', and at least somewhat unclear.

QUESTION: "What does 'modulo' really mean?"
ANSWER: "It depends on who you ask..."
   :-o


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.