Dividing float numbers

Armin Seltz's icon

Though I am still pretty new to Max, I have picked up a few of the quirks or ways that Max is different to other visual programming tools. But can someone please explain why one of the most simple tasks like dividing a number uses such a counter-intuitive syntax?

So by default it does integer rounding - weird choice, but ok. So to get it to divide floats you cant just specifiy it in the inspector, no you have to write ANY random float after the "/". Okay, at least you can then have a fixed value on the right and not need an input.

Unless of course the left inlet is static and the right one changes.. well.. in that case.. screw you, you're not meant to do that. If I type "60.0 /" thats not "divide 60 by right input as a float" no its "pack 60. and /". Ey?

So if I want to divide 60 by a variable, like for example when you calculate ms from bpm, I have to do something like this? :

Max Patch
Copy patch and select New From Clipboard in Max.

Please tell me I have missed some option somewhere where you can change the @trigger of "/" and divide a constant.

Screen-Shot-2016-10-06-at-14.43.44.jpg
jpg
mzed's icon

Yes, you've missed !/. Like this:

Max Patch
Copy patch and select New From Clipboard in Max.

As far as the inherent weirdness of all of this: Max is quite an old programming language. The syntax of the math objects predates the existence of inspectors, so arguments are really the only way one can specify floating-point vs integer math. These objects were also written in a day where floating point math was much more costly than integer math. If Max we're written from scratch today, it probably would be different.

Armin Seltz's icon

I also just noticed that it isn't the output thats rounded, its the input that is rounded as well. So 0.4 * 1000 is 0 and not 400 as you would expect. So again you have to enter some number with a point.

Why not just /. *. +. and -. and it then uses floats?

Armin Seltz's icon

@MZED

Thanks, ok that makes some sense, although I dont understand why they wouldn't add it later on. Its not like the math operators cant be changed or made backwards compatible.

That !/ is a good workaround, if only it made any mention of it in the help file for "/". Also in the dropdown when you create objects it says for !/ "Divide input from a number". That doesnt really make sense both gramatically and logically, because you are still just dividing 2 numbers, except that the other input is triggering. So weird that you still input the number on the left though? The tooltip tells you its the other way round. Couldnt they just have made the right input red and left one blue? Its not like nobody would understand.

Lastly when double-clicking and typing "/" the other dividing method "!/" doesn't show up. Only if I type "div" does it appear.

So please Max devs. There is no reason to leave it like that and for people starting with Max it is totally illogical.

Screen-Shot-2016-10-06-at-15.05.17.jpg
jpg
mzed's icon

In fact, floating points are truncated to integers (not rounded) before doing integer math. So, 0.9999999 * 1000 = 0. Everything after the decimal is dropped.

!/ is mentioned in the reference document for /, but not on the "?" dropdown in the helpfile. That seems like a bug to me. The help files used to have more information.

Armin Seltz's icon

Yeah, that is strange. Never came across this behaviour anywhere. I find it kind of strange how they are handled. Also that round cant limit the decimal places of floats as you would expect. Ah well.. every language has some weird quirks.. just never when it comes to the most basic components there are. :D

mizu's icon

"round" ?
zz

Roman Thilenius's icon

as already said: !/

but, for the future, because you will need this stuff many times again in different contexts:

number
t b i

or

number
t 60 i
/ 0.

mzed's icon

There are some esoteric programming languages that truncate floats. Like C/C++.

Roman Thilenius's icon

"Why not just /. *. +. and -. and it then uses floats?"

that comes from ancient times.

if the [+] object would allow both foats and integers at the second inlet, so that you can first calculate 1/0.25=4. and then 1/3=1, the second inlet would have to do a typecheck.

you might now ask why this isnt the case for the first inlet... i can only speculate, but i believe that for the hot inlet of the math objects it is a quite common application that you want to feed it floats and ints alternately.

btw, if there would not be the wonderful solution of [!/ 0.] - and if you didnt need to learn about message ordering and the [t] object anway - you could still use the other wonderful solution which is [expr 60./$f1].

or [expr 60/$f1] - since the type of values entered does not matter in expr, its mode (int or float) is determined by the $ arguments only.

-110

Roman Thilenius's icon

oh i misread your question.

if you really want to it be [/.] (i think it is not so nicely readable) you can do that yourself by putting [expr 60./$f1] in a patcher and saving that patcher to disk with the filename "/..maxpat". (here is already one example where you can see that points suck ;)

-110

Armin Seltz's icon

@ROMAN THILENIUS

Thanks for your suggestions and explanations.

"the second inlet would have to do a typecheck" - I think thats exactly where other visual programming tools are different, in that they usually DO do a typecheck. In that sense Max seems a lot more like a programming language, where you have to specify the type for everything. That of course makes for much better performance. vvvv is the only other visual programming tool that I have used that is geared for performance, but it assumes everything to be floats and not integers first.

Anyways, I am sure I will get used to it and it will become second nature, just as the quirks of the other tools have.

mizu's icon

thanks too !
one question: is it a gain to think boolean in Max ? Or 0 / 1 are now allways treated as 32/64 bit values ?
- Max is my only way in the world of programming. Just a curious observer of the symptoms...
It was a thing to scale MIDI values with the floating world of MSP. Used to add 0.5 to round clearly to integers.
best
zz

Roman Thilenius's icon

everything what works for without harming any other creature is allowed.

of course any 0 or 1 in maxmsp is a 32 bit integer but you can think in bools whenever you wish.

especially with comparison operators it wouldnt make much sense to start thinking different.

if you are brave you also use 0. and 1. floating point as bools, for example in expr expressions.

and if you use pd, a sister language of max, you have to use floats as bools because it doesn have ints.

-110

mizu's icon

Thanks Roman
i understand the machine prefers treat values with a regular nb of bits. The number of ants working inside and emerging from isn't infinite.
best
michel