Scheme Programming/Further Maths

From Wikibooks, open books for an open world
Jump to: navigation, search

Contents

[edit] Trigonometric Functions

Scheme always uses radians for its internal representation of angles, so it's sine, cosine, tangent, arcsine, arccosine, and arctangent functions operate as such:

> (sin 0)
0.0
> (cos 0)
1.0
> (tan 0)
0.0
> (asin 1)
1.5707963267948965
> (acos 0)
1.5707963267948965
> (atan 1)
0.7853981633974483

[edit] Hyperbolic Functions

Scheme provides a number of hyperbolic functions, such as hyperbolic sine, cosine, tangent and their inverses.

> (sinh 0)
0.0
> (cosh 0)
1.0
> (tanh 1)
0.7615941559557649
> (asinh 0)
0.0
> (acosh 1)
0.0
> (atanh 0)
0.0

[edit] Power Functions

[edit] Raising a base to a power

Scheme provides the expt function to raise a base to an exponent.

> (expt 2 10)
1024
[edit] Finding the Square root of a number

Scheme provides a sqrt function for finding the square root of a number.

> (sqrt 2)
1.4142135623730951
> (expt 2 0.5) ; Note an ingenious way to find the n-th power of something!
1.4142135623730951

[edit] Exponential and logarithmic functions

[edit] Exponential

Scheme provides a exp function for raising base e to a power:

> (exp 2)
7.3890560989306504
[edit] Logarithm

Scheme provides a log function for finding the natural logarithm of a number:

> (log 7.389056)
1.999999986611192

Note that there is no built-in procedure for finding any other base logarithm other than base e.

[edit] Other useful maths functions (rounding, modulo, gcd, etc.)

[edit] Rounding functions.

Scheme provides a set of functions for rounding a real number up, down or to the nearest integer:

  • (floor x) - This returns the largest integer that is no larger than x.
  • (ceiling x) - This returns the smallest integer that is no smaller than x.
  • (truncate x) - This returns the integer value closest to x that is no larger than the absolute value of x.
  • (round x) - This rounds value of x to the nearest integer as is usual in mathematics. Even when halfway between values.
  • (abs x) - This returns the absolute value of x.
[edit] Number theoretic division

In order to perform mathematically exact divisions and accomplish tasks for number theorists, Scheme provides a small number of division specific functions:

  • (remainder x y) - Calculates the remainder of dividing y into x (that is, the remainder of x/y):
> (remainder 5 4)
1
> (remainder -5 4)
-1
> (remainder 5 -4)
1
> (remainder -5 -4)
-1
  • (modulo x y) - Calculates the modulo of x and y.
> (modulo 5 4)
1
> (modulo -5 4)
3
> (modulo 5 -4)
-3
> (modulo -5 -4)
-1

There is clearly a difference between modulo and remainder, one of them not shown here is that remainder is the only on which will return an inexact value, and can take inexact arguments.

Personal tools
Namespaces
Variants
Actions
Navigation
Community
Toolbox
Sister projects
Print/export