1#
2# CDDL HEADER START
3#
4# The contents of this file are subject to the terms of the
5# Common Development and Distribution License (the "License").
6# You may not use this file except in compliance with the License.
7#
8# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9# or http://www.opensolaris.org/os/licensing.
10# See the License for the specific language governing permissions
11# and limitations under the License.
12#
13# When distributing Covered Code, include this CDDL HEADER in each
14# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15# If applicable, add the following below this CDDL HEADER, with the
16# fields enclosed by brackets "[]" replaced with your own identifying
17# information: Portions Copyright [yyyy] [name of copyright owner]
18#
19# CDDL HEADER END
20#
21
22#
23# Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
24#
25
26#
27# This test checks whether arithmetric math correctly supports
28# negative zero values
29#
30# This was reported as CR #6805795 ("[ku1] ksh93 does not differ between -0 and +0"):
31# ------------ snip ------------
32#  Original bug report was:
33# ------ snip ------
34# Is there a reason why ksh93 does not display the negative sign for the
35# value zero ? For example if I have use the C99 function "copysign"
36# (copies absolute value of operant a and sign of operant b) I get this
37# for { a=5, b=-0 }:
38# -- snip --
39# $ ksh93 -c 'float x; (( x=copysign(5, -0) )) ; printf "%f\n"
40# x'
41# -5.000000
42# -- snip --
43# Now if I swap operands a and b I get this result:
44# -- snip --
45# $ ksh93 -c 'float x; (( x=copysign(0, -5) )) ; printf "%f\n" x'
46# 0.000000
47# -- snip --
48# AFAIK this result should be "-0.000000" ... or not ?
49# BTW: Parsing of "-0" doesn't seem to work either, e.g.
50# -- snip --
51# $ ksh93 -c 'float x a=-1 b=-0; (( x=copysign(a, b) )) ; printf "%f\n"
52# x'
53# 1.000000
54# -- snip --
55# ... while AFAIK it should be "-1.000000" since the 2nd operand of
56# "copysign" defines the sign of the result.
57# ------ snip ------
58# ------------ snip ------------
59#
60
61# test setup
62function err_exit
63{
64	print -u2 -n "\t"
65	print -u2 -r ${Command}[$1]: "${@:2}"
66	(( Errors < 127 && Errors++ ))
67}
68alias err_exit='err_exit $LINENO'
69
70set -o nounset
71Command=${0##*/}
72integer Errors=0
73
74typeset str
75
76# test 1: test "copysign()" using constant values
77str=$(
78	set -o errexit
79
80	print -- $(( copysign(0, -5) ))
81	) || err_exit "test failed."
82[[ "${str}" == "-0" ]] || err_exit "Expected copysign(0, -5) == -0, got ${str}"
83
84
85# test 2: Same as test 1 but using variables for the values
86str=$(
87	set -o errexit
88
89	float a
90	float b
91	float c
92
93	a=0.
94	b=-5.
95
96	(( c=copysign(a, b) ))
97
98	print -- "$c"
99	) || err_exit "test failed."
100[[ "${str}" == "-0" ]] || err_exit "Expected c == -0, got ${str}"
101
102
103# test 3: test "signbit()"
104str=$(
105	set -o errexit
106
107	float a
108
109	a=-0.
110
111	print -- $(( signbit(a) ))
112	) || err_exit "test failed."
113[[ "${str}" == "1" ]] || err_exit "Expected signbit(a, b) == 1, got ${str}"
114
115
116# test 4: test "signbit()"
117str=$(
118	set -o errexit
119
120	float a
121	float c
122
123	a=-0.
124
125	(( c=signbit(a) ))
126
127	print -- "$c"
128	) || err_exit "test failed."
129[[ "${str}" == "1" ]] || err_exit "Expected c == 1, got ${str}"
130
131
132# test 5: test whether "typeset -X" (C99 "hexfloat") correctly recognizes
133# negative zero assigned from a "float"
134str=$(
135	set -o errexit
136
137	float a      # float
138	typeset -X c # hexfloat
139
140	a=-0.
141
142	# copy value from "float" to "hexfloat"
143	(( c=a ))
144
145	print -- "$c"
146	) || err_exit "test failed."
147[[ "${str}" == -0x* ]] || err_exit "Expected c == -0x*, got ${str}"
148
149
150# test 6: Reverse of test 5: Test whether "float" correctly recognizes
151# a C99 "hexfloat" value
152str=$(
153	set -o errexit
154
155	typeset -X a # hexfloat
156	float c      # float
157
158	a=-0x0.0000000000000000000000000000p+00
159
160	# copy value from "hexfloat" to "float"
161	(( c=a ))
162
163	print -- "$c"
164	) || err_exit "test failed."
165[[ "${str}" == "-0" ]] || err_exit "Expected c == -0, got ${str}"
166
167
168# tests done
169exit $((Errors))
170