veenu@cbnewsj.ATT.COM (veenu.r.rashid) (01/19/90)
In article <2385@ektools.UUCP> randolph@ektools.UUCP (Gary L. Randolph) writes: >In article <3198@cbnewsj.ATT.COM> veenu@cbnewsj.ATT.COM (veenu.r.rashid) writes: >> On the compiler I'm using, abs() and strtol() seem to generate incorrect >>or at least inaccurate results. I'm using the following code: >>main(int argc, char *argv[]) >>{ >> double temp; >> >> temp = strtod(argv[1]); /* get the argument as a double */ >> printf("Absolute value of %g is %g\n", temp, abs(temp)); >>} >>Any code, suggestions, comments greatly appreciated. > Well, I got quite a flurry of messages about the problem above which I posted to the net.. The vast majority of the messages pointed out one obvious flaw: the usage of strtod() takes two args. This is quite right. I used atof(), which takes one argument, in the actual program, but posted strtod(), since I typed in the message on the fly. I apologize for the confusion, but the other part of the responses did help in a major way -- using fabs(). As it turns out, the abs() macro definition tends to return int, (but not always!), while fabs() returns the correct floating point value. Now, is this just a macro characteristic or is it the compiler I'm using. (*Disclaimer* The question is academic in my case!) Thanks very much to all who replied to the original posting :-) ruze att!mtmfi!ruze AT&T Bell Laboratories (201) 957-6684