Popup YouTube Video
Sheerpower Logo

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.

Core Math Functions

ABS() returns the absolute value of a number.

print abs(-10) // Returns: 10 print abs(10) // Returns: 10

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.

print sqrt(64) // Returns: 8 print sqrt(2) // Returns: 1.414213562373095 print sqrt(-4 + 0i) // Returns: 0+2i

SGN() returns the sign of a number: -1 for a negative number, 0 for zero, and 1 for a positive number.

print sgn(-238) // Returns: -1 print sgn(0) // Returns: 0 print sgn(238) // Returns: 1

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.

print eps // Returns: 0.0000000000000001 print abs(0.1 + 0.2 - 0.3) <= eps // Returns: 1

DIV0() divides two numbers and returns 0 if the divisor is 0.

print div0(0.8, 0.000004) // Returns: 200000 print div0(0.8, 0.0) // Returns: 0 print div0(6, 3) // Returns: 2 print div0(6, 0) // Returns: 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).

print div0inf(6, 3) // Returns: 2 print div0inf(6, 0) // Returns: Inf print div0inf(-6, 0) // Returns: -Inf print div0inf(0, 0) // Returns: NaN

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).

print divr(1, 3) * 3 // Returns: 1 print fraction$(divr(0.05, 12)) // Returns: 1/240 print divr(1, 4) // Returns: 0.25

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.

print numerator(divr(22, 7)) // Returns: 22 print denominator(divr(22, 7)) // Returns: 7 print denominator(0.25) // Returns: 4 print decimal(divr(2, 3)) // Returns: 0.6666666666666667

FP() returns the fractional part of a number.

print fp(238.304) // Returns: 0.304

MOD() returns the remainder of one number divided by another.

print mod(36, 13) // Returns: 10

REMAINDER() returns the remainder of a division operation.

print remainder(-4, 3) // Returns: -1

Trigonometric Functions

SIN() returns the sine of an angle.

print sin(0) // Returns: 0 print sin(1.5708) // Returns: 1 (approximately)

COS() returns the cosine of an angle.

print cos(0) // Returns: 1 print cos(1.5708) // Returns: 0 (approximately)

TAN() returns the tangent of an angle.

print tan(0) // Returns: 0 print tan(0.7854) // Returns: 1 (approximately, pi/4)

SEC() returns the secant of an angle in radians, which is 1 / cos().

print sec(0) // Returns: 1

CSC() returns the cosecant of an angle in radians, which is 1 / sin().

print csc(pi / 2) // Returns: 1

COT() returns the cotangent of an angle in radians, which is 1 / tan().

print cot(pi / 4) // Returns: 1

ASIN() returns the arcsine of a number.

print asin(0) // Returns: 0 print asin(1) // Returns: 1.5708 (pi/2)

ACOS() returns the arccosine of a number.

print acos(1) // Returns: 0 print acos(0) // Returns: 1.5708 (pi/2)

ATAN() returns the arctangent of a number. ATN() is an older name for the same function.

print atan(0) // Returns: 0 print atan(1) // Returns: 0.7854 (pi/4) print atn(1) // Returns: 0.785398163397448 (the same as atan(1))

ATAN() also returns the arctangent of a number.

print atan(33) // Returns: 1.5405

ANGLE() returns the angle from (0,0) to (x,y) in radians.

print angle(4, 9) // Returns: 1.15257

DEG() converts an angle in radians to degrees.

print deg(pi) // Returns: 180 print deg(pi / 4) // Returns: 45

RAD() converts an angle in degrees to radians, the unit sin(), cos() and tan() take.

print rad(180) // Returns: 3.1415926535897932 print sin(rad(90)) // Returns: 1

SINH() returns the hyperbolic sine of a number.

print sinh(0) // Returns: 0 print sinh(1) // Returns: 1.1752011936438

COSH() returns the hyperbolic cosine of a number.

print cosh(0) // Returns: 1 print cosh(1) // Returns: 1.54308063481524

TANH() returns the hyperbolic tangent of a number, always between -1 and 1.

print tanh(0) // Returns: 0 print tanh(1) // Returns: 0.761594155955765

Rounding and Integer Functions

CEIL() returns the smallest integer greater than or equal to a number.

print ceil(2.3) // Returns: 3 print ceil(-2.3) // Returns: -2

FLOOR() returns the largest integer less than or equal to a number.

print floor(2.7) // Returns: 2 print floor(-2.7) // Returns: -3

INT() returns the whole portion of a REAL number as a REAL.

print int(148.8432) // Returns: 148

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").

print int(2.7); integer(2.7) // Returns: 2 3 print integer(2.5); integer(-2.5) // Returns: 3 -3 z = integer(4 + (993 * 35)) print z // Returns: 34759

IP() truncates the decimal portion of a number.

print ip(1234.56) // Returns: 1234

ROUND() rounds a number to a specified number of decimal places.

print round(21.83492, 2) // Returns: 21.83

TRUNCATE() cuts off digits after a specified decimal place.

print truncate(123.45678, 2) // Returns: 123.45 print truncate(123.45678, 4) // Returns: 123.4567

Random Number Functions

RND() generates a random number.

print rnd // Returns: 0.94097 (example) print rnd(100) // Returns: 56 (example)
Note: The 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.

set seed 7 print rnd(1000); rnd(1000); rnd(1000) // Returns: 598 300 332 set seed 7 print rnd(1000); rnd(1000); rnd(1000) // Returns: 598 300 332 (the same three) randomize print rnd(1000); rnd(1000); rnd(1000) // Returns: 954 333 830 (different on every run)

Conversion and Utility Functions

REAL() converts a numeric expression to a REAL value.

input 'Your age': age% let decimal_age = real(age%) print 'Your number is'; decimal_age // Your age? 31 → Your number is 31

EXP() returns e raised to the power of a number.

print exp(1) // Returns: 2.7183 print exp(0) // Returns: 1

LOG() returns the natural logarithm (base e) of a number.

print log(1) // Returns: 0 print log(2.7183) // Returns: 1

LOG2() returns the base-2 logarithm of a number.

print log2(8) // Returns: 3 print log2(1024) // Returns: 10

LOG10() returns the base-10 (common) logarithm of a number.

print log10(1000) // Returns: 3 print log10(0.001) // Returns: -3

MIN() returns the smaller of two values.

print min(3, 2) // Returns: 2 print min(-1, -5) // Returns: -5

MAX() returns the larger of two values.

print max(5, 9) // Returns: 9

CLAMP() restricts a value to a given range.

print clamp(15, 1, 10) // Returns: 10 print clamp(5, 1, 10) // Returns: 5 print clamp(-3, 1, 10) // Returns: 1

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.