Originally Posted by
tabstop
You're going to have to back that up with actual data, because I don't believe you. (I believe short is that range, but I don't believe that the numbers in the formula are outside that range.)
I agree: they can't be if x is short; it's impossible! Even the compiler says it's silly
Code:
#include <stdio.h>
#include <math.h>
#include <limits.h>
#include <assert.h>
#define PI 3.14159265358979323846
#define PIOVER180 0.01745329251994329577
int main(void)
{
size_t n;
int thingy; // No idea what this is
short thing; // No idea what this is
printf("SHRT_MAX = %d\n", SHRT_MAX);
for (n = 0; n < 1000000; n++) {
thing = (PI * n / 512) * 31000; // Mystery thing
assert(thing >= SHRT_MIN && thing <= SHRT_MAX);
thingy = (short)((PI * n / 512) * 31000);
if (thingy > SHRT_MAX)
printf("SOMETHING'S WRONG! thing == %d\n", thingy);
}
return 0;
}
Code:
$ gcc -Wall -Wextra van.c -lm
van.c: In function ‘main’:
van.c:19:9: warning: comparison is always true due to limited range of data type [-Wtype-limits]
assert(thing >= SHRT_MIN && thing <= SHRT_MAX);
^
van.c:19:9: warning: comparison is always true due to limited range of data type [-Wtype-limits]