C deep section “take mold and round”

Time:2022-1-14

catalogue

1: Four rounding

1.1 rounding in 0 direction

1.2 floor rounding (- ∞ rounding)

1.3. + ∞ rounding

1.4 rounding

1.5 summary of examples

2: Mold taking / residue taking

2.1 concept

2.2. Examples (C and python)

2.3. Is residue taking the same as mold taking?

2.4. The calculation data is the same as the symbol

2.5 different symbols of calculation data

2.6 summary


1: Four rounding

1.1 rounding in 0 direction

Look at the code:

#include <stdio.h>
int main()
{
	//The essence is to round 0
	int i = -2.9;
	int j = 2.9;
	printf("%d\n", i); // The result is: - 2
	printf("%d\n", j); // The result is: 2
    printf("%d %d\n", 5 / 2, -5 / 2);  // 2 -2
	return 0;
}

Rounding in 0 direction is the default in C, and the drawing explains rounding in 0 direction:

C deep section

There is a TRUNC function in C language, which also uses 0-direction rounding. Note that the reference header file #include < math h>. Examples are as follows:

#include <stdio.h>
#include<math.h>
int main()
{
	int i = -2.9;
	int j = 2.9;
	printf("%d\n", i); // The result is: - 2
	printf("%d\n", j); // The result is: 2
	printf("%d\n", (int)trunc(2.9)); // The result is: 2 // the result is: - 2
	printf("%d\n", (int)trunc(-2.9));// The result is: - 2
	return 0;
}

1.2, -∞ rounding (floor rounding)

In fact, the floor function is used, which refers to #include < math like TRUNC above h>

Look at the code:

#include<stdio.h>
#include<math.h>
int main()
{
	printf("%.1f\n", floor(-2.9)); // -3.0
	printf("%.1f\n", floor(-2.1)); // -3.0
	printf("%.1f\n", floor(2.9));  // 2.0
	printf("%.1f\n", floor(2.1));  // 2.0
	return 0;
}

Drawing interpretation – ∞ rounding

C deep section

1.3. + ∞ rounding

Ceil function is used, which also needs #include < math h>

Look at the code:

#include<stdio.h>
#include<math.h>
int main()
{
	printf("%.1f\n", ceil(-2.9));  // -2.0
	printf("%.1f\n", ceil(-2.1));  // -2.0
	printf("%.1f\n", ceil(2.9));   // 3.0
	printf("%.1f\n", ceil(2.1));   // 3.0
	return 0;
}

Drawing interpretation + ∞ rounding:

C deep section

1.4 rounding

The use of the round function also requires #include < math h>

Look at the code:

#include<stdio.h>
#include<math.h>
int main()
{
	printf("%.1f\n", round(-2.9)); // -3.0
	printf("%.1f\n", round(-2.1)); // -2.0
	printf("%.1f\n", round(2.9));  // 3.0
	printf("%.1f\n", round(2.1));  // 2.0
}

1.5 summary of examples

#include <stdio.h>
#include <math.h>
int main()
{
	const char* format = "%.1f \t%.1f \t%.1f \t%.1f \t%.1f\n";
	printf("value\tround\tfloor\tceil\ttrunc\n");
	printf("-----\t-----\t-----\t----\t-----\n");
	printf(format, 2.3, round(2.3), floor(2.3), ceil(2.3), trunc(2.3));
	printf(format, 3.8, round(3.8), floor(3.8), ceil(3.8), trunc(3.8));
	printf(format, 5.5, round(5.5), floor(5.5), ceil(5.5), trunc(5.5));
	printf(format, -2.3, round(-2.3), floor(-2.3), ceil(-2.3), trunc(-2.3));
	printf(format, -3.8, round(-3.8), floor(-3.8), ceil(-3.8), trunc(-3.8));
	printf(format, -5.5, round(-5.5), floor(-5.5), ceil(-5.5), trunc(-5.5));
	return 0;
}

C deep section

Conclusion:

Floating point numbers (integers / integers) have many rounding methods.

2: Mold taking / residue taking

2.1 concept

If a and D are two natural numbers and D is non-zero, it can be proved that there are two unique integers Q and R, which satisfy a = Q * D + R and 0 ≤ r < D. Among them, q is called quotient and R is called remainder.

2.2. Examples (C and python)

#include <stdio.h>
int main()
{
	int a = 10;
	int d = 3;
	printf("%d\n", a % d); // The result is 1
	//Because: a = 10, d = 3, q = 3, r = 1, 0 < = R < D (3)
	//So: a = Q * D + R - > 10 = 3 * 3 + 1

	int m = -10;
	int n = 3;
	printf("%d\n", m / n); // C language is - 3, very easy to understand
	printf("%d\n", m % n); // C language is - 1

	return 0;
}

stayC languageWe are right-10/3=-3as well as-10%3=-1Well understood, no dispute

And inPythonIn,-10 / 3 = – 4, and-10% 3 = 2

Why?

Conclusion: obviously, the above definition of modulo can not meet the modulo operation in language

Resolution:

Because in C, now – 10% 3 has a negative number. According to the definition, if a = Q * D + R and 0 ≤ r < D are satisfied, the remainder in C language does not meet the definition, because r < 0.

Therefore, we have a revised definition of modulus: if a and D are two natural numbers and D is non-zero, it can be proved that there are two unique integers Q and R, which satisfy a = Q * D + R, q is an integer, and 0 ≤| R | < | D |. Among them, q is called quotient and R is called remainder.

With this new definition, the “modulo” in C or Python can be explained.

Interpretation C: – 10 = (- 3) * 3 + (- 1)

Explain Python: – 10 = (?)* 3 + 2, where, can be pushed out, ‘?’ Must be – 4 (to be verified later) That is – 10 = (- 4) * 3 + 2 to meet the definition.

Therefore, in different languages, for the same calculation expression, the negative “modulus” results are different. We can call it positive remainder and negative remainder, respectively

What determines this phenomenon?

As can be seen from the above example, the size of the specific remainder r essentially depends on the quotient Q.

And who does the business depend on? Depending on the rounding rule when dividing.

2.3. Is residue taking the same as mold taking?

Careful students should see that my molds are taken with “”. It shows that these two are not strictly equivalent (although they are almost the same in most cases). We should calculate the quotient before we can get the remainder.

Essence 1 rounding:

Balance taking: let the quotient as much as possible and round to 0.

Take the mold: let the quotient as much as possible and round it in the – ∞ direction.

Therefore:

The essence of% in C is actually remainder.

The% essence of Python is to take modules.

Understanding chain:

For any number greater than 0, round it in the 0 direction and – ∞ direction, and the rounding direction is the same. Therefore, taking modulus is equivalent to taking remainder. For any number less than 0, it is rounded in the direction of 0 and – ∞, and the rounding direction is opposite. Therefore, modulus is not equivalent to remainder

The quotient obtained by dividing with symbolic data must be a positive number (positive number vs positive integer), that is, greater than 0! Therefore, when rounding its quotient, modulus is equivalent to remainder.

Essence 2 Symbol:

If the two data participating in the remainder are of the same sign, modulo is equivalent to remainder

2.4. The calculation data is the same as the symbol

Look at the code:

#include <stdio.h>
int main()
{
	printf("%d\n", 10 / 3);  // 3
	printf("%d\n\n", 10 % 3);// 1
	printf("%d\n", -10 / -3); // 3
	printf("%d\n\n", -10 % -3); // -1
	return 0;
}

In Python, we found that the running result is the same as above.

Conclusion:Through the comparative test, it is more verified that if the two data participating in the remainder are of the same symbol, modulo is equivalent to remainder

2.5 different symbols of calculation data

Look at the code:

#include <stdio.h>
int main()
{
	printf("%d\n", -10 / 3); // Results: - 3
	printf("%d\n\n", -10 % 3); // Results: - 1 why- 10=(-3)*3+(-1)
	printf("%d\n", 10 / -3); // Results: - 3
	printf("%d\n\n", 10 % -3); // Results: 1 why? 10=(-3)*(-3)+1
	return 0;
}

In Python, this is not the case.

Obvious conclusion:If there are different symbols, refer to the previous definition for the remainder. The remainder sign is the same as the divisor.

Why?

Look at the definition again: if a and D are two natural numbers and D is non-zero, it can be proved that there are two unique integers Q and R, which satisfy a = Q * D + R, q is an integer, and 0 ≤| R | < | D |. Where q is called quotient and R is called remainder

A = Q * D + R to r = a – Q * D to r = a + (- Q * d)

For such an expression: x = y + Z, the sign of X is consistent with the large data in | y |, | Z |

In R = a + (- Q * d), the absolute value of | a | and | – Q * D | depends on the rounding method of quotient Q.

C is rounded to 0, that is, the absolute value of Q itself is reduced. For example:

-10 / 3 = -3.333.33 rounded to 0 -3.33 a=-10 |10|, -q*d=-(-3)*3=9 |9|

10 / – 3 = -3.333.33 rounded to 0 -3.33 a=10 |10|, -q*d=-(-3)*(-3)=-9 |9|

The absolute values are getting smaller

Python is rounded to – ∞, that is, the absolute value of Q itself increases.

-10 / 3 = -3.333.33 ‘/ /’ rounded to – ∞ – 4 a=-10 |10|, -q*d=-(-4)*3=12 |12|

10 / – 3 = –3.333.33 ‘/ /’ rounded to – ∞ – 4 a=10 |10|, -q*d=-(-4)*(-3)=-12 |12|

The absolute values are getting bigger

Conclusion: if the two data symbols involved in the remainder are different, in C language (or other languages that use rounding to 0, such as C + +, Java), the remainder symbol is the same as the divisor.

2.6 summary

(1) Floating point numbers (or integer division) have many rounding methods.

(2) If a and D are two natural numbers and D is non-zero, it can be proved that there are two unique integers Q and R, satisfying that a = Q * D + R, q is an integer, and 0 ≤| R | < | D |. Among them, q is called quotient and R is called remainder.

(3) In different languages, the results of “modulo” are different for the same calculation expression. We can call it positive remainder and negative remainder, respectively

(4) The size of the specific remainder r essentially depends on the quotient Q. The quotient depends on the rounding rule when dividing.

(5) Remainder vs modulus: the remainder shall be rounded to 0 by quotient as much as possible. The modulus shall be rounded in the direction of – ∞ as far as possible.

(6) If the two data participating in the remainder are of the same sign, modulo is equivalent to remainder

(7) If the two data symbols involved in the remainder are different, in C language (or other languages that use rounding to 0, such as C + +, Java), the remainder symbol is the same as the divisor. (because of rounding to 0)

Recommended Today

Springboot flowable workflow-2 (code integration)

1. Preface Last blog【Springboot integrates flowable workflow-1 (drawing process definition)】This paper draws a simple flow chart with flowable UI. This blog will introduce the code integration part, which mainly includes: [publish process definition], [open process task], [get user task], [user approval task], [add approval opinion], [get flow chart], [get my to-do task], [get my initiated […]