|
Common Math Functions |
For transcendental functions like sin(),
cos(), exp(), and log(),
Sheerpower temporarily converts its internal REAL number to
a standard DOUBLE, performs the operation, and then
converts the result back to a REAL. This is acceptable
because the results of these functions are irrational and cannot be
represented exactly by any finite-precision data type.
For all other math operations, Sheerpower maintains its high-precision REAL data type throughout the calculation, ensuring maximum numeric accuracy wherever mathematically possible.
ABS() returns the absolute value of a number.
SQRT() returns the square root of a number. A perfect square comes back
exact, and any other root is correct to the last digit a REAL holds. The square root of a negative
number raises an error -- give it as a complex number, sqrt(-4 + 0i), for the complex
root. SQR() is an older name for the same function.
SGN() returns the sign of a number: -1 for a negative number, 0 for zero, and 1 for a positive number.
EPS is the smallest step a REAL value can take in its exact range: 0.0000000000000001,
one unit of the 16-decimal grid (see the REAL data type page). Use it as the tolerance for
"close enough" comparisons of computed decimals -- abs(a - b) <= eps -- instead of
a magic number.
DIV0() divides two numbers and returns 0 if the divisor is 0.
DIV0INF() divides two numbers by the IEEE rules: a nonzero
number over 0 is Inf or -Inf, 0 / 0
is NaN, anything else is the ordinary exact division. The companion of
div0() for when a hole or an infinity is the honest answer
(the plain / raises DIVBY0).
DIVR() divides and keeps the exact fraction when the quotient does
not terminate: divr(1, 3) * 3 is exactly 1, where 1 / 3 * 3
is 0.9999999999999999. Its operands are whole numbers, fractions, or decimals of up
to nine places (divr(0.05, 12) is 1/240); a quotient that terminates is
an ordinary decimal (divr(1, 4) is 0.25), and a zero divisor raises
DIVBY0 as / does. How fractions behave in arithmetic, in
comparison and in print is on the REAL internals page
(Internals: REAL and String Data Types).
NUMERATOR() and DENOMINATOR() take a fraction apart (a decimal gives its reduced fraction's parts, a value in scientific form too, and a whole number itself and 1), and DECIMAL() rounds a fraction to its sixteen-place decimal.
FP() returns the fractional part of a number.
MOD() returns the remainder of one number divided by another.
REMAINDER() returns the remainder of a division operation.
SIN() returns the sine of an angle.
COS() returns the cosine of an angle.
TAN() returns the tangent of an angle.
SEC() returns the secant of an angle in radians, which is 1 / cos().
CSC() returns the cosecant of an angle in radians, which is 1 / sin().
COT() returns the cotangent of an angle in radians, which is 1 / tan().
ASIN() returns the arcsine of a number.
ACOS() returns the arccosine of a number.
ATAN() returns the arctangent of a number. ATN() is an older name for the same function.
ATAN() also returns the arctangent of a number.
ANGLE() returns the angle from (0,0) to (x,y) in radians.
DEG() converts an angle in radians to degrees.
RAD() converts an angle in degrees to radians, the unit sin(),
cos() and tan() take.
SINH() returns the hyperbolic sine of a number.
COSH() returns the hyperbolic cosine of a number.
TANH() returns the hyperbolic tangent of a number, always between -1 and 1.
CEIL() returns the smallest integer greater than or equal to a number.
FLOOR() returns the largest integer less than or equal to a number.
INT() returns the whole portion of a REAL number as a REAL.
INTEGER() converts a numeric expression to an integer, ROUNDING it to the nearest
whole number (a half goes away from zero), where INT() drops the fraction. The
result must fit an integer, -2,147,483,648 to 2,147,483,647; a value outside that range raises
the catchable exception INTERR ("Integer overflow").
IP() truncates the decimal portion of a number.
ROUND() rounds a number to a specified number of decimal places.
TRUNCATE() cuts off digits after a specified decimal place.
RND() generates a random number.
rnd() function returns a
fraction between 0 and 1 when used without parameters. If a numeric
argument is provided, it returns a whole number between 1 and that
number. Use set seed NN to specify a repeatable
sequence.
RANDOMIZE starts the random numbers over from an unpredictable point taken from the
clock -- the same thing every program run does when it starts, so a program never needs it
for that. Its use is to leave a set seed sequence: set seed NN makes
rnd() give the same numbers on every run (handy while testing), and
randomize goes back to numbers nobody can predict.
REAL() converts a numeric expression to a REAL value.
EXP() returns e raised to the power of a number.
LOG() returns the natural logarithm (base e) of a number.
LOG2() returns the base-2 logarithm of a number.
LOG10() returns the base-10 (common) logarithm of a number.
MIN() returns the smaller of two values.
MAX() returns the larger of two values.
CLAMP() restricts a value to a given range.
Sheerpower provides a wide range of additional built-in math functions. Most operate directly on its REAL data type to preserve precision throughout. For a complete reference, see the documentation here.
|
Hide Description
|
|
|
Enter or modify the code below, and then click on RUN |
|
Looking for the full power of Sheerpower?
Check out the Sheerpower website. Free to download. Free to use. |