[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

6.2.4 Arithmetics

Function: + z ...
Function: * z ...
[R5RS] Returns the sum or the product of given numbers, respectively. If no argument is given, (+) yields 0 and (*) yields 1.

Function: - z1 z2 ...
Function: / z1 z2 ...
[R5RS] If only one number z1 is given, returns its negation and reciprocal, respectively.

If more than one number are given, returns:
 
z1 - z2 - z3 ...
z1 / z2 / z3 ...
respectively.

 
(- 3)       => -3
(- -3.0)    => 3.0
(- 5+2i)    => -5.0-2.0i
(/ 3)       => 0.333333333333333
(/ 5+2i)    => 0.172413793103448-0.0689655172413793i

(- 5 2 1)     => 2
(- 5 2.0 1)   => 2.0
(- 5+3i -i)   => 5.0+2.0i
(/ 6+2i 2)    => 3.0+1.0i

Function: abs z
[R5RS+] For real number z, returns an absolute value of it. For complex number z, returns the magnitude of the number. The complex part is Gauche extension.
 
(abs -1)   => 1
(abs -1.0) => 1.0
(abs 1+i)  => 1.4142135623731

Function: quotient n1 n2
Function: remainder n1 n2
Function: modulo n1 n2
[R5RS] Returns the quotient, remainder and modulo of dividing an integer n1 by an integer n2. The result is an exact number only if both n1 and n2 are exact numbers.

Remainder and modulo differ when either one of the arguments is negative. Remainder R and quotient Q have the following relationship.
 
  n1 = Q * n2 + R
where abs(Q) = floor(abs(n1)/abs(n2)). Consequently, R's sign is always the same as n1's.

On the other hand, modulo works as expected for positive n2, regardless of the sign of n1 (e.g. (modulo -1 n2) == n2 - 1). If n2 is negative, it is mapped to the positive case by the following relationship.
 
  modulo(n1, n2) = -modulo(-n1, -n2)
Consequently, modulo's sign is always the same as n2's.
 
(remainder 10 3)    => 1
(modulo 10 3)       => 1

(remainder -10 3)   => -1
(modulo -10 3)      => 2

(remainder 10 -3)   => 1
(modulo 10 -3)      => -2

(remainder -10 -3)  => -1
(modulo -10 -3)     => -1

Function: quotient&remainder n1 n2
Calculates the quotient and the remainder of dividing integer n1 by integer n2 simultaneously, and returns them as two values.

Function: gcd n ...
Function: lcm n ...
[R5RS] Returns the greatest common divisor or the least common multiplier of the given integers, respectively

Function: numerator q
Function: denominator q
[R5RS] Returns the numerator and denominator of a rational number q. Since Gauche doesn't support full rational numbers, they actually work only on integers; that is, given integer q, numerator always returns q and denominator always return 1.

Function: floor x
Function: ceiling x
Function: truncate x
Function: round x
[R5RS] The argument x must be a real number. Floor and ceiling return a minimum integer that is greater than x and a maximim integer that is less than x, respectively. Truncate returns an integer that truncates x towards zero. Round returns an integer that is closest to x. If fractional part of x is exactly 0.5, round returns the closest even integer.

Function: clamp x &optional min max
Returns
 
 min if x < min
 x   if min <= x <= max
 max if max < x
If min or max is omitted or #f, it is regarded as -infinity or +infinity, respectively. Returns an exact integer only if all the given numbers are exact integers.
 
(clamp 3.1 0.0 1.0) => 1.0
(clamp 0.5 0.0 1.0) => 0.5
(clamp -0.3 0.0 1.0) => 0.0
(clamp -5 0)        => 0
(clamp 3724 #f 256) => 256

Function: exp z
Function: log z
Function: sin z
Function: cos z
Function: tan z
Function: asin z
Function: acos z
Function: atan z
[R5RS] Transcedental functions. Work for complex numbers as well.

Function: atan x y
[R5RS] For real numbers x and y, returns atan(y/x).

Function: sinh z
Function: cosh z
Function: tanh z
Function: asinh z
Function: acosh z
Function: atanh z
Hyperbolic trigonometric functions. Work for complex numbers as well.

Function: sqrt z
[R5RS] Returns a square root of a complex number z. The branch cut scheme is the same as Common Lisp. For real numbers, it returns a positive root.

Function: expt z1 z2
[R5RS] Returns z1^z2 (z1 powered by z2), where z1 and z2 are complex numbers.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

This document was generated by Ken Dickey on November, 28 2002 using texi2html