operator definitions, modulus remainder calculation

Oct 18, 2014 at 5:56pm
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 Oct 19, 2014 at 3:10am
Oct 18, 2014 at 6:10pm
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
Oct 18, 2014 at 6:34pm
closed account (1CfG1hU5)
is that i'm interested in seeing the define
Oct 18, 2014 at 7:28pm
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.
Oct 18, 2014 at 8:18pm
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
Oct 18, 2014 at 8:21pm
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 Oct 18, 2014 at 8:24pm
Oct 18, 2014 at 8:34pm
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
Oct 18, 2014 at 8:42pm
The mod operator is probably something like

a mod b = a - b * (a / b)
Last edited on Oct 18, 2014 at 8:46pm
Oct 18, 2014 at 9:04pm
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 Oct 18, 2014 at 9:08pm
Oct 18, 2014 at 9:08pm
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.
Oct 18, 2014 at 9:26pm
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 Oct 18, 2014 at 9:33pm
Oct 18, 2014 at 9:31pm
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 Oct 18, 2014 at 9:32pm
Oct 18, 2014 at 9:44pm
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.
Oct 18, 2014 at 9:47pm
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)
Oct 18, 2014 at 9:50pm
closed account (1CfG1hU5)
yes calc info. and thx
Oct 18, 2014 at 10:04pm
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 Oct 18, 2014 at 10:13pm
Oct 19, 2014 at 12:35am
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 Oct 19, 2014 at 7:59am
Oct 19, 2014 at 12:43am
Modulo only works for integers, so (a / b) is going to be truncated to an integer before it's multiplied.
Oct 19, 2014 at 12:48am
closed account (1CfG1hU5)
yea right huh. yep looked up, integers only. thanks for referencing.

maybe giblit can find a correct equation.

Oct 19, 2014 at 1:51am
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 Oct 19, 2014 at 2:14am
Topic archived. No new replies allowed.