mod() operator argument types

Hello there,

It is not that hard to create a mod() function for the Arduino, but i checked and it is built into the language so why not use it.

The slight problem i ran into was that in all the doc's they specify an integer, but do not explicitly state that it only takes type int (16 bit).

What i was wondering, was does it take type long too, or how about type long long, or unsigned long? I know it does not take type float because the doc's do state that and i dont need that anyway.

I also did not want to have to do self testing on numbers from 65536 to over 2 billion to find out if it would take type long or type unsigned long. I did search the Arduino source files but did not find the specific implementation they are using in any of the files so it must be in a precompiled lib or something. I was also unable to find any prototype function which would state the argument types.

If it does take type long and unsigned long then i wont have to make a new routine. Not a big deal but it would save some code space if it works with those bigger types too.

Thanks :slight_smile:

In German, we have a saying: "Probieren over studieren". Really, really rough translated: "Trying is better than thinking about it"...

Compilers are pretty smart. If you defined two variables, for example as Long, and tried to do Modulo on them, the Compiler will complain if it has no Definition which accepts those Longs. If the Compiler doesn't complain, chances are that it will work. I also don't think you would have to do a Billion tests. Computers being the way they are, if it works with one number, it will work with others, except for the so called border cases. Border cases can be Zero, or the Maximum/Minimum values that the data type can handle.

What is mod()?

Is it different from the modulo operator, %?

'cos I'm pretty sure I've used % successfully with unsigned long.

JaBa:
In German, we have a saying: "Probieren over studieren". Really, really rough translated: "Trying is better than thinking about it"...

Compilers are pretty smart. If you defined two variables, for example as Long, and tried to do Modulo on them, the Compiler will complain if it has no Definition which accepts those Longs. If the Compiler doesn't complain, chances are that it will work. I also don't think you would have to do a Billion tests. Computers being the way they are, if it works with one number, it will work with others, except for the so called border cases. Border cases can be Zero, or the Maximum/Minimum values that the data type can handle.

Hi,

Ha ha, in English we also have a saying, "If you dont know the answer to a question and you know a message board where hundreds of users with experience in the field you are working in at one time or another probably had the same question and already found an answer, then by all means, ask the question".
Loosely translated it means, "Ask and maybe you will receive" :slight_smile:

In other words, i didnt want to have to experiment with this and after all there are so many users that visit this site i would bet that someone tried this already and can give an answer without even thinking about it. Yes, maybe they wont see this thread, but then maybe they will and will be willing to give a direct answer.

I also think it might help to update the doc's to show what types it can take so future users dont have to experiment either.

As a last point, sometimes there are bugs in the codes used and experiences users would know about this too. Maybe it works with type int but with type long it has a bug where it does not work over a certain value like 0x100000 or something like that, and that could be because of the implementation, which i could not find.
Because of the way mod() is sometimes calculated, it may be that the combination of the first number and last number must meet a certain criterion such as a lmiit:
y=firstnumb % lastnumb

In some calculations of this statement, if we declare firstnumb to be type long and lastnumb to be type long, the prototype wont complain, but internally when the calculation is performed it could still cause an error because in order to fully handle type long's it might have to do the actual calculation in type long long. Similarly, if it takes type long long for both then it may not ever be able to handle the full range of those two.

So see there is more to it than just satisfying the prototype definition. We might get lucky and see it fail for any type beyond type int, but then we still dont know if the internal calculation is done with type longs or type int's. If it is done with type int's then there could be a problem with certain combination of values for firstnumb and lastnumb.

GypsumFantastic:
What is mod()?

Is it different from the modulo operator, %?

'cos I'm pretty sure I've used % successfully with unsigned long.

Hi there,

Yes, mod() is the function that in Arduinoland would be done for example as:
y=24 % 7;

and here y would be equal to 3 after the instruction is complete.

In the doc's, they show that the 24 in that expression is an integer, type int. But there is reason to believe that it can also be type long.

Also, it would be nice to know what the 7 could be also, type int or can it be type long, or unsigned long, etc.

Thanks.

Where do you see the docs for mod()? I can't find it referenced for C++ or for arduino.

If you just use the operator - % - it will handle all integral types.

KeithRB:
Where do you see the docs for mod()? I can't find it referenced for C++ or for arduino.

If you just use the operator - % - it will handle all integral types.

Hi,

I found it in the doc's for the Arduino under the IDE menu where it says help/reference, but it is also on the web. Just search Arduino Mod function or something like that. It is actually written as N % M where the percent sign indicates the mod function not integer division.

The thing is that there is no room for guessing because if the code can not be checked for validity right away, then there is a chance that in the future it could fail unexpectedly, and the mod function is not your ordinary function where you can just assume "it works". To be sure it works, we really have to have a document that says it works and under what conditions it works, or else be able to see the implementation.

I could write my own i guess, that way i know what the range is without any guesswork or extensive experimentation. Also, i guess i could experiment a little with the built in function to see if here is a problem with larger operands or not. I cant do this right away though so maybe someone else knows.

One of the direct questions would go as follows...

If the prototype was something like this:
int mod(int n, int m)

then the question comes up if this would work, just for example:
y=mod(0xFFFF,Ox0FFF);

I am thinking that it should work but it's nice to see some documentation like:
long mod(long n, long m)

If the prototype was shown in the doc's we'd know right away.

% is not a mod function, it is an operator and guaranteed to work with all integer types.

I searched every file in the Arduino core and avrlibc. There is no mod() function in any of them.

WTF are you talking about?

MrAl:
Hi,

Ha ha...i didnt want to have to experiment with this and after all there are so many users that visit this site i would bet that someone tried this already and can give an answer without even thinking about it.

So, in other words, your time is more valuable than ours. Hmmm...I might want to argue that.

Where do you see the docs for mod()?

I found it in the doc's for the Arduino under the IDE menu where it says help/reference

Oh no you didn't !

KeithRB:
% is not a mod function, it is an operator and guaranteed to work with all integer types.

Hello,

Yes i know it is an operator, but ultimately it will execute a function. I made that clear in post #6.

How do you know it works with all integer types?
All integer types would be at least int, long, and long long.

econjack:
So, in other words, your time is more valuable than ours. Hmmm...I might want to argue that.

Hi,

That's a false conclusion from the data given.
If my time was worth $120 per hour and your time was worth $240 per hour and i had to spend 1 hour doing a task and you only had to spend 2 minutes doing the task, my total cost would be 120 dollars while yours would be only 8 dollars.
So although your time was move valuable it would cost me more than you.
The task we are talking about here is finding out exactly how a function behaves, which entails some experimentation which takes some finite amount of time. But that's only if you dont know the answer already. If you know the answer already then it no longer takes you any time to show the answer. If someone asked this question before then someone else here or even that same person might already know the answer and can provide the result in less than 2 minutes whereas someone new to the question could end up spending 2 hours experimenting.
That's how sharing knowledge works.

Did you search for an add() routine when you used the operator '+' in your code?
How about the assign() routine, you use '=', don't you?

What does '%' make special?

MrAl:
Yes i know it is an operator, but ultimately it will execute a function. I made that clear in post #6.

How do you know it works with all integer types?
All integer types would be at least int, long, and long long.

You can just as well question every single C/C++ function and operator. How do you know that '%' works correctly; or that '/' works. And it does not call a function

And no, it does not ultimately execute a function.

Just for the fun: Pentium FDIV bug so you better start checking your processor :smiley:

By the way; please provide a link to that mysterious mod() function; I could not find it in the reference home page and I could only find modf(), modff() and fmod() in my Arduino installation directory.

The modulus operator "%" is defined by the C language standard...

MrAl:
Hi,

That's a false conclusion from the data given.
If my time was worth $120 per hour and your time was worth $240 per hour and i had to spend 1 hour doing a task and you only had to spend 2 minutes doing the task, my total cost would be 120 dollars while yours would be only 8 dollars.
So although your time was move valuable it would cost me more than you.
The task we are talking about here is finding out exactly how a function behaves, which entails some experimentation which takes some finite amount of time. But that's only if you dont know the answer already. If you know the answer already then it no longer takes you any time to show the answer. If someone asked this question before then someone else here or even that same person might already know the answer and can provide the result in less than 2 minutes whereas someone new to the question could end up spending 2 hours experimenting.
That's how sharing knowledge works.

No, that's not how it works. The value to me from my 2 minutes of effort is worth 0 to me, but worth $120 to you. The cost to me of ignoring your request is zero, but the cost to you is higher...indeed more than $120. If the reward to you is less than your cost, you shouldn't bother finding the answer. Since that's not the case, you want someone else to subsidize your reward. Even if I know the answer without research, it still takes time to type it here to let you view it. I think this is especially true for your question, as even a shallow search would have turned up an answer. Also, those who ferret out the answer are more likely to learn from the process and be able to help others down the road when they need it. Those who are spoon-fed the answers usually learn very little. It's that give-a-fish-learn-to-fish thing.

I don't think I'm alone in expecting the poster to show evidence that they have exhausted their own efforts before coming here for help. Those who show that effort are much more likely to get help from us.

MrAl:
Yes i know it is an operator, but ultimately it will execute a function. I made that clear in post #6.

But in your post #6 you talked quite a bit about a non existent mod() function.
You reached an incorrect conclusion based on some false assumptions or misunderstandings.
An operator is NOT a function and a % operator in real world code is very unlikely to ever call a library function as the compiler will simply generate in line instructions.

How do you know it works with all integer types?
All integer types would be at least int, long, and long long.

Because it is part of the C language.

In my opinion this thread is going no where, and is based on incorrect assumptions based on some false information about a non existent mod() function.

And based on the OPs warped views of time, money and the value of peoples time, I'd suggest that we simply let the thread drop and move on.

--- bill

Those who show that effort are much more likely to get help from us.

Especially ones that have a better understanding of economics than your average libertarian.

An operator is NOT a function and a % operator in real world code is very unlikely to ever call a library function as the compiler will simply generate in line instructions.

You sure about that? Some operators can have very complex behavior. copy-assigning (operator =) an STL container class for example can require deallocating existing memory, reallocating new memory, and copying a lot of elements that may have their own copy constructors. Inlining all of that would be stupid, it would be made into a function call.

Operators are just functions with different syntax. What is the difference between Alpha + Bravo and plus(Alpha, Bravo)? Nothing! It is syntactic sugar, nothing but aesthetics that separates the two. This becomes obvious when you look at operator overloading. Suppose a make a fixed_point class to provide a compromise between floating point and integer math. How would I define how addition, subtraction, and multiplication work?

fixed_point operator+(const fixed_point&, const fixed_point&);
fixed_point operator-(const fixed_point&, const fixed_point&);
fixed_point operator*(const fixed_point&, const fixed_point&);
fixed_point operator/(const fixed_point&, const fixed_point&);

Why, those look suspiciously like function declarations!

The error the OP made is not in thinking of % as a function, since that is completely correct. The error was in choosing to call this function by a made-up name. It is not a "mod" function, it is the "% operator" function.

Jiggy-Ninja:
Some operators can have very complex behavior. copy-assigning (operator =)

True, but I specifically said the % operator.
And even then for the % operator only said:

is very unlikely to ever call a library function as the compiler will simply generate in line instructions.

This I would highly disagree with.

The error the OP made is not in thinking of % as a function, since that is completely correct.

An operator is not a function and does not map directly to any function since it isn't a function.
While an operator may provide a certain level of functionality it definitely is not function within the context of the actual language.

--- bill