operator definitions, modulus remainder calculation

closed account (1CfG1hU5)
where are operators defined in C/C++? in headers or compiled code?

looking for the definition of how % modulus is calulated
Last edited on
The built-in operators are defined in the source code of the compiler. It shouldn't matter how they are defined as long as they satisfy the requirements of the respective language.

built-in operator % in C++ is summarized here: http://en.cppreference.com/w/cpp/language/operator_arithmetic#Multiplicative_operators
closed account (1CfG1hU5)
is that i'm interested in seeing the define
What Cubbi is saying is that there is no "define" for the modulus operator in C++, that's part of the compiler itself.

It's like asking where is the #define or #include for the "new" keyword. There is none.

Now, if you would like to know how the modulus operator is implemented, you will need to look at the source code for a C++ compiler.
closed account (1CfG1hU5)
i saw the answer from Cubbi that the operators are defined in the source code of the compiler.
the calculation is in the compiled code or maybe a data file. and he did not say there is no define obviously. maybe you should reread his message

is not like asking for a new keyword. operators are not keyword therefore not like

do not argue if unnecessary
i know how to use % in a source file. stay with the question, do not stray
i did not ask for that
operators are like keywords in that they are implemented directly by the compiler program, and not taken from libraries.

Consider this, is there any header file you would need to include in order to get this to work:
1
2
3
4
5
int main()
{
int x = 2, y = 5, z;
z = y % x;
}

Last edited on
For example, in gcc, I have a feeling that the bulk of % logic is in expand_divmod found in expmed.c: https://gcc.gnu.org/viewcvs/gcc/trunk/gcc/expmed.c?view=markup#l3843
The mod operator is probably something like

a mod b = a - b * (a / b)
Last edited on
closed account (1CfG1hU5)
quoted from Esslercuffi, ends before edits to run:

operators are like keywords in that they are implemented directly by the compiler program, and not taken from libraries.

Consider this, is there any header file you would need to include in order to get this to work:
1
2
3
4
5
int main()
{
int x = 2, y = 5, z;
z = y % x;
}


edits to run

1
2
3
4
5
6
7
8
9
10
#include <iostream>
using namespace std;

int main()
{
int x = 2, y = 5, z;
z = y % x;
cout << "remainder is " << z;
return 0;  // because main is int type
}

or

1
2
3
4
5
6
7
8
9
#include <stdio.h>

int main()
{
int x = 2, y = 5, z;
z = y % x;
printf("remainder is %d", z);
return 0;
}

Last edited on
my point was that nothing was needed to get the math to run. <iostream> and <stdio.h> are only there to facilitate output. I was trying to show that you can do all the calculations without any libraries, thus demonstrating that they are handled intrinsically by the compiler, and not implemented in a library.
closed account (1CfG1hU5)
the declarations and assignments of variables and calc by % modulus does work without libraries.

without either library declared, some library information would be included with an exe bulid.
i see: __exitclean, __exit, __IOERR, _malloc, __setupio, _setvbuf and more.
dos language c++
Last edited on
jt1 wrote:
the declarations and assignments of variables and calc by % modulus does work without libraries.
What are you talking about? Are you telling me I need to include any arbitrary library to declare a variable? To me this is nonsensical.

The only library you even include is simply for output as mentioned by Esslercuffi. His code does compile by the way. Please click the Gear/Cog next to the code. If it doesn't compile for you, I highly suggest you invest some time and get a new compiler. Unless you are thinking it doesn't compile because there is no output?

Edit after you just did

without either library declared, some library information would be included with an exe bulid.
Would you be a little more clear on what you mean please?
Last edited on
closed account (1CfG1hU5)
tmason:
What Cubbi is saying is that there is no "define" for the modulus operator in C++, that's part of the compiler itself.

It's like asking where is the #define or #include for the "new" keyword. There is none.

Now, if you would like to know how the modulus operator is implemented, you will need to look at the source code for a C++ compiler.

jt1 response:
i was not asking for a new operator like asking for a new keyword. asked about the calc done to achieve remainder in different words. don't get hungup too much on wording. respond with an accurate c/c++ reference to subject and give calc info or web site to calc info. % is a standard operator for a long time now.

some arguments here are tiring

Esslercuffi (77)
operators are like keywords in that they are implemented directly by the compiler program, and not taken from libraries.

directly implemented yes for both.
ok everyone calm down, there may just be a bit of a language barrier that's causing some confusion. If all you're looking for is an example of the calculations done to get a modulo, Giblit had it several posts ago

The mod operator is probably something like

a mod b = a - b * (a / b)
closed account (1CfG1hU5)
yes calc info. and thx
closed account (1CfG1hU5)
giblit (3375)
jt1 wrote:
the declarations and assignments of variables and calc by % modulus does work without libraries.
What are you talking about? Are you telling me I need to include any arbitrary library to declare a variable? To me this is nonsensical.

jt1 response:
did you read the text? i said, "without libraries". subtract the arbitrary, forget the library,
and assign variables without #include. specifically without <iostream> and <stdio.h> get some glasses! and hold the nonsensical. go back to elementary school if accepted.

grumble. giblit actually responded to that.

giblit wrote:
The only library you even include is simply for output as mentioned by Esslercuffi. His code does compile by the way. Please click the Gear/Cog next to the code. If it doesn't compile for you, I highly suggest you invest some time and get a new compiler. Unless you are thinking it doesn't compile because there is no output?

Edit after you just did

without either library declared, some library information would be included with an exe bulid.
Would you be a little more clear on what you mean please?

jt1 response:
the code was run on cpp.sh by click the cog to right. the code did compile and executed
without output to screen.
Last edited on
closed account (1CfG1hU5)
giblit (3375)
The mod operator is probably something like

a mod b = a - b * (a / b)

jt1:
a = 5, b = 2
a % b = a - b * (a / b)

something like, so expected may not work.

example 1.
a % b = 5 - 2 * (2.5)
a % b = 5 - 5 = 0

removed example 2.

(a / b) guaranteed by parentheses. no 3rd possibility

result looking for is 1

for a % b

giblit, you have fix for equation?
Last edited on
Modulo only works for integers, so (a / b) is going to be truncated to an integer before it's multiplied.
closed account (1CfG1hU5)
yea right huh. yep looked up, integers only. thanks for referencing.

maybe giblit can find a correct equation.

closed account (1CfG1hU5)
giblit's equation works if:

(a / b) is rounded down to nearest integer

a mod b = a - (b * round(a / b))

example 1. works if 2.5 rounded down
a % b = 5 - (2 * (2.5))
a % b = 5 - (2 * (2))
a % b = 5 - 4 = 1 correct value

this is the equation from microsoft excel almost same except for variables to
giblet's original equation.
n - d is number - divisor
INT is the equation that rounds down (n/d) to integer

n - d*INT(n/d)
n - (d * rounded(n/d))
98 - (6 * rounded(98/6)
98 - (6 * 16)
98 - 96 = 2 correct answer
Last edited on
Topic archived. No new replies allowed.