3.4 - Math


abs

Syntax: include math.e
x = abs(x)
Description: Returns the absolute value of each element of x
Comments: This function may be applied to an atom or to all elements of a sequence
Example:
x = abs({10.5, -12, 3})
-- x is {10.5, 12, 3}

i = abs(-4)
-- i is 4
See Also: sign

arccos

Syntax: include math.e
x2 = arccos(x1)
Description: Return an angle with cosine equal to x1.
Comments: The argument, x1, must be in the range -1 to +1 inclusive.

A value between 0 and PI radians will be returned.

This function may be applied to an atom or to all elements of a sequence.

arccos() is not as fast as arctan().

Example:
s = arccos({-1,0,1})
-- s is {3.141592654, 1.570796327, 0}

See Also: cos, arcsin, arctan

arcsin

Syntax: include math.e
x2 = arcsin(x1)
Description: Return an angle with sine equal to x1.
Comments: The argument, x1, must be in the range -1 to +1 inclusive.

A value between -PI/2 and +PI/2 (radians) will be returned.

This function may be applied to an atom or to all elements of a sequence.

arcsin() is not as fast as arctan().

Example:
s = arcsin({-1,0,1})
-- s is {-1.570796327, 0, 1.570796327}

See Also: sin, arccos, arctan

arctan

Syntax: x2 = arctan(x1)
Description: Return an angle with tangent equal to x1.
Comments: A value between -PI/2 and PI/2 (radians) will be returned.

This function may be applied to an atom or to all elements of a sequence.

arctan() is faster than arcsin() or arccos().

Example:
s = arctan({1,2,3})
-- s is {0.785398, 1.10715, 1.24905}

See Also: tan, atan2, arcsin, arccos

atan2

Syntax: include math.e
a3 = atan2(a1, a2)
Description: calculate the arctangent of y/x
Example:
a = atan2(10.5, 3.1)
-- a is 1.283713958
See Also: arctan, tan, arcsin, arccos

average

Syntax include math.e
a = average(x)
Description Compute the average of all the argument's elements
Comments: This function may be applied to an atom or to all elements of a sequence
Example:
  a = average({8.5, 7.25, 10, 18.75})
  -- a is 11.125
See Also: sum, max, min

ceil

Syntax: include math.e
i = ceil()
Description: Computes next higher argument's integers. Returns the integers that are greater or equal to each element in the argument.
Comments: This function may be applied to an atom or to all elements of a sequence
Example:
sequence nums
nums = {8, -5, 3.14, 4.89, -7.62, -4.3}
nums = ceil(nums) -- {8, -5, 4, 5, -7, -4}
See Also: floor, round, round_to

cos

Syntax: x2 = cos(x1)
Description: Return the cosine of x1, where x1 is in radians.
Comments: This function may be applied to an atom or to all elements of a sequence.
Example:
x = cos({.5, .6, .7})

-- x is {0.8775826, 0.8253356, 0.7648422}

See Also: sin, tan, log, sqrt

deg2rad

Syntax: include math.e
x2 = deg2rad(x1)
Description: Convert an angle measured in degrees to an angle measured in radians
Comments: This function may be applied to an atom or sequence.
Example:
x = deg2rad(194)
-- x is 3.385938749
See Also: rad2deg

E

Syntax: include math.e
E
Description: base of the natural logarithm
Example:
x = E
-- x is 2.718281828459045235
See Also:

exp

Syntax: include math.e
x = exp(2)
Description: calculate E to the n'th power
Example:
x = exp(5.4)
-- x is 221.4064162
See Also: power

floor

Syntax: x2 = floor(x1)
Description: Return the greatest integer less than or equal to x1. (Round down to an integer.)
Comments: This function may be applied to an atom or to all elements of a sequence.
Example:
y = floor({0.5, -1.6, 9.99, 100})
-- y is {0, -2, 9, 100}

See Also: ceil remainder round, round_to

log

Syntax: x2 = log(x1)
Description: Return the natural logarithm of x1.
Comments: This function may be applied to an atom or to all elements of a sequence. Note that log is only defined for positive numbers. Your program will abort with a message if you try to take the log of a negative number or zero.

To compute the inverse, you can use power(e, x) where e is 2.7182818284590452...

Example:
a = log(100)
-- a is 4.60517

See Also: log10, sin, cos, tan, power, sqrt

log10

Syntax: include math.e
x2 = log10(x1)
Description: Return the base 10 logarithm of x1.
Comments: This function may be applied to an atom or to all elements of a sequence. Note that log10 is only defined for positive numbers. Your program will abort with a message if you try to take the log of a negative number or zero.
Example:
a = log10(12)
-- a is 2.48490665
See Also: log, sin, cos, tan, power, sqrt

max

Syntax: include math.e
a = max(x)
Description: Computes the maximum value among all the argument's elements.
Comments: This function may be applied to an atom or to all elements of a sequence
Example:
a = max({10,15.4,3})
-- a is 15.4
See Also: average, min, sum

min

Syntax: include math.e
a = min(x)
Description: Computes the minimum value among all the argument's elements.
Comments: This function may be applied to an atom or to all elements of a sequence
Example:
a = min({10,15.4,3})
-- a is 3
See Also: average, max, sum

PI

Syntax: include math.e
PI
Description: PI (3.141592653589793238) has been defined as a global constant.
Comments: Enough digits have been used to attain the maximum accuracy possible for a Euphoria atom.
Example:
x = PI  -- x is 3.141592653589793238

See Also: sin, cos, tan

power

Syntax: x3 = power(x1, x2)
Description: Raise x1 to the power x2
Comments: The arguments to this function may be atoms or sequences. The rules for operations on sequences apply.

Powers of 2 are calculated very efficiently.

Example 1:
? power(5, 2)
-- 25 is printed

Example 2:
? power({5, 4, 3.5}, {2, 1, -0.5})
-- {25, 4, 0.534522} is printed

Example 3:
? power(2, {1, 2, 3, 4})
-- {2, 4, 8, 16}

Example 4:
? power({1, 2, 3, 4}, 2)
-- {1, 4, 9, 16}

See Also: log, sqrt

rad2deg

Syntax: include math.e
x2 = rad2deg(x1)
Description: Convert an angle measured in radians to an angle measured in degrees
Comments: This function may be applied to an atom or sequence.
Example:
x = rad2deg(3.385938749)
-- x is 194
See Also: deg2rad

rand

Syntax: x2 = rand(x1)
Description: Return a random integer from 1 to x1, where x1 may be from 1 to the largest positive value of type integer (1073741823).
Comments: This function may be applied to an atom or to all elements of a sequence.
Example:
s = rand({10, 20, 30})
-- s might be: {5, 17, 23} or {9, 3, 12} etc.

See Also: set_rand

rand_range

Syntax: include math.e
i3 = rand_range(i1, i2)
Description: Return a random integer from i1 to i2, where i1 may be from 1 to the largest positive value of type integer (1073741823).
Example:
s = rand_range(18, 24)
-- s might be: 18, 19, 20, 21, 22, 23 or 24
See Also: set_rand, rand

remainder

Syntax: x3 = remainder(x1, x2)
Description: Compute the remainder after dividing x1 by x2. The result will have the same sign as x1, and the magnitude of the result will be less than the magnitude of x2.
Comments: The arguments to this function may be atoms or sequences. The rules for operations on sequences apply.
Example 1:
a = remainder(9, 4)
-- a is 1

Example 2:
s = remainder({81, -3.5, -9, 5.5}, {8, -1.7, 2, -4})
-- s is {1, -0.1, -1, 1.5}

Example 3:
s = remainder({17, 12, 34}, 16)
-- s is {1, 12, 2}

Example 4:
s = remainder(16, {2, 3, 5})
-- s is {0, 1, 1}
  
See Also: ceil, floor, round, round_to

round

Syntax: i = round(x)
Description: Return the argument's elements rounded to the nearest integer.
Comments: This function may be applied to an atom or to all elements of a sequence.
Example:
round({4.12, 4.67, -5.8, -5.21}) -- {4, 5, -6, -5}
round(12.2) -- 12
See Also: ceil, floor, remainder, round_to

round_to

Syntax: a = round_to(x, i)
Description: Return the argument's elements rounded to i precision.
Comments: This function may be applied to an atom or to all elements of a sequence.
Example:
round_to({4.12, 4.67, -5.8, -5.21}, 10) -- {4.1, 4.7, -5.8, -5.2}
round_to(12.2512, 100) -- 12.25
See Also: ceil, floor, remainder, round

set_rand

Syntax: include machine.e
set_rand(i1)
Description: Set the random number generator to a certain state, i1, so that you will get a known series of random numbers on subsequent calls to rand().
Comments: Normally the numbers returned by the rand() function are totally unpredictable, and will be different each time you run your program. Sometimes however you may wish to repeat the same series of numbers, perhaps because you are trying to debug your program, or maybe you want the ability to generate the same output (e.g. a random picture) for your user upon request.
Example:
sequence s, t
s = repeat(0, 3)
t = s

set_rand(12345)
s[1] = rand(10)
s[2] = rand(100)
s[3] = rand(1000)

set_rand(12345)  -- same value for set_rand()
t[1] = rand(10)  -- same arguments to rand() as before
t[2] = rand(100)
t[3] = rand(1000)
-- at this point s and t will be identical

See Also: rand

sign

Syntax: include math.e
i = sign(x)
Description: Return -1, 0 or 1 for each element according to it being negative, zero or positive
Comments: This function may be applied to an atom or to all elements of a sequence
Example:
i = sign(5)
-- i is 1

i = sign(0)
-- i is 0

i = sign(-2)
-- i is -1
See Also: abs

sin

Syntax: x2 = sin(x1)
Description: Return the sine of x1, where x1 is in radians.
Comments: This function may be applied to an atom or to all elements of a sequence.
Example:
sin_x = sin({.5, .9, .11})
-- sin_x is {.479, .783, .110}

See Also: cos, tan

sqrt

Syntax: x2 = sqrt(x1)
Description: Calculate the square root of x1.
Comments: This function may be applied to an atom or to all elements of a sequence.

Taking the square root of a negative number will abort your program with a run-time error message.

Example:
r = sqrt(16)
-- r is 4

See Also: log, power

sum

Syntax include math.e
a = sum(x)
Description Compute the sum of all the argument's elements
Comments: This function may be applied to an atom or to all elements of a sequence
Example:
  a = sum({10, 20, 30})
  -- a is 60

  a = sum({10.5, 11.2, 8.1})
  -- a is 29.8
See Also: average, max, min

tan

Syntax: x2 = tan(x1)
Description: Return the tangent of x1, where x1 is in radians.
Comments: This function may be applied to an atom or to all elements of a sequence.
Example:
t = tan(1.0)
-- t is 1.55741

See Also: sin, cos, arctan