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

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))?

Related

Round division of unsigned integers with no overflow

I'm looking for an overflow-safe method to perform round division of unsigned integers.
I have this:
uint roundDiv(uint n, uint d)
{
return (n + d / 2) / d;
}
But unfortunately, the expression n + d / 2 may overflow.
I think that I will have to check whether or not n % d is smaller than d / 2.
But d / 2 itself may truncate (when d is odd).
So I figured I should check whether or not n % d * 2 is smaller than d.
Or even without a logical condition, rely on the fact that n % d * 2 / d is either 0 or 1:
uint roundDiv(uint n, uint d)
{
return n / d + n % d * 2 / d;
}
This works well, however once again, n % d * 2 may overflow.
Is there any custom way to achieve round integer division which is overflow-safe?
Update
I have come up with this:
uint roundDiv(uint n, uint d)
{
if (n % d < (d + d % 2) / 2)
return n / d;
return n / d + 1;
}
Still, the expression d + d % 2 may overflow.
return n/d + (d-d/2 <= n%d);
The way to avoid overflow at any stage is, as OP stated, to compare the remainder with half the divisor, but the result isn't quite as obvious as it first seems. Here are some examples, with the assumption that 0.5 would round up. First with an odd divisor:
Numerator Divisor Required Quotient Remainder Half divisor Quot < Req?
3 3 1 1 0 1 no
4 3 1 1 1 1 no
5 3 2 1 2 1 yes
6 3 2 2 0 1 no
Above, the only increment needed is when d / 2 < remainder. Now with an even divisor:
Numerator Divisor Required Quotient Remainder Half divisor Quot < Req?
4 4 1 1 0 2 no
5 4 1 1 1 2 no
6 4 2 1 2 2 yes
7 4 2 1 3 2 yes
8 4 2 2 0 2 no
But here, the increment is needed when d / 2 <= remainder.
Summary:
You need a different condition depending on odd or even divisor.

Generate random even number in range [m, n]

I was looking for C code to generate a set of random even number in range [start, end]. I tried,
int random = ((start + rand() % (end - start) / 2)) * 2;
This won't work, for example if the range is [0, 4], both 0 & 4 included
int random = (0 + rand() % (4 - 0) / 2) * 2
=> (rand() % 2) * 2
=> 0, 2, ... (never includes 4) but expectation = 0, 2, 4 ...
On the other hands if I use,
int random = ((start + rand() % (end - start) / 2) + 1) * 2;
This won't work, for example,
int random = (0 + (rand() % (4 - 0) / 2) + 1) * 2
=> ((rand() % 4 / 2) + 1) * 2
=> 2, 4, ... (never includes 0) but expectation = 0, 2, 4 ...
Any clue? how to get rid of this problem?
You complicated it too much. Since you're using rand() and the modulo operator, I'm assuming that you will not be using this for cryptographic or security purposes, but as a simple even number generator.
The formula I have found for generating a random even number in the range of [0, 2n] is to use
s = (rand() % (n + 1)) * 2
An example code:
#include <stdio.h>
int main() {
int i, s;
for(i = 0; i < 100; i++) {
s = (rand() % 3) * 2;
printf("%d ", s);
}
}
And it gave me the following output:
2 2 0 2 4 2 2 0 0 2 4 2 4 2 4 2 0 0 2 2 4 4 0 0 4 4 4 2 2 2 4 0 0 0 4 0 2 2 2 2 0 0 0 4 4 2 4 4 4 0 4 2 2 4 4 0 4 4 2 2 0 0 4 0 4 4 2 0 2 4 0 0 0 0 4 0 4 4 0 4 2 0 0 4 4 0 0 4 4 2 0 0 4 0 2 2 2 0 0 4 0 2 4 2
Best regards!
rand() % x will generate a number in the range [0,x) so if you want the range [0,x] then use rand() % (x+1)
Common notation for ranges is to use [] for inclusive and () for exclusive, so [a,b) would be a range such that a is included but not b.
So in your case, just use (rand() % 3)*2 to get random numbers among {0,2,4}
If you want even numbers in the range [m,n], then use ((m/2) + rand() % ((n-m+2)/2))*2
I do not trust in the mod operator for random numbers. I prefer
start + ((1 + stop - start) * rand())
/ (1 + RAND_MAX)
which only relies on the distribution of rand() in the interval
[0, .. , RAND_MAX] and not on any distribution of rand()%n in the
interval [0, .. , n-1].
Note: If you use this expression you should add appropriate casts to avoid multiplication overflow.
Note also
ISO/IEC 9899:201x (p.346):
There are no guarantees as to the quality of the random sequence produced and some implementations are known to produce sequences with distressingly non-random low-order bits. Applications with particular requirements should use a generator that is known to be sufficient for their needs.
Just and-out the low bit, which makes it even:
n= (rand()%N)&(-2);
and to use a start/stop (a range), the values can be offset:
int n, start= 5, stop= 20+1;
n= ((rand()%(stop-start))+start)&(-2);
The latter calculation generates a random number between 0 and RAND_MAX (this value is library-dependent, but is guaranteed to be at least 32767).
If the stop value must be included in the range of generated numbers, then add 1 to the stop value.
It takes that value modulo the stop value plus the start value, and then adds the start value. The value is now within the range of [start, stop]. As only even numbers are required, the low bit is anded-out because even numbers start at 2.
The anding-out is performed by generating a mask of all 1's, except the lowest bit. As -1 is all 1's (0xFFF...FFFFF), -2 is all 1's except this low bit (0xFFF...FFFFE). Next the bitwise AND operation (&) is perfomed with this mask and the number is now in the range [start,stop]. QED.

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

Remainder operator in c89 and c99

c99 standard says that result of modulo operation has same sign as first operand. So -9 % 7 = -2 and 9 % -7 = 2.
I read in one book that c89 standard depends on implementation. So -9 % 7 could yield -2 or 5??? How remainder of -9 / 7 could be 5?
Consider two numbers a and b.
The quotient q=a/b and remainder r=a%b satisfy the equation a == q*b + r.
An (hypothetical) implementation of C89 in which -9 % 7 produces 5 is an implementation in which -9 / 7 is computed as -2.
The mathematical (Euclidian) division constrains r to be positive and smaller than b. C99 constrains it to be of the same sign as a and strictly between -b and b.
It is all only a matter of convention.
% operator is defined as:
a == (a / b * b) + a % b
so
a % b = a - (a / b * b)
% as a remainder operator
If / rounds towards 0 (like C99):
-9 % 7 == -2
you have -9 / 7 == -1 so the % is -2 because
-9 % 7 == -9 - (-9 / 7 * 7) + 9 == -9 + 7 == -2 
% as a modulo operator
If / rounds towards minus infinity:
-9 % 7 == 5
you have -9 / 7 == -2 so the % is 5
-9 % 7 == -9 - (-9 / 7 * 7) + 9 == -9 + 14 == 5

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.

Resources