Scheme Programming/Further Maths

From Wikibooks, open books for an open world
Jump to navigation Jump to search
Scheme Programming
 ← Simple Maths Further Maths List Operations → 

Trigonometric Functions[edit | edit source]

Scheme always uses radians for its internal representation of angles, so its 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

Hyperbolic Functions[edit | edit source]

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

Power Functions[edit | edit source]

Raising a base to a power[edit | edit source]

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

> (expt 2 10)
1024

Finding the square root of a number[edit | edit source]

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

> (sqrt 2)
1.4142135623730951
> (expt 2 0.5)
1.4142135623730951

Exponential and logarithmic functions[edit | edit source]

Exponential[edit | edit source]

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

> (exp 2)
7.3890560989306504

Logarithm[edit | edit source]

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 . Instead, you can type

> (define logB 
    (lambda (x B) 
      (/ (log x) (log B))))

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

Rounding functions[edit | edit source]

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. It even works when halfway between values.
  • (abs x) - This returns the absolute value of x.

Number theoretic division[edit | edit source]

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
> (remainder x y)
error
> (remainder 2 1)
0
  • (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 one which will return an inexact value, and can take inexact arguments.