C
#include "stdio.h" int main() {{ int i,j,s,d; /* auto test */ for(i=1;i<=2;i++) {{ j=i; d=diff(i,j); s=sum(j,j); if( d != 0 || s != 2*i ) abort(); }} /* redo it out of loop to be really sure */ d=diff(i,j); s=sum(j,j); printf("%d-%d=%d\n",i,j,d); printf("%d+%d=%d\n",j,j,s); return 0; }} diff(a,b) {{ int i=a,j=b; return i-j; }} sum(i,j) {{ int i,j; return i+j; }}
It outputs:
$ clang test1.c 2>/dev/null $ ./a.out 3-2=1 2+2=5
This is my first program in C, could you help me?
NOTE
I came to totally distrust the integer arithmetic unit, so I modified my functions with the aim of using the floating point unit instead:
copy(int*a,float*b){*b=*a;} diff(a,b) { float i,j; copy(&a,&i); copy(&b,&j); return i-j; } sum(i,j) { float a,b; copy(&a,&i); copy(&b,&j); return a+b; }
but no luck so far, the floating point arithmetic is equally broken!
$ clang test2.c 2>/dev/null $ ./a.out 3-2=1 2+2=5
echo "2+2=5";\$\endgroup\$