sign -- the sign of a real or
complex number
Introductionsign(z) returns the sign of the number
z.
Call(s)sign(z)
Parametersz |
- | an arithmetical expression |
Returnsan arithmetical expression.
z
Side
Effectssign respects properties of identifiers. For real
expressions, the result may depend on the value of the environment
variable DIGITS.
Related
Functions
Detailssign(0) and
sign(0.0) return 0. The user may
redefine this value by a direct assignment, e.g.:
unprotect(sign): sign(0) := 1: protect(sign):
z is DOM_INT, DOM_RAT, or DOM_FLOAT, a fast kernel function
is used to determine the sign. The return value is either
-1, 0, or 1.expand function
rewrites the sign of a product to a product of signs. E.g.,
expand(sign(x*y)) yields sign(x)*sign(y). Cf.
example 2.PI - sqrt(2),
exp(I*3) - I*sin(3) etc., internal floating point
evaluation is used to determine, whether the expression represents a
nonzero real number. If so, the sign -1 or 1 is
returned. Internally, the floating point approximation is checked for
reliability. Cf. example 4.
Example
1We compute the sign of various real numbers and expressions:
>> sign(-8/3), sign(3.2), sign(exp(3) - sqrt(2)*PI), sign(0)
-1, 1, 1, 0
The sign of a complex number z is the
complex number z/abs(z):
>> sign(0.5 + 1.1*I), sign(2 + 3*I), sign(exp(sin(2 + 3*I)))
1/2
0.4138029443 + 0.9103664775 I, (2/13 + 3/13 I) 13 ,
exp(I cos(2) sinh(3))
Example
2sign yields a symbolic, yet simplified,
function call if identifiers are involved:
>> sign(x), sign(2*x*y), sign(2*x + y), sign(PI*exp(2 + y))
sign(x), sign(x y), sign(2 x + y), sign(exp(y + 2))
In special cases, the expand function may provide further
simplifications:
>> expand(sign(2*x*y)), expand(sign(PI*exp(2 + y)))
sign(x) sign(y), sign(exp(y))
Example
3sign respects properties of
identifiers:
>> sign(x + PI)
sign(x + PI)
>> assume(x > -3): sign(x + PI)
1
>> unassume(x):
Example
4The following rational number approximates PI to about 20 digits:
>> p := 157079632679489661923/50000000000000000000:
With the standard precision DIGITS = 10, the float
test inside sign does not give a decisive answer, whether
p is larger or smaller than PI:
>> float(PI - p)
0.0
This result is subject to numerical roundoff and does
not allow a conclusion on the sign of the number PI - p.
The float test inside sign checks the reliablity of
floating point approximations. In this case, no simplified result is
returned:
>> sign(PI - p)
sign(PI - 157079632679489661923/50000000000000000000)
With increased DIGITS, a reliable decision can be
taken:
>> DIGITS := 20: sign(PI - p)
1
>> delete p, DIGITS: