Awk -- gets math wrong

bugs at shire.cs.psu.edu bugs at shire.cs.psu.edu
Thu Aug 10 14:17:18 AEST 1989


Serial Number:  824E0190
Machine Type:   Sun 4/260S
O/S Version:    SunOS 4.0.3
Organization:   Computer Science Department, The Pennsylvania State University
                333 Whitmore Laboratory, University Park, PA   16802
Phone Number:   +1 814 865 9723

Description:

	Awk gets math wrong, in sevaral ways.  Symptoms are: 
	--	cosine is computed incorrectly.
	--  expressions involving expressions in sin and cosine are computed
		strangely.

	The first problem is not tolerable.  The second is annoying but
	potentially defensible depending on how the expression is parsed.
	In any case, Sun's documentation does not mention whether or not
	Awk even supports transcendental math functions, a serious bug
	in the documentation.

	[ A possibly pedantic, but very surprising obervation is that
	Sun's man page omits the reference to the paper by Kernighan,
	et.al.  which describes the actual language.  BSD's man page
	sites this paper:  why did Sun delete that reference?  Sun's
	repackaging of the traditional Unix supporting documentation
	(found in /usr/doc on BSD) into anonymous offline collections
	("Pattern Scanning and Processing with AWK" in this case) is
	quite irritating.  These documents should be available online,
	unadulterated, and with authorship correctly cited. ]

Repeat-By:

% awk ' BEGIN {
	ang = 0.15
	rad = (ang) / (57.2958)

	s = sin(rad)
	c = cos(rad)
	print "s " s
	print "c " c

	print "t " s/c
	print "sin/cos " sin(rad) / cos(rad)
} '

s 0.00261799			# Okay
c 0.00261799			# Wrong!
t 1						# Okay
awk: division by zero	# Wrong?  Parsed as (sin(rad)/cos)(rad)


Fix:

Writing parantheses around the denominator works around the second failure:
    print "sin/cos " sin(rad) / (cos(rad))

GUN awk gets it right.  I presume the new SysV awk will too.  Replace
old awk with a modern version.  (Is this planned for 4.1?)



More information about the Comp.sys.sun mailing list