floating point modulo in expr?

Jan Klug's icon

hi,

shouldn't it be possible to use % in expr more or less the same way
as in the % object - with floats?

thanks,

jan

AlexHarker's icon

Apologies if you're making an observation and not asking for a solution, but as it doesn't work, and possibly never will do because modulo arithmetic is for integers only, you can do this to get the remainder of a float division in expr:

[expr $f1 - ($f2 * int($f1 / $f2))]

Regards

Alex

chrisroode's icon

Hey everyone! Years later, my C ignorance didn't know this, and It was great to see it, and a solution explained to me! So thank you for having this thread still on the site!

Gregory Taylor's icon

Hey, Jan.

the modulo work is integer-based. Grab the non-integer portion and process it separately, as my pal Alex suggests, and you'll be good to go!

Zeg tegen Jantine dat ik "hoi" heb gezegd! Misschien volgend jaar...

Roman Thilenius's icon


funny, i am always writing it the other way round: expr $f1-int($f1/$f2)*$f2

if i would be peter castine i could add that modulo is normally something different, and both, max and C, do it wrong. :)

chrisroode's icon

Funny, I'll just throw out that I decided to use a different approach in my situation for brevity:

($f1*100/$f2) % 100 * 0.01

This gave a fixed decimal result. But of course, I will have the other implementation in a comment...I did a little reading on float vs int math, and more recent gpu's have a smaller benefit to int math over float. Also, I am using this in a jit.expr rather than an expr or vexpr.

All things to consider, and it leads me to a question which I might post elsewhere #mindDrifting

Roman Thilenius's icon

0.01ing things is often enough to "float" them.