This started with a question from CSAPP Problem 2.44: (x * x) >= 0; which asks to find a counterexample that evaluates to 0,or prove that all number evaluate to 1.(part,32bit)
The official solution provides only 65535, but I found many more.
To test my results, I wrote a C program to find all such number.
#include <stdio.h>
#include <limits.h>
int main() {
FILE *file = fopen("lessthan0.md","w");
int start = 0;
int end = 0;
int total = 0;
int total_tmp = 0;
for (int i = -INT_MAX -1 ; i <= INT_MAX; i++) {
unsigned ui = (unsigned)i;
int result = (int)(ui * ui);
if (result < 0) {
total++;
total_tmp++;
total_tmp == 1 ? start = i : 1;
total_tmp == 1 ? end = i : 1;
i == end + 1 ? end = i : 1;
}
if (i == end + 1) {
start == end ? fprintf(file,"%d total: %d\n",start,total_tmp) : fprintf(file, "%d to %d total: %d\n",start, end,total_tmp);
total_tmp = 0;
}
if (i == INT_MAX) {
fprintf(file,"total: %d",total);
break;
}
}
fclose(file);
}
So I get this:
ls -lh lessthan0.md
-rw-r--r-- 1 kyee users 31G Aug 23 18:50 lessthan0.md
ls -l -rw-r--r-- 1 kyee users 32288890271 Aug 23 18:50 lessthan0_1.md
-rw-r--r-- 1 kyee users 32288890271 Aug 23 20:27 lessthan0.md
wc -l lessthan0.md
1073741824 lessthan0.md
head lessthan0.md
-2147437307 to -2147418113 total: 19195
-2147403383 to -2147390967 total: 12417
-2147380026 to -2147370137 total: 9890
-2147361041 to -2147352577 total: 8465
-2147344625 to -2147337106 total: 7520
-2147329952 to -2147323119 total: 6834
-2147316563 to -2147310257 total: 6307
-2147304170 to -2147298285 total: 5886
-2147292579 to -2147287041 total: 5539
-2147281652 to -2147276405 total: 5248
sed -n "470063416,470063426p" lessthan0.md
-535664087 to -535664086 total: 2
-535664083 to -535664082 total: 2
-535664079 to -535664078 total: 2
-535664075 to -535664074 total: 2
-535664071 to -535664070 total: 2
-535664067 to -535664066 total: 2
-535664063 to -535664062 total: 2
-535664059 to -535664058 total: 2
-535664055 to -535664054 total: 2
-535664051 to -535664050 total: 2
-535664047 to -535664046 total: 2
tail -n 10 lessthan0.md
2147287041 to 2147292579 total: 5539
2147298285 to 2147304170 total: 5886
2147310257 to 2147316563 total: 6307
2147323119 to 2147329952 total: 6834
2147337106 to 2147344625 total: 7520
2147352577 to 2147361041 total: 8465
2147370137 to 2147380026 total: 9890
2147390967 to 2147403383 total: 12417
2147418113 to 2147437307 total: 19195
total: 2147418112
I noticed symmetry between the begging and end of the output.Can someone explain why this happens?
2147418112 / 2^32 = 0.4999847412109375
This accounts for nearly half of all 32-bit signed integers.
20
u/EpochVanquisher Aug 23 '26
Technically this is undefined behavior. If your program uses signed integers and they overflow, then it’s a bug in your program.
Maybe it’s a question about 32-bit signed numbers in twos-complement, but I don’t know the question.
When you say (x * x) >= 0, and “finding a counterexample that evaluates to zero”, I’m not really sure what you are saying here, because finding an x * x that evaluates to zero is not a counterexample.
5
u/nugatory308 Aug 23 '26
Which is what you’d expect, because all you’re really checking for is whether the high-order bit of the 32-bit product is zero or one. And just off the top of the head, that’s going to about 50%, less a bit because the numbers less than 0xffff can never set that high order bit.
7
u/MagicWolfEye Aug 23 '26
That is not C specific though, but specific to signed int32.
3
u/nemotux Aug 23 '26
This should not be surprising. While technically overflow is undefined behavior in signed integers, compilers will typically deal with it (at least sans optimization) by just doing the same thing they do for non-overflowing multiplies and then truncate off the upper bits. This results in roughly half the resulting values having a 1 bit in the msb of the result and roughly half having a 0 bit there. There are a lot, lot more multiplies that overflow than that don't if you consider the whole range of 32-bit numbers. So it'll look about 50/50 positive/negative.
The reason for the symmetry you're seeing is that X*X == -X*-X
3
u/sciencekm Aug 23 '26
The symmetry is because you are going from the minimum negative to the maximum positive.
The pattern goes like this:
So, whatever you are doing (like counting or adding up) at the start will be the same at the end.
2
u/LordDarthSaber Aug 23 '26
This the reason one should always put expected large results in long or long long
It is a runtime error that the programmer must account for
Always know what max size of result could be
2
u/tobdomo Aug 23 '26
You'll be surprised that there's even a lot less where the result actually is correct. Learn about binary number and find out why.
1
u/aocregacc Aug 23 '26
Think about what happens with negative integers when you convert and square them. Converting a signed negative number to unsigned gives you a number of the form
2^31 + x, since the most significant bit will be set. If you have a number of the form2^31 + x, squaring it gives you2^62 + 2 * 2^31 * x + x^2. Modulo 232, that's justx^2again. So when you square numbers starting at INT_MIN, you'd expect the same sequence mod 232 as when you start at 0.1
u/flyingron Aug 23 '26
-INT_MAX is not typically the minimum integer. Two's complement can represent one more value on the negative side.
1
u/SmokeMuch7356 Aug 23 '26
Why
-INT_MAX - 1instead ofINT_MIN? Just because C guarantees two's complement representation now doesn't make that any less risky.1
u/marc_b_reynolds Aug 25 '26
Given sets of two numbers which have 'a' and 'b' significant binary digits then the full product requires 'a+b' binary digits. Take 3*3 = 9. In binary 11*11 = 1001. Each of the 3s have 2 digits so the full result require 2+2=4 digits.
0
u/wild-and-crazy-guy Aug 23 '26
I usually use Integers for counting things.
(Like array index or times to perform a loop)
Whenever I need to do “math” I switch to float