Skip to content

Numbers

Amitai Erfanian edited this page Dec 10, 2020 · 34 revisions

Number Attributes

Rational

A rational number is any number that can be expressed as the quotient or fraction of two integers. For example, 12, 2.75, and 1/9 are all rational numbers, while √2, √-1, e, and π are not.

(rational? 1.234) -> #true

Irrational

An irrational number is any number that cannot be expressed as the quotient or fraction of two integers. For example, √2, √-1, e, and π are all irrational numbers, while 12, 1.75, and 1/11 are not.

(rational? pi) -> #false

Integer

An integer is a rational number that can be written without a fractional component. For example, 21, 4, 0, and -2048 are integers, while 9.75, π, and √2 are not.

(integer? 1.234) -> #false

Natural

A natural is a positive integer not including zero, more commonly referred to as the counting numbers. For example, 21, 4, 1, and 2048 are all naturals, while 0, -1, 9.75, π, and √2 are not.

(natural? 1) -> #true

Exact

An exact number is any number that is derived without any assumption or estimation. For example, a number derived from the function sine would not produce an exact number where a number derived from the function add would.

(exact? -20) -> #true

Inexact

An inexact number is any number that is derived with rounding or truncation. For example, a number derived from the function cosine would produce an inexact number where a number derived from the function add would not.

(exact? #i3.14159) -> #false

Number Methods

Absolute Value

(abs [number]) -> number

Determines the absolute value of a number.

(abs -12)
12


Add

(+ [number] [number] [number] ...) -> number

Adds up all numbers.

(+ 1 2 3)
6


Add One

(add1 [number]) -> number

Increments the given number by one.

(add1 2)
3


ArcCosine

(acos [number]) -> number

Computes the arccosine (inverse of cos) of a number.

(acos 0)
#i1.5707963267948966


ArcSine

(asin [number]) -> number

Computes the arcsine (inverse of sin) of a number.

(asin 0)
#i0.0


ArcTangent

(atan [number]) -> number

Computes the arctangent of the given number.

(atan 0)
#i0.0


Ceiling

(ceiling [number]) -> integer

Determines the closest integer (exact or inexact) above a number. See round.

(ceiling 12.3)
13.0


Cosine

(cos [number]) -> number

Computes the cosine of a number (radians).

(cos pi)
#i-1.0


Current Seconds

(current-seconds) -> integer

Determines the current time in seconds elapsed (since a platform-specific starting date in the case of Racket-Lite: January 1st, 1 AD at 12:00:00 AM).

(current-seconds)
63738984180


Divide

(/ [number] [number] [number] ...) -> number

Divides the first by the second (and all following) number(s).

(/ 12 2 3)
2


Equal

(= [number] [number] [number] ...) -> boolean

Compares numbers for equality.

(= 2 3)
#false


Exponent

(expt [number] [number]) -> number

Computes the power of the first to the second number.

(expt 16 1/2)
4


Exponential

(exp [number]) -> number

Determines e raised to a number.

(exp -2)
#i0.1353352832366127


Floor

(floor [number]) -> integer

Determines the closest integer (exact or inexact) below a number. See round.

(floor 12.3)
12.0


Greater Than

(> [number] [number] [number] ...) -> boolean

Compares numbers for greater-than.

(> 3 2)
#true


Greater Than or Equal

(>= [number] [number] [number] ...) -> boolean

Compares numbers for greater-than or equality.

(>= 3 2)
#true


Greatest Common Divisor

(gcd [integer] [integer] [integer] ...) -> integer

Determines the greatest common divisor of two integers (exact or inexact).

(gcd 6 12 8)
2


Hyperbolic Cosine

(cosh [number]) -> number

Computes the hyperbolic cosine of a number.

(cosh 10)
#i11013.232920103324


Hyperbolic Sine

(sinh [number]) -> number

Computes the hyperbolic sine of a number.

(sinh 10)
#i11013.232874703393


Is Even

(even? [integer]) -> boolean

Determines if some integer (exact or inexact) is even or not.

(even? 2)
#true


Is Exact

(exact? [number]) -> boolean

Determines whether some number is exact.

(exact? #i12.87)
#false


Is Integer

(integer? [any]) -> boolean

Determines whether some value is an integer (exact or inexact).

(integer? "hello world")
#false


Is Natural

(natural? [any]) -> boolean

Determines whether some value is a natural (exact or inexact).

(natural? -2)
#false


Is Negative

(negative? [number]) -> boolean

Determines if some number is strictly smaller than zero.

(negative? -2)
#true


Is Number

(number? [any]) -> boolean

Determines whether some value is a number.

(number? "hello world")
#false


Is Odd

(odd? [integer]) -> boolean

Determines if some integer (exact or inexact) is odd or not.

(odd? 2)
#false


Is Positive

(positive? [number]) -> boolean

Determines if some number is strictly larger than zero.

(positive? -2)
#false


Is Rational

(rational? [any]) -> boolean

Determines whether some value is a rational number.

(rational? #i3.141592653589793)
#false


Is Zero

(zero? [number]) -> boolean

Determines if some number is zero or not.

(zero? 2)
#false


Less Than

(< [number] [number] [number] ...) -> boolean

Compares numbers for less-than.

(< 2 2)
#false


Less Than Or Equal

(<= [number] [number] [number] ...) -> boolean

Compares numbers for less-than or equality.

(<= 2 2)
#true


Least Common Multiple

(lcm [integer] [integer] [integer] ...) -> integer

Determines the least common multiple of two integers (exact or inexact).

(lcm 6 12 8)
24


Maximum

(max [number] [number] [number] ...) -> number

Determines the largest number (AKA the maximum).

(max 36 25 16 9)
36


Minimum

(min [number] [number] [number] ...) -> number

Determines the smallest number (AKA the minimum).

(min 36 25 16 9)
9


Modulo

(modulo [integer] [integer]) -> integer

Finds the remainder of the division of the first number by the second.

(modulo 9 2)
1


Multiply

(* [number] [number] [number] ...) -> number

Multiplies all numbers.

(* 1 3 5)
15


Natural Logarithm

(log [number]) -> number

Determines the base-e logarithm of a number.

(log 12)
#i2.4849066497880004


Quotient

(quotient [integer] [integer]) -> integer

Divides the first integer (also called dividend) by the second integer (also known as the divisor) to obtain the quotient.

(quotient 9 2)
4


Random

(random [natural]) -> natural

Generates a random natural number less than or equal to some given natural.

(random 42)
11


Remainder

(remainder [integer] [integer]) -> [integer]

Determines the remainder of dividing the first by the second integer (exact or inexact).

(remainder 9 2)
1


Round

(round [number]) -> [integer]

Rounds a number to an integer (rounds to even to break ties). See floor and ceiling.

(round 12.3)
12.0


Sign

(sgn [number]) -> (union 1 or -1)

Determines the sign of a number.

(sgn -12)
-1


Sine

(sin [number]) -> number

Computes the sine of a number (radians).

(sin pi)
#i1.2246467991473532e-16


Square

(sqr [number]) -> number

Computes the square of a number.

(sqr 8)
64


Square Root

(sqrt [number]) -> number

Computes the square root of a number.

(sqrt 9)
3


Subtract

(- [number] [number] [number] ...) -> number

Subtracts the second (and following) number(s) from the first; negates the number if there is only one argument.

(- 5 3 1)
1


Subtract One

(sub1 [number]) -> number

Decrements the given number by one.

(sub1 2)
1


Tangent

(tan [number]) -> number

Computes the tangent of a number (radians).

(tan pi)
#i-1.2246467991473532e-16


Conversion Methods

Exact To Inexact

(exact->inexact [number]) -> number

Converts an exact number to an inexact one.

(exact->inexact 12)
#i12.0


Inexact To Exact

(inexact->exact [number]) -> number

Approximates an inexact number by an exact one.

(exact->inexact #i12.0)
12.0


Number To String

(number->string [number]) -> string

Converts a number to a string.

(number->string 65)
"65"