size_t i = size;
while(i-- > 0){
// loop body, possibly break
}
use(i); // i wraps below 0
size_t i = size;
while(i > 0){
i--;
// loop body, possibly break
}
use(i); // i doesn't wrap
Practically, i leaves the for-loop scope, so most never encounter this problem. Ensure y+x ≤ INT_MAX.
If x ≤ UINT_MAX-y, then x+y evaluates correctly:
∀x∀y(x ≤ UINT_MAX-y → ∃z(z = y+x))
Ensure y-x ≤ INT_MAX.
If x≤y, then y-x evaluates correctly:
∀x∀y(x≤y → ∃z(z = y-x))
The safety conditions for signed arithmetic: Ensure INT_MIN ≤ y+x and y+x ≤ INT_MAX.
To avoid overflow or underflow, first compare x to 0.
In the case x≤0, INT_MIN-x cannot underflow, and y+x cannot overflow. If y compares greater than INT_MIN-x, then y+x evaluates correctly.
In the case 0≤x, then INT_MAX-x cannot overflow, and y+x cannot underflow. And if y compares less than INT_MAX-x, then y+x evaluates correctly.
∀x∀y((x≤0 ∧ INT_MIN-x≤y)∨(0≤x ∧ y≤INT_MAX-x) → ∃z(z = y-x))
Ensure INT_MIN ≤ y-x and y-x ≤ INT_MAX.
To avoid overflow or underflow, first compare x to 0.
In the case 0≤x, INT_MIN+x cannot underflow, and y-x cannot overflow. If y compares greater than INT_MIN+x, then y-x evaluates correctly.
In the case x≤0, INT_MAX+x cannot overflow, and y-x cannot underflow. If y compares less than INT_MAX+x, then y-x evaluates correctly.
∀x∀y((0≤x ∧ INT_MIN-x≤y)∨(x≤0 ∧ y≤INT_MAX+x) → ∃z(z = y-x))
The programmers that prefer unsigned arithmetic intuitively feel the greater simplicity compared to signed integers, but without any theorem proving, I agree that your assumption of small integers strongly supports signed integers. for(size_t i = 0; i < size; i++){
// loop body
}
for(size_t i = size; i > 0;){ i--;
// loop body
} dB_P = log(10)/10
dB_F = log(10)/20
log(10*V) = log(V) + 20*dB_F // the level of 10 V equals 20 dB more than the power level of 1 V.
SPL = 20*10^-6 * Pa
hearing_damage = log(SPL) + 90*dB_F // hearing damage occurs over 90 dB_F above SPL (neglecting A-weighting)
pow(hearing_damage) = pow(log(SPL) + 90*dB_F))
pow(hearing_damage) = pow(log(SPL) + 90*log(10)/20))
pow(hearing_damage) = SPL*pow(90*log(10)/20))
pow(hearing_damage) = SPL*31622.7766 // the pressure of hearing damage occurs above 31622 times SPL
pow(hearing_damage) = 0.632455532 Pa // the pressure of hearing damage occurs above 0.632 Pa
Very helpful!! Imagine combining the goofy list of decibel suffixes into a uniform notation. Write the logarithm first so the + or - stays in the same spot. log(reference_unit) + value*dB_F (or dB_P)
log(reference_unit) - value*dB_F (or dB_P)
https://en.wikipedia.org/wiki/Decibel#List_of_suffixes log(freq) = pitch
freq = pow(pitch)
octave = log(2)
400*Hz = 100*Hz*4 // the frequency 400 Hz equals 4 times 100 Hz
log(400*Hz) = log(100*Hz) + log(4)
log(400*Hz) = log(100*Hz) + 2*log(2)
log(400*Hz) = log(100*Hz) + 2*octave
log(400*Hz) = log(100*Hz) + 2*octave // the pitch of 400 Hz equals 2 octaves above the pitch of 100 Hz
cent = log(2)/1200
A4 = log(440*Hz)
B4 = A4 + 200*cent // the pitch B4 equals 200 cents above A4
B4 = log(440*Hz) + 200*log(2)/1200
B4 = log(440*Hz) + log(2^(2/12))
B4 = log(440*Hz * 2^(2/12))
pow(B4) = 493.883 Hz // the frequency of B4 equals 493.883 Hz
I like the intuition that baseless logarithm notation gives, and it also avoids needing to choose a specific reference point. I can also directly calculate by choosing an arbitrary base: pow(log(440*Hz) + 200*log(2)/1200)
exp(ln(440) + 200*ln(2)/1200) import ln, floor
define log := (num,base) -> ln(num) / ln(base)
define digits := (num,base) -> floor(log(num,base) + 1)
define tobits := (base) -> log(base,2)
define infocost := (num,base) -> digits(num,base) * tobits(base)
define infocost_wikipedia := (num,base) -> digits(num,base) * base
define infocost_tbwtc := (num, base) -> (digits(num,base) - 1) * tobits(base) + tobits(base- 1)
https://www.desmos.com/calculator/1wfdtsuaav
We know that universal Turing machines can emulate other Turing machines. Weirdos like Wolfram believe that a universal Turing machine can emulate reality. In a quick skim of this lecture series, the presenter doesn't talk about that, rather he just calls computation a scientific principal (universal and fundamental in the sense of physical laws, not fundamental in the sense of emulating reality on a computer).