• ### Floating point modulo?

1. Is `[expr fmod(\$f1, something)]` the only way to get a floating-point modulo?

2. This is a bit amusing: `/` and `div` are floating-point and integer division, respectively. So one might expect `%` and `mod` to be floating-point and integer modulo, respectively. But someone seems to have had a sense of humor some years ago?

hjh

• Posts 9 | Views 3016
• Both mod and % produce remainders for integer division in C and C++. Pd is written in C; since it uses the "%" operator internally to produce the output for `[%]` that is the operation you get.

It may seem like humor in retrospect given that Javascript, for example, gives double-precision floating point output for the "%" op. But I don't think it appeared that Javascript would run on billions of devices in the late 90s.

• @jancsika "Both mod and % produce remainders for integer division in C and C++. Pd is written in C; since it uses the "%" operator internally to produce the output for [%] that is the operation you get."

OK, fair, but... by analogy, we might go looking for a Pd object that uses fmod() internally:

`````` fmod 4
... couldn't create
``````

The specific use case was a demonstration for a class of crossfading between multiple wavetables (in the trendy Massive or Serum softsynth way). The crossfade coefficients are based on `fmod(wavetable_index, 2)` and really do need the fractional part.

hjh

• @ddw_music you can get rid of the floating point when multiplying by 10s and dividing at the end.
for example:

``````
[* 10000]
[mod 20000]
[/ 10000]

``````

fmod() you can use in [expr], [expr~] and [fexpr~]

And notice in the helpfile of [expr] >> [pd [expr] examples] the use of floats and integers

• OK, fair, but... by analogy, we might go looking for a Pd object that uses fmod() internally:

Would probably be nice to give a suggestion on error for common things like expr functions and other common errors the way git's CLI tools do.

I added a feature request to Purr Data for this:

https://git.purrdata.net/jwilkes/purr-data/issues/561

Edit: and to be clear-- I don't think Pd Vanilla needs to add all the expr functions as internal classes. That could easily cause backwards compatibility issues with externals or private abstraction collections for little benefit.

• @ddw_music Not internal, but not wildly expensive either.....
creb-fmod.zip
David.

• @whale-av Yes, creb is actually loaded by default in Purr Data.

• Thanks for the further comments.

I know you can multiply-mod-divide and get fractions. The broader point is about usability. It's a difficult trade-off -- adding more features for the sake of usability creates maintenance problems (which, to be honest, we see in SuperCollider-land), but keeping things light and maintainable means that easy things (fmod) become harder than they should be (concrete impact here is, I will have to explain to a classroom full of not particularly technical students why they can't use a simple, dedicated object for floating-point modulo when there are simple, dedicated objects for the other basic operators -- too much fine print gets old after awhile -- or let them use it Purr-Data now, and take it away from them later when I have to switch them to vanilla Pd so that they can use Gem on Mac).

hjh

• Just thought I'd point out that the difference between mod and % is how they handle negative values.

Posts 9 | Views 3016
Internal error.

Oops! Looks like something went wrong!