POV-Ray : Newsgroups : povray.newusers : What does the 'mod' operator do? Server Time
10 Nov 2025 20:45:49 EST (-0500)
  What does the 'mod' operator do? (Message 1 to 10 of 14)  
Goto Latest 10 Messages Next 4 Messages >>>
From: Kenneth
Subject: What does the 'mod' operator do?
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

From: jr
Subject: Re: What does the 'mod' operator do?
Date: 10 Nov 2025 02:40:00
Message: <web.691196451bdd4431475fba6a6cde94f1@news.povray.org>
hi,

"Kenneth" <kdw### [at] gmailcom> wrote:
> 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 ...
> ...
> QUESTION: "What does 'modulo' really mean?"
> ANSWER: "It depends on who you ask..."
>    :-o

fwiw, I like the term "clock arithmetic", can understand it :-), find it
"illustrative".
<en.wikipedia.org/wiki/Modular_arithmetic>


regards, jr.


Post a reply to this message

From: Kenneth
Subject: Re: What does the 'mod' operator do?
Date: 10 Nov 2025 06:15:00
Message: <web.6911c81e1bdd4431e83955656e066e29@news.povray.org>
"jr" <cre### [at] gmailcom> wrote:
>
> fwiw, I like the term "clock arithmetic", can understand it :-), find it
> "illustrative".
> <en.wikipedia.org/wiki/Modular_arithmetic>
>

Yes, I saw that too, and it makes perfect sense-- I could 'see' the result! I
even made a note about it in my test scene, for future reference when I
eventually forget what 'modulo' means, ha.

When I was first querying Google about this topic and read the AI summary--
which changes by the minute, apparently-- it also suggested a particular
video...which I now can't find. The mathematician host quickly discussed some
typical (i.e. pure-integer) examples of using mod...but then began a rather
abstract discourse on how modulo should be thought of in other NON-math
contexts. I had real trouble following it, but my own takeaway would be
*something* like this (not his own original example, which I didn't understand):

      mod(a collection of vegetables,vegetables that are red)

so the 'remainder' would be...vegetables that are not red(?)

Anyway, the video was certainly not a 'simple and easy' introduction to the
topic; it's a mystery as to how the AI engine picked it, out of so many others.


Post a reply to this message

From: kurtz le pirate
Subject: Re: What does the 'mod' operator do?
Date: 10 Nov 2025 09:11:06
Message: <6911f27a@news.povray.org>
On 10/11/2025 03:52, Kenneth wrote:
> 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.

Indeed, the definition given in the documentation is somewhat 
restrictive or ambiguous at first glance.

Just as multiplication is a simple addition, division is a simple 
subtraction.

Starting with any number (numerator), subtract the denominator as many 
times as possible : that is, as long as the remainder is greater than 
the denominator.

The “last” number is therefore the remainder. This is the number that 
the modulo function returns.


Try this :

#declare a = 27.44;
#declare b = 7.97;

#while(a > b)
  #declare r = a - b;
  #debug concat("mod(",str(a,0,2),", ",str(b,0,2),") = ",str(r,0,2),"\n")
  #declare a = r;
#end



Which gives :

mod(27.44, 7.97) = 19.47
mod(19.47, 7.97) = 11.50
mod(11.50, 7.97) = 3.53



...and 3.53 is of course equal to mod(27.44, 7.97)




-- 
kurtz le pirate
compagnie de la banquise


Post a reply to this message

From: Bald Eagle
Subject: Re: What does the 'mod' operator do?
Date: 10 Nov 2025 10:00:00
Message: <web.6911fcfe1bdd4431c93adb4f25979125@news.povray.org>
"Kenneth" <kdw### [at] gmailcom> wrote:

> Unfortunately, I'm still a bit puzzled by the docs' written description.

Well don't feel bad - because at one time I was too.
https://news.povray.org/povray.newusers/thread/%3Cweb.51fb3b6e744b4afe73fc9ebb0%40news.povray.org%3E/?mtop=386122

> 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."

Yes, well, when you get around to playing with / using these things enough, you
find that things like this can be ... flexible.
What is N/0?  It it "undefined"?  "unbounded"?  Infinity?
what about pow(0, 0)?

Sometimes the definitions of things change - or, rather - grow - to encompass
new discoveries and new ways of understanding, and therefore defining what that
thing is.

Like acids.  There have been a number of progressive expansions of the
definition of what an acid is.  All the way from Bronstead to Lewis.

zero
Negative numbers
or the square root of -1.
Look at the fundamentals of calculus.  We needed LIMITS to make that leap, and
land on putatively solid ground.

So:
Can you take the modulo of a COMPLEX number?
.. . .might depend on which computer language you use.


> In any case, POV-ray's 'mod' can obviously work with float values too...and
> fractions...at least when running in Windows.

Well of course.  If I have a 40-foot, 9-inch long log, and I want to cut it up
into sections that are 18 inches long, or 1.5 feet, then I will get a certain
integer number of logs, plus some remainder.
Just conceptually replace "integer" with "unit value".

(The universe is silently begging you to then expand your inquiries and
understanding to Basis Vectors ;) )

> So IMO the docs are either
> outdated or too 'strict', and at least somewhat unclear.

The docs in that case are just "poorly written" - because probably at the time,
they just wanted to casually provide the kindergarten-level explanation of the
function, and get on with the rest of it, rather than expanding it into a
mathematically accurate treatise on Modular Arithmetic.

Likely the documentation needs to be written in a large number of places, if
only to clear up little details like this.

I voluntell Kenneth.   :D

- BE


Post a reply to this message

From: Bald Eagle
Subject: Re: What does the 'mod' operator do?
Date: 10 Nov 2025 10:25:00
Message: <web.691203491bdd4431c93adb4f25979125@news.povray.org>
"Kenneth" <kdw### [at] gmailcom> wrote:
> ... it also suggested a particular
> video...which I now can't find. The mathematician host quickly discussed some
> typical (i.e. pure-integer) examples of using mod...but then began a rather
> abstract discourse on how modulo should be thought of in other NON-math
> contexts.

Video length?
Background?
Visual aids?
Presenter?

You're at the part where things get interesting - and remarkably useful - if you
stick with it and follow through far enough.

I can attest that there are an amazing number of things that can be done with
the simplest of "tricks" - just using vcross, vdot, matrix determinants,
eigenvalues, and vector swizzling.

And while that may all sound complicated and "out of reach" - the point is that
you don't have to understand a damned thing about how any of it is "done" - you
just need to understand the significance of the result.   What it means.

And then you can go on to code some pretty amazing stuff, that's easy to write
(as a result of knowing the "tricks"), and is FAST - compared to the usual "I'm
going to write my code in the exact same way as I went about figuring this out
the long way" that we all do so often.

- BW


Post a reply to this message

From: Bald Eagle
Subject: Re: What does the 'mod' operator do?
Date: 10 Nov 2025 13:50:00
Message: <web.691233971bdd44317f81dbac25979125@news.povray.org>
Just got a chance to do a quick AI-interrogation

 Practical Computer Graphics Uses of Modulo


Tiling and Repeating Patterns

Use case: Seamless textures, checkerboards, brick walls.
How: mod(x, tile_width) wraps coordinates to repeat a pattern.
Example: Creating a grid of alternating colors:

Text.#declare Color = select(mod(X + Y, 2), rgb <1,1,1>, rgb <0,0,0>);Show more
lines




UV Mapping and Texture Wrapping

Use case: Preventing texture coordinates from exceeding [0,1].
How: mod(U, 1) ensures UVs stay within bounds.



Animation Loops

Use case: Looping motion or color changes.
How: mod(Time, Duration) resets animation after a cycle.



Striping and Banding Effects

Use case: Zebra stripes, barcodes, scanlines.
How: mod(Y, StripeHeight) < Threshold to alternate colors.



Grid Indexing

Use case: Mapping 1D index to 2D grid.
How: Row = Index / Width, Col = mod(Index, Width).



Symmetry and Mirroring

Use case: Reflecting patterns across axes.
How: mod(abs(X), MirrorWidth) to repeat mirrored segments.



Procedural Noise and Randomization

Use case: Repeating pseudo-random patterns.
How: Seeded PRNGs often use mod to wrap values.




Non-Math Logic and Programming Tasks Using Modulo


Alternating Behavior

Use case: Toggle between two states (e.g., even/odd rows).
How: if (mod(i, 2) = 0) → even; else → odd.



Scheduling and Timing

Use case: Trigger events every N frames or seconds.
How: if (mod(Frame, N) = 0) → trigger.



Circular Buffers

Use case: Wrap-around indexing in fixed-size arrays.
How: Index = mod(CurrentIndex, BufferSize).



Color Cycling

Use case: Cycle through a palette.
How: Color = Palette[mod(i, PaletteSize)].



Hashing and Bucketing

Use case: Assign items to groups or bins.
How: Bucket = mod(ID, NumBuckets).



File Naming or Resource Allocation

Use case: Distribute tasks or files evenly.
How: mod(TaskID, NumWorkers).


Post a reply to this message

From: Thorsten
Subject: Re: What does the 'mod' operator do?
Date: 10 Nov 2025 13:55:55
Message: <6912353b$1@news.povray.org>
On 10.11.2025 12:13, Kenneth wrote:
> When I was first querying Google about this topic and read the AI summary--
> which changes by the minute, apparently-- it also suggested a particular
> video...which I now can't find. The mathematician host quickly discussed some
> typical (i.e. pure-integer) examples of using mod...but then began a rather
> abstract discourse on how modulo should be thought of in other NON-math
> contexts. I had real trouble following it, but my own takeaway would be
> *something* like this (not his own original example, which I didn't understand):

Well, have you considered that "AI" in the sense of large language 
models is the equivalent of a parrot? ;-)

Maybe a real source of information edited by humans might help? Apart 
from a good math book, try https://en.wikipedia.org/wiki/Modulo , which 
is reasonably complete and free human-made information.


Post a reply to this message

From: Kenneth
Subject: Re: What does the 'mod' operator do?
Date: 10 Nov 2025 15:15:00
Message: <web.6912476d1bdd4431e83955656e066e29@news.povray.org>
Thorsten <tho### [at] trfde> wrote:
>
> Well, have you considered that "AI" in the sense of large language
> models is the equivalent of a parrot? ;-)
>
> Maybe a real source of information edited by humans might help? Apart
> from a good math book, try https://en.wikipedia.org/wiki/Modulo , which
> is reasonably complete and free human-made information.

Currently, I have a very skeptical response to AI-generated answers -- when I
even pay attention to them; I wish Google would place those summaries at the
*bottom* of the search page, instead of at the top where they are the first
things we see. But of course, that would negate the entire purpose of why they
are there: using all of us as guinea pigs to help the AI engine 'learn', if only
by clicking on the 'Like' or 'Don't like' buttons that are so conveniently
placed-- which I never respond to, as MY silent protest.

Here's one AI answer/example that I saw while searching for 'modulo'-- it
quickly came and went, but I swear that I'm not making it up, honest!

"72 mod 10: The result is 2, because 10 goes into 70 three times, with a
remainder of 2."

So 10 X 3 = 70? I didn't know that!


Post a reply to this message

From: Bald Eagle
Subject: Re: What does the 'mod' operator do?
Date: 10 Nov 2025 15:20:00
Message: <web.691248401bdd44317f81dbac25979125@news.povray.org>
"Kenneth" <kdw### [at] gmailcom> wrote:

> Unfortunately, I'm still a bit puzzled by the docs' written description.

Check out the "divmod" function!
https://docs.python.org/3/library/functions.html#divmod

divmod(a, b, /)
Take two (non-complex) numbers as arguments and return a pair of numbers
consisting of their quotient and remainder when using integer division. With
mixed operand types, the rules for binary arithmetic operators apply. For
integers, the result is the same as (a // b, a % b). For floating-point numbers
the result is (q, a % b), where q is usually math.floor(a / b) but may be 1 less
than that. In any case q * b + a % b is very close to a, if a % b is non-zero it
has the same sign as b, and 0 <= abs(a % b) < abs(b).

HuhSayWhat?

"Your next assignment, should you choose to accept it....."

:D


Post a reply to this message

Goto Latest 10 Messages Next 4 Messages >>>

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