Up Next Tail

### 7.1 Numerical Operators

REDUCE includes a number of functions that are analogs of those found in most numerical systems. With numerical arguments, such functions return the expected result. However, they may also be called with non-numerical arguments. In such cases, except where noted, the system attempts to simplify the expression as far as it can. In such cases, a residual expression involving the original operator usually remains. These operators are as follows:

#### 7.1.1 ABS

ABS returns the absolute value of its single argument, if that argument has a numerical value. A non-numerical argument is returned as an absolute value, with an overall numerical coefficient taken outside the absolute value operator. For example:

abs(-3/4)     ->  3/4
abs(2a)       ->  2*ABS(A)
abs(i)        ->  1
abs(-x)       ->  ABS(X)

#### 7.1.2 CEILING

This operator returns the ceiling (i.e., the least integer greater than the given argument) if its single argument has a numerical value. A non-numerical argument is returned as an expression in the original operator. For example:

ceiling(-5/4) ->  -1
ceiling(-a)   ->  CEILING(-A)

#### 7.1.3 CONJ

This returns the complex conjugate of an expression, if that argument has an numerical value. A non-numerical argument is returned as an expression in the operators REPART and IMPART. For example:

conj(1+i)     -> 1-I
conj(a+i*b)   -> REPART(A) - REPART(B)*I
- IMPART(A)*I - IMPART(B)

#### 7.1.4 FACTORIAL

If the single argument of FACTORIAL evaluates to a non-negative integer, its factorial is returned. Otherwise an expression involving FACTORIAL is returned. For example:

factorial(5)  ->  120
factorial(a)  ->  FACTORIAL(A)

#### 7.1.5 FIX

This operator returns the fixed value (i.e., the integer part of the given argument) if its single argument has a numerical value. A non-numerical argument is returned as an expression in the original operator. For example:

fix(-5/4)   ->  -1
fix(a)      ->  FIX(A)

#### 7.1.6 FLOOR

This operator returns the floor (i.e., the greatest integer less than the given argument) if its single argument has a numerical value. A non-numerical argument is returned as an expression in the original operator. For example:

floor(-5/4)   ->  -2
floor(a)      ->  FLOOR(A)

#### 7.1.7 IMPART

This operator returns the imaginary part of an expression, if that argument has an numerical value. A non-numerical argument is returned as an expression in the operators REPART and IMPART. For example:

impart(1+i)   -> 1
impart(a+i*b) -> REPART(B) + IMPART(A)

#### 7.1.8 MAX/MIN

MAX and MIN can take an arbitrary number of expressions as their arguments. If all arguments evaluate to numerical values, the maximum or minimum of the argument list is returned. If any argument is non-numeric, an appropriately reduced expression is returned. For example:

max(2,-3,4,5) ->  5
min(2,-2)     ->  -2.
max(a,2,3)    ->  MAX(A,3)
min(x)        ->  X

MAX or MIN of an empty list returns 0.

#### 7.1.9 NEXTPRIME

NEXTPRIME returns the next prime greater than its integer argument, using a probabilistic algorithm. A type error occurs if the value of the argument is not an integer. For example:

nextprime(5)      ->  7
nextprime(-2)     ->  2
nextprime(-7)     -> -5
nextprime 1000000 -> 1000003

whereas nextprime(a) gives a type error.

#### 7.1.10 RANDOM

random(n) returns a random number r in the range 0 r < n. A type error occurs if the value of the argument is not a positive integer in algebraic mode, or positive number in symbolic mode. For example:

random(5)         ->    3
random(1000)      ->  191

whereas random(a) gives a type error.

#### 7.1.11 RANDOM_NEW_SEED

random_new_seed(n) reseeds the random number generator to a sequence determined by the integer argument n. It can be used to ensure that a repeatable pseudo-random sequence will be delivered regardless of any previous use of RANDOM, or can be called early in a run with an argument derived from something variable (such as the time of day) to arrange that different runs of a REDUCE program will use different random sequences. When a fresh copy of REDUCE is first created it is as if random_new_seed(1) has been obeyed.

A type error occurs if the value of the argument is not a positive integer.

#### 7.1.12 REPART

This returns the real part of an expression, if that argument has an numerical value. A non-numerical argument is returned as an expression in the operators REPART and IMPART. For example:

repart(1+i)   -> 1
repart(a+i*b) -> REPART(A) - IMPART(B)

#### 7.1.13 ROUND

This operator returns the rounded value (i.e, the nearest integer) of its single argument if that argument has a numerical value. A non-numeric argument is returned as an expression in the original operator. For example:

round(-5/4)   ->  -1
round(a)      ->  ROUND(A)

#### 7.1.14 SIGN

SIGN tries to evaluate the sign of its argument. If this is possible SIGN returns one of 1, 0 or -1. Otherwise, the result is the original form or a simplified variant. For example:

sign(-5)      ->  -1
sign(-a^2*b)  ->  -SIGN(B)

Note that even powers of formal expressions are assumed to be positive only as long as the switch COMPLEX is off.

 Up Next Front