Help with function

veenu.r.rashid veenu at cbnewsj.ATT.COM
Sat Jan 6 06:13:56 AEST 1990



	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));
}

---

While, to myself at least, this seems like simple code, which
should be rather straightforward, the results are far from
correct -- or, at least they're correct only part of the time.

compile with:
	cc t1.c -ot1

I get the following results, based on random input:

>t1 98.0
98			/* fine		*/

>t1 685944.32
685944.320327		/* some roundoff error	*/

>t1 75433.983
-0.12325946e+11		/* ???		*/

The program produces truly screwy output at the least
provocation.  The strange thing about it that the
last input lies *in-between* the other two values, those
which produce correct output.

So, are there any theories which might explain why this
happens.  Better yet, does anyone have PD code to replace
abs() and/or strtod() ??  (Yes, I *know* abs(x) defined
as (x<0 ? x : -(x)) is not the culprit, since I tried it with
 (how can something so *simple* screw up??!?)
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
my own implementation, and other than the roundoff (sigh)
it seems to work fine.)

BTW, I'm using UNIX System V version 4.0 on an AT&T machine.


Any code, suggestions, comments greatly appreciated.


Navruze Rashid
ruze at mtfmi.att.com	--or--	att!mtfmi!ruze
Bell Laboratories	MT 3F-129

Disclaimer: There is no proprietary material in here,
if I mention UNIX is registered with AT&T.



More information about the Comp.lang.c mailing list