[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: A1 Questions
> For part 3, I get a warning in compilation for "Undefined reference to 'sqrt'"
> which only goes away when I attach -lm to the end of the compiler command.
On Unix systems -lm is required to link with the math library, so that is normal.
> when I compile and run on your online environment my p3 code works, but when I run on my
> command line by using "gcc -std=c11 -pedantic -Wall -g -lm -o p3 p3.c"
> I get this error: undefined reference to `sqrt'
The compile command in VECR is a little non-standard but works with the compiler version there.
The -lm should really be at the end of the gcc command line. So instead of this:
gcc -std=c11 -pedantic -Wall -g -lm -o p3 p3.c
do this:
gcc -std=c11 -pedantic -Wall -g -o p3 p3.c -lm
which should work on any system.
> my overflow instance is labeled as a success with no error number.
Math function overflow should produce a non-zero errno, but it looks like: y = pow(256,256);
is computing the result with extended precision internally (i.e. long double) which does
not overflow, but then storing into double y overflows y. Thus y is "Inf" but errno is 0.
That seems like a bug in the math library. Try using larger values, like pow(1000,1000)
Updated answer: pow(256,256) seems to be computed at compile time without actually using
pow or the math library, since it's a simple power of 2. But pow(255,255) requires linking
with the math library and does set errno.
> For part 4, I know that we are supposed to use a bigger data type than int for INT_MAX+1
> which I did. However, I am still getting an overflow warning when using long long.
In #4, INT_MAX+1 uses int arithmetic, which overflows, regardless of where the result
is stored (the long long variable). So either use a cast, i.e. (long long)INT_MAX+1
or a long long 1 value 1LL, or add the 1 after storing as a long long:
long long m = INT_MAX; ++m;