C arithmetic precedence - c

I stumbled upon a question about arithmetic precedence in a test and I cannot wrap my head at all around its answer.
float x = 5 % 3 * + 2 - 4.5 / 5 * 2 + 2;
My "understanding" right now is that multiplication must take place first before division and modulus, yet when I try using that approach, the answer is 6.55 instead of 4.20. I tried playing around with the expression (adding brackets here and there), and it turns out that 5 % 3 takes place first before everything else. I just don't understand why since, according to the precedence table I was provided, that shouldn't be the case. Could someone clear this up for me?

Please refer to the documentation here.
The precedence of multiplication, division and remainder operators are higher than those of sum and subtraction.
When multiplication, division or remainder operators go one after another, then they are left-associated, meaning they will be executed one by one in the given order.
In your example 5 % 3 will be performed first, then the multiplication (by whatever number there is), then the division 4.5 / 5, then multiplication of the result by 2, and only after all that will the sum and subtraction be performed.

Your C code:
x = 5 % 3 * + 2 - 4.5 / 5 * 2 + 2;
First, unary plus and unary minus has the highest precedence:
x = 5 % 3 * (+ 2) - 4.5 / 5 * 2 + 2;
Second, multiplication, division, and remainder have the same precedence, associated from left to right:
x = ((5 % 3) * (+ 2)) - ((4.5 / 5) * 2) + 2;
Last, addition and subtraction have the same precedence, associated from left to right:
x = ((((5 % 3) * (+ 2)) - ((4.5 / 5) * 2)) + 2);
Now we evaluate the expression:
x = (((2 * (+ 2)) - ((4.5 / 5) * 2)) + 2);
x = (((2 * 2) - ((4.5 / 5) * 2)) + 2);
x = ((4 - ((4.5 / 5) * 2)) + 2);
x = ((4 - (0.9 * 2)) + 2);
x = ((4 - 1.8) + 2);
x = (2.2 + 2);
x = 4.2;

you can refers this link for more detail
http://www.difranco.net/compsci/C_Operator_Precedence_Table.htm

Related

base-2: why DECIMAL_DIG - DIG == 2 or 3?

base-2: why DECIMAL_DIG - DIG == 2 or 3?
Examples:
FLT_DECIMAL_DIG - FLT_DIG == 9 - 6 == 3
DBL_DECIMAL_DIG - DBL_DIG == 17 - 15 == 2
LDBL_DECIMAL_DIG - LDBL_DIG == 21 - 18 == 3
FLT16_DECIMAL_DIG - FLT16_DIG == 5 - 3 == 2
Extra: Is it guaranteed that on any given implementation using base-2 this difference will be 2 or 3?
Yes, for base-2 radix, xxx_DECIMAL_DIG - xxx_DIG will be 2 or 3.
Informal proof
For a floating point type:
b is the base or radix or exponent representation (an integer > 1)
p is the precision (the number of base-b digits in the significand) (I assume this is > 1 and rational)
The xxx_DECIMAL_DIG values are defined as:
⎰ p log10 b — if b is a power of 10
⎱ ⌈1 + p log10 b⌉ — otherwise
The xxx_DIG values are defined as:
⎰ p log10 b — if b is a power of 10
⎱ ⌊(p - 1) log10 b⌋ — otherwise
For b = 2, log10 b ≈ 0.301 and is irrational (proof).
∴ p log10 2 is irrational (since p is rational and > 1).
∴ ⌈p log10 2⌉ - ⌊p log10 2⌋ = 1.
∴ ⌈1 + p log10 2⌉ - ⌊p log10 2⌋ = 2. — ①
⌊p log10 2⌋ - ⌊(p - 1) log10 2⌋ ∈ {0, 1}. — ② (since 0 < log10 2 < 1)
∴ ⌈1 + p log10 2⌉ - ⌊(p - 1) log10 2⌋ ∈ {2, 3}. — (from ① and ②) ∎
Given definitions of xxx_DECIMAL_DIG and xxx_DIG we have
2 <= ceil(1+p*log10(2)) - floor((p-1)*log10(2)) <= 3
Simplifying:
2 <= ceil(1+p*log10(2)) - floor((p-1)*log10(2)) <= 3
2 <= 1 + ceil( p*log10(2)) - floor((p-1)*log10(2)) <= 3
let q = p – 1
2 <= 1 + ceil( (1 + q) *log10(2)) - floor((p-1)*log10(2)) <= 3
2 <= 1 + ceil(log10(2) + q*log10(2)) - floor( q *log10(2)) <= 3
let R = trunc(q*log10(2))
let r = trunc(q*log10(2)) – R, r is [0.0 …1.0)
log10(2) = 0.301…
2 <= (1 + R + ceil(0.301… + r)) – (R + floor(r)) <= 3
2 <= 1 + ceil(0.301… + r) – floor(r) <= 3
2 <= 1 + ceil(0.301… + r) – 0 <= 3
When r <= 1.0 - 0.301…
2 <= 1 + 1 <= 3
Otherwise r > 1.0 - 0.301…
2 <= 1 + 2 <= 3
Even though it is shown that the difference is 2 or 3, I suppose the next questions are why are
xxx_DECIMAL_DIG: ceil(1+p*log10(2))
and
xxx_DIG: floor((p-1)*log10(2))?

Understanding the math behind the code

I have a really basic and simple question but I am having problems with understanding this C code.
#define POLYNOMIAL(x) \
(((((3.0 * (x) + 2.0) * (x) - 5.0) * (x) - 1.0) * (x) + 7.0) * (x) - 6.0)
This definition is for this polynomial: 3x5+2x4-5x3-x2+7x-6
How can I convert this polynomial into the form shown in the #define? Is there any trick for this?
Your polynomial:
3x5 + 2x4 - 5x3 - x2 + 7x - 6
Can be rewritten successively:
(3x4 + 2x3 - 5x2 - x + 7) · x - 6
((3x3 + 2x2 - 5x - 1) · x + 7) · x - 6
(((3x2 + 2x - 5) · x - 1) · x + 7) · x - 6
((((3x + 2) · x - 5) · x - 1) · x + 7) · x - 6
This an expanded, or unrolled, Horner's Method loop. If the coefficients were expressed as an array:
double polynomial[] = { -6, 7, -1, -5, 2, 3 };
Then, the polynomial could be evaluated with this function:
double horners (double poly[], int terms, double x) {
double result = 0;
while (terms--) {
result = result * x + poly[terms];
}
return result;
}
Just add parenthesis and decrease out powers inside until you get to the last one, like this:
(3x^5)+(2x^4)-(5x^3)-(x^2)+7x-6
((3x^4)+(2x^3)-(5x^2)-x+7)x-6
(((3x^3)+(2x^2)-5x-1)x+7)x-6
((((3x^2)+2x-5)x-1)x+7)x-6
((((3x+2)x-5)x-1)x+7)x-6

Understanding CEILING macro use cases

I've found the following macro in a utility header in our codebase:
#define CEILING(x,y) (((x) + (y) - 1) / (y))
Which (with help from this answer) I've parsed as:
// Return the smallest multiple N of y such that:
// x <= y * N
But, no matter how much I stare at how this macro is used in our codebase, I can't understand the value of such an operation. None of the usages are commented, which seems to indicate it is something obvious.
Can anyone offer an English explanation of a use-case for this macro? It's probably blindingly obvious, I just can't see it...
Say you want to allocate memory in chunks (think: cache lines, disk sectors); how much memory will it take to hold an integral number of chunks that will contain the X bytes? If the chuck size is Y, then the answer is: CEILING(X,Y)
When you use an integer division in C like this
y = a / b
you get a result of division rounded towards zero, i.e. 5 / 2 == 2, -5 / 2 == -2. Sometimes it's desirable to round it another way so that 5 / 2 == 3, for example, if you want to take minimal integer array size to hold n bytes, you would want n / sizeof(int) rounded up, because you want space to hold that extra bytes.
So this macro does exactly this: CEILING(5,2) == 3, but note that it works for positive y only, so be careful.
Hmm... English example... You can only buy bananas in bunches of 5. You have 47 people who want a banana. How many bunches do you need? Answer = CEILING(47,5) = ((47 + 5) - 1) / 5 = 51 / 5 = 10 (dropping the remainder - integer division).
Let's try some test values
CEILING(6, 3) = (6 + 3 -1) / 3 = 8 / 3 = 2 // integer division
CEILING(7, 3) = (7 + 3 -1) / 3 = 9 / 3 = 3
CEILING(8, 3) = (8 + 3 -1) / 3 = 10 / 3 = 3
CEILING(9, 3) = (9 + 3 -1) / 3 = 11 / 3 = 3
CEILING(10, 3) = (9 + 3 -1) / 3 = 12 / 3 = 4
As you see, the result of the macro is an integer, the smallest possible z which satisfies: z * y >= x.
We can try with symbolics, as well:
CEILING(k*y, y) = (k*y + y -1) / y = ((k+1)*y - 1) / y = k
CEILING(k*y + 1, y) = ((k*y + 1) + y -1) / y = ((k+1)*y) / y = k + 1
CEILING(k*y + 2, y) = ((k*y + 2) + y -1) / y = ((k+1)*y + 1) / y = k + 1
....
CEILING(k*y + y - 1, y) = ((k*y + y - 1) + y -1) / y = ((k+1)*y + y - 2) / y = k + 1
CEILING(k*y + y, y) = ((k*y + y) + y -1) / y = ((k+1)*y + y - 1) / y = k + 1
CEILING(k*y + y + 1, y) = ((k*y + y + 1) + y -1) / y = ((k+2)*y) / y = k + 2
You canuse this to allocate memory with a size multiple of a constant, to determine how many tiles are needed to fill a screen, etc.
Watch out, though. This works only for positive y.
Hope it helps.
CEILING(x,y) gives you, assuming y > 0, the ceiling of x/y (mathematical division). One use case for that would be a prime sieve starting at an offset x, where you'd mark all multiples of the prime y in the sieve range as composites.

round to the nearest multiple of 1/16 in C

Please let me know how to round a decimal number like 0.53124 to a nearest multiple of 1/16 which is 0.5. And similarly, when you round 0.46875 we must get 0.5. Thanks
floor(0.53124 * 16 + 0.5) / 16
floor(0.46875 * 16 + 0.5) / 16
floor(x * 16 + 0.5) / 16
I suppose, that you can multiply by 16, call round(double x) and divide by 16. noob code:
double x;
x=x*16;
x=round(x);
x=x/16;
and the one line code:
double x;
x=round(x*16)/16;
C Code:
answer = (int) ((x + 1.0/32.0) * 16) / 16.0;
Python verification:
>>> int(((.53124 + 1.0/32) * 16)) / 16.0
0.5
>>> int(((.46875 + 1.0/32) * 16)) / 16.0
0.5
>>>

How does modulus of a smaller dividend and larger divisor work?

7 % 3 = 1 (remainder 1)
how does
3 % 7 (remainder ?)
work?
remainder of 3/7 is 3..since it went 0 times with 3 remainder so 3%7 = 3
7 goes into 3? zero times with 3 left over.
quotient is zero. Remainder (modulus) is 3.
Conceptually, I think of it this way. By definition, your dividend must be equal to (quotient * divisor) + modulus
Or, solving for modulus: modulus = dividend - (quotient * divisor)
Whenever the dividend is less than the divisor, the quotient is always zero which results in the modulus simply being equal to the dividend.
To illustrate with OP's values:
modulus of 3 and 7 = 3 - (0 * 7) = 3
To illustrate with other values:
1 % 3:
1 - (0 * 3) = 1
2 % 3:
2 - (0 * 3) = 2
The same way. The quotient is 0 (3 / 7 with fractional part discarded). The remainder then satisfies:
(a / b) * b + (a % b) = a
(3 / 7) * 7 + (3 % 7) = 3
0 * 7 + (3 % 7) = 3
(3 % 7) = 3
This is defined in C99 §6.5.5, Multiplicative operators.
7 divided by 3 is 2 with a remainder of 1
3 divided by 7 is 0 with a remainder of 3
As long as they're both positive, the remainder will be equal to the dividend. If one or both is negative, then you get reminded that % is really the remainder operator, not the modulus operator. A modulus will always be positive, but a remainder can be negative.
(7 * 0) + 3 = 3; therefore, the remainder is 3.
a % q = r means there is a x so that q * x + r = a.
So, 7 % 3 = 1 because 3 * 2 + 1 = 7,
and 3 % 7 = 3 because 7 * 0 + 3 = 3
It seems you forgot to mention the surprising case, if the divident is smaller and negative:
-3 % 7
result: 4
For my brain to understand this kind of question, I'm always converting it to a real world object, for example if I convert your question 3 % 7. I'm going to represent the "3" as a 3-inch wide metal hole, then the "7" as a 7 inch metal screw. Can you insert the 7 inch metal screw to a 3 inch wide metal hole? Of course not, therefore the answer should be the 3-inch metal hole, it doesn't matter even let say you have a 1000 or a million inch wide screw, it is still 3, because how many times can you insert the 1000 or a million inch wide screw to a 3 inch wide metal hole? Zero times, right?
The most simple and effective catch to remember would be:
Whenever dividend is less than the divisor, modulus is just that dividend.
Let's formulate this:
if x < y, then x % y = x

Resources