mod() operator argument types

JosAH: Not that it matters much, but strictly speaking the C/C++ % operator does not equal the modulo function for all of its arguments; see this link: https://en.wikipedia.org/wiki/Modulo_operation

kind regards,

Jos

Hi Jos,

That's actually a good point there. It brings light to the difference between modulo and other types of operations like addition and subtraction, which are more straightforward. When we look at that page you linked to, we see a myriad of implementations and nomenclature that are varied and different, including one called "FUNCTION MOD" (har har har). That's in Cobol, which i dont think anyone uses anymore but who knows.

Thanks for the intelligent and respectful reply :-)

MrAl: So did anyone ever try using longs with % and if so, did you get the results you expected?

I tried it with both 'long' and 'long long' yesterday, and got the expected results.

MrAl: Hello,

Are you implying what i think you are implying? That no one should ever ask a question on a message board made for asking questions and sharing information?

Clearly, not what he is suggesting. I think he's just making the same point others have made: With such a simple question, why didn't you answer it yourself empirically? Type in a short test program, run it, and see if it produces the desired result. That's what both of us mean when we say you should be answering some of these questions yourself. We just don't see that effort on your part, with you preferring us to spoon feed you answers rather than you making an effort to answer them yourself.

I am using the word function because that's the way i view anything that has to provide a result from one or more operands. It wont do you any good to tell me not to do that.

Perfect! You are unteachable, so why waste your time here? Most of us here are aware of the hierarchy: operands and operators --> expressions --> statements --> function blocks --> programs. Your view thrown out most of those and says operators are the same as functions. I'll bet you're a member of the Flat Earth Society, too. Your perception of what a function is and does is simply wrong. Since you stand alone with your incorrect view of what a function is, and tell us it won't do any good for us to tell you that you are wrong, it might be best for you to just move along...clearly, there's nothing here for you to see.

@MrAl: well, there is a distinction between operators and functions:

  • operators resemble those mathematical symbols (+, -, *, / etc); functions have names;
  • operators have precedence, functions don’t;
  • operators have a fixed set of operand types; functions don’t (disregarding user defined operators in C++)

Calling something an operator or a function depends on the language being used (e.g. Lisp only has functions, FORTH only has a notion of ‘words’ etc.)

kind regards,

Jos

Accurate and precise use of terminology is necessary for accurate and precise communication. It's especially important for those among us (like me) who are less experienced, come here to learn, and are easily diverted into wild goose chases.

I hate wild goose chases.

--Michael

MrAl: Hello,

Are you implying what i think you are implying? That no one should ever ask a question on a message board made for asking questions and sharing information?

All I asked was had you tried it, so, no, I was not implying that you should not ask questions, but rather that you should share your results if you had any.

@MrAl,

I have used it on int64_t types (and smaller) and have not experienced mistakes; it does not mean that there weren’t. This however is on a PC so might not be relevant; my divisor is 188 mostly and the dividend is in that case the filesize of video files. Languages C and C#.

And just one divisor is more than likely also not relevant.

On an Uno, you can do roughly 2000 int64_t modulo calculations per second; this includes calculating expected result and testing of expected result against actual result. For you to do the math, get sufficient UNOs and test :wink:

JosAH: @MrAl: well, there is a distinction between operators and functions:

  • operators resemble those mathematical symbols (+, -, *, / etc); functions have names;
  • operators have precedence, functions don't;
  • operators have a fixed set of operand types; functions don't (disregarding user defined operators in C++)

Calling something an operator or a function depends on the language being used (e.g. Lisp only has functions, FORTH only has a notion of 'words' etc.)

kind regards,

Jos

Hi Jos,

Yes i am aware of that, and i tried to get that point across too but for some to no avail. My use of the word is more general and some people just cant accept that. I realize that some people cant think out of the box though so i will try my best to refrain from calling % a function and try to call it JUST an operator, so people here will feel more happy about it. After all, i dont want the world to end just yet because of some terminology :-)

Thanks again.

econjack: Clearly, not what he is suggesting. I think he's just making the same point others have made: With such a simple question, why didn't you answer it yourself empirically? Type in a short test program, run it, and see if it produces the desired result. That's what both of us mean when we say you should be answering some of these questions yourself. We just don't see that effort on your part, with you preferring us to spoon feed you answers rather than you making an effort to answer them yourself. Perfect! You are unteachable, so why waste your time here? Most of us here are aware of the hierarchy: operands and operators --> expressions --> statements --> function blocks --> programs. Your view thrown out most of those and says operators are the same as functions. I'll bet you're a member of the Flat Earth Society, too. Your perception of what a function is and does is simply wrong. Since you stand alone with your incorrect view of what a function is, and tell us it won't do any good for us to tell you that you are wrong, it might be best for you to just move along...clearly, there's nothing here for you to see.

Hello again,

"With such a simple question..."

I explained that side of the store several times in this thread but you either didnt read it or did not understand it. I'll say it again, but i have a feeling that will just give you more fuel to complain :-)

I stated that the 'operator' referred to as modulo was documented differently than the other similar operators, and that modulo calculations are done in a different way than other types of 'operator' calculations. There is quite a wide variety of the way it is calculated. That means that i did not think i would be able to sufficiently test the 'operator' in a reasonable amount of time, so i queried the board. If it was a simpler 'operator' then it would probably not have led to that, but because it was more complex i felt that experienced users might have a better idea what to expect than i did. So in a way that's a complement to the people on this board, that they might know something that i did not know.

Do you understand now why i did not want to test this myself?

mjward: Accurate and precise use of terminology is necessary for accurate and precise communication. It's especially important for those among us (like me) who are less experienced, come here to learn, and are easily diverted into wild goose chases.

I hate wild goose chases.

--Michael

Hi,

Apparently you dont hate them that much cause if you are implying that this is one of them then you just put yourself on it :-)

UKHeliBob: All I asked was had you tried it, so, no, I was not implying that you should not ask questions, but rather that you should share your results if you had any.

Hi,

As you can see in my other post, i believed the function was too complex to test myself so i sought after another way to find the answer.

sterretje: @MrAl,

I have used it on int64_t types (and smaller) and have not experienced mistakes; it does not mean that there weren't. This however is on a PC so might not be relevant; my divisor is 188 mostly and the dividend is in that case the filesize of video files. Languages C and C#.

And just one divisor is more than likely also not relevant.

On an Uno, you can do roughly 2000 int64_t modulo calculations per second; this includes calculating expected result and testing of expected result against actual result. For you to do the math, get sufficient UNOs and test ;)

Hello,

That's very interesting. Yeah it would take a while to fully test :-)

If i could find the source code for the 'operator' then i would be able to go over it and see how it calculated the result, but i could not find it anywhere. I thought someone here might have that information too.

What it is starting to look like is that i just create my own FUNCTION and just call that. That way it will be stable over several version changes too if any. Just to note, this would definitely not be an operator :-)

MrAl: If i could find the source code for the 'operator' then i would be able to go over it and see how it calculated the result, but i could not find it anywhere. I thought someone here might have that information too.

Create a program that makes use of '%' and compile; run avr-objdump -S sketch.ino.elf > sketch.lst and analyse the resulting assembly.

But anyway

int64_t dividend;
int64_t divisor;
int64_t result;

result = dividend / divisor;
result *= divisor;

result = dividend - result;

How difficult could it be?

MrAl: What it is starting to look like is that i just create my own FUNCTION and just call that. That way it will be stable over several version changes too if any.

Don't be an idiot.

sterretje: Create a program that makes use of '%' and compile; run avr-objdump -S sketch.ino.elf > sketch.lst and analyse the resulting assembly.

But anyway

int64_t dividend;
int64_t divisor;
int64_t result;

result = dividend / divisor; result *= divisor;

result = dividend - result;



How difficult could it be?
Don't be an idiot.

Your judgemental attitude reflects your bias and is quiet alarming. What you dont seem to realize is that when you judge someone they might judge you right back.

But anyway it's too late. It's written, im using it. Took less than 5 minutes to write. So it's done, end of story, no more replies needed. Goodbye.

Thanks to all who replied intelligently.

Thank goodness he is gone, but you all know who I thought was answering intelligently. 8^)

MrAl: Took less than 5 minutes to write.

Kinda makes one wonder why he didn't do that in the first place...

Especially since it looks like he never believed it when we said that operators can work with all integral types.

I test this function it work!

int a=4; int b=3; float c=a/b; //// c=3/4 then c=1.33

float d =(a-a%b)/b; // d=(4-4%3)/3 then c=1

it equal to d= mod(a,b) that not have this function in arduino

float c=a/b; //// c=3/4 then c=1.33

No, because the compiler will notice that a and b are integers, and do integer division on them, getting 1, and then it will convert to a float and get 1.0... This rule about using the types of the operands on the right side of the assignment frequently confuses people. and yes, a%b is the same as "a mod b" or mod(a,b)... The other thing to watch out for, if you're used to some other language, is the "^" operator. In some languages (fortran), "10^5" would be 10 to the 5th power, or 100000. In C and C++, you get 10 xor 5, which is 15.