Skip to content

Instantly share code, notes, and snippets.

@pietern
Created July 29, 2010 18:43
Show Gist options
  • Save pietern/498889 to your computer and use it in GitHub Desktop.
Save pietern/498889 to your computer and use it in GitHub Desktop.
# Infinity arithmetic in C
-50.0 + -50.0 = -100.0
-50.0 * -50.0 = 2500.0
-50.0 + 0.0 = -50.0
-50.0 * 0.0 = -0.0
-50.0 + 50.0 = 0.0
-50.0 * 50.0 = -2500.0
-50.0 + inf = inf
-50.0 * inf = -inf
-50.0 + -inf = -inf
-50.0 * -inf = inf
0.0 + -50.0 = -50.0
0.0 * -50.0 = -0.0
0.0 + 0.0 = 0.0
0.0 * 0.0 = 0.0
0.0 + 50.0 = 50.0
0.0 * 50.0 = 0.0
0.0 + inf = inf
0.0 * inf = nan
0.0 + -inf = -inf
0.0 * -inf = nan
50.0 + -50.0 = 0.0
50.0 * -50.0 = -2500.0
50.0 + 0.0 = 50.0
50.0 * 0.0 = 0.0
50.0 + 50.0 = 100.0
50.0 * 50.0 = 2500.0
50.0 + inf = inf
50.0 * inf = inf
50.0 + -inf = -inf
50.0 * -inf = -inf
inf + -50.0 = inf
inf * -50.0 = -inf
inf + 0.0 = inf
inf * 0.0 = nan
inf + 50.0 = inf
inf * 50.0 = inf
inf + inf = inf
inf * inf = inf
inf + -inf = nan
inf * -inf = -inf
-inf + -50.0 = -inf
-inf * -50.0 = inf
-inf + 0.0 = -inf
-inf * 0.0 = nan
-inf + 50.0 = -inf
-inf * 50.0 = -inf
-inf + inf = nan
-inf * inf = -inf
-inf + -inf = -inf
-inf * -inf = inf
#include <stdio.h>
#include <math.h>
int main(int argc, char **argv) {
double a[5];
a[0] = -50.0;
a[1] = 0.0;
a[2] = +50.0;
a[3] = __builtin_inf();
a[4] = -__builtin_inf();
int i,j;
for (i = 0; i < 5; i++)
for (j = 0; j < 5; j++) {
printf("%7.1f\t+ %7.1f\t= %7.1f\n", a[i], a[j], a[i]+a[j]);
printf("%7.1f\t* %7.1f\t= %7.1f\n", a[i], a[j], a[i]*a[j]);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment