Wikia

Programmer's Wiki

Mathematical functions

Talk4
393pages on
this wiki

In programming languages mathematical functions compute values of "functions" as defined in the science of mathematics. While functions in programming languages are subroutines that return values given certain parameters, functions in mathematics are defined as relations between elements in a definition set Df, and elements in a value set Dv, such that

f: x --> y, where x belongs to Df and y to Dv,

for each x there must be exactly one y, and this y must be the same whenever f is called. This can also be expressed in the more recognizable

f(x) = y,

a pattern that programming languages have borrowed since the old days of the FORTRAN programming language. In most programming languages there is no requirement that f shall produce the same return value (same as the value y from the value set Dv) irrespectively of when the function is called, but instead the "function" may remember a state, and produce another value next time it is called.

Mathematical functions of programming languages adhere to the mathematical same-return-value-each-time rule of mathematical functions. The most often used ones produce one floating point return value from one or sometimes two floating point arguments. The most common mathematical functions regard square roots, logarithms, exponentials and trigonometry.

In generalEdit

In general programming languages use to provide a minimal set of mathematical functions that are fit for the most mathematical purposes, although now and then with some shortcomings as regards to number precision.

For some mathematically oriented programming languages, there are also complex versions, computing from complex value numbers and returning complex value return values.

sign and division functionsEdit

Sign and division functions use to take floating point values and return floating point values. There are often similar functions ranging over integer values, that are inbuilt in the programming language itself.

The following use to be available:

  • abs(x) - absolute value (in math written as |x|), i.e. the value but without any minus sign, so that abs(-2) = abs(2) = 2,
  • min(x,y) - returns the lower (minimum) of x and y, so if x > y, y is returned, x otherwise.
  • max(x,y) - returns the higher (maximum) of x and y, so if x > y, x is returned, y otherwise.

logarithms and exponentialsEdit

Logarithms and exponentials take floating point values and return floating point values.

The common set of functions for powers and exponentials use to be

  • sqrt(x) - implementing square root, such that if sqrt(x) = y, then x = y*y
  • pow(x,y) - implementing the exponentiation xy, [WRITEME!]
  • log(x) - usually not implementing log10x but instead logex = ln x, [WRITEME!]
  • exp(x) - the inverse of log(x), meaning that it implements ex, [WRITEME!]

Constant:

  • e - the natural logarithm constant 2.7182818284590451...

trigonometryEdit

Trigonometric functions take floating point values and return floating point values.

Normally the following functions are represented in most programming languages:

  • sin(x) - [WRITEME!]
  • cos(x) - [WRITEME!]
  • tan(x) - [WRITEME!]
  • arctan2(y,x) (often called atan2) - [WRITEME!]

And their constant pi:

  • pi - 3.1415926535897931... the ratio of the circumference of a circle to its diameter

Specific programming languagesEdit

ActionScriptEdit

ActionScript has about the same mathematical functions as Java has.

CEdit

C mathematical functions are included by perusing the math.h heading:

#include <math.h>

In the linking process one links in the library libm.so, such as by the call

cc -o program program.c -lm

By all C standards, the library libm.so resides in the global C library, so that no -L option is ever needed to link it in. For the need of a static math library linked in, the compilation should instead be

cc -o program program.c /usr/lib/libm.a

or instead of /usr/lib/, wherever your operating system store the C libraries.

GroovyEdit

Many math functions can be called directly as a method of particular objects.

Number classesEdit

  • abs
  • round

CollectionsEdit

  • sum
  • max
  • min

JavaEdit

  • Math.abs gives the absolute value of something
  • Math.round rounds a number to the closest full number
  • Math.random generates a pseudo random number between 0 and 1
  • Math.PI gives Pi with an accuracy of 8 decimals
  • Math.E gives E.
  • Math.sqrt gives the square root of a number
  • Math.tan gives the tangent value of a number
  • Math.cos gives the cosine value of a number

Python Edit

FunctionsEdit

  • abs(x) - absolute value of x
  • math.sqrt(x) - square root of x
  • x ** y - x to the power of y
  • math.log(x)
  • math.log10(x)
  • math.exp(x)
  • math.isinf(x) - Checks if the float x is positive or negative infinite.
  • math.ceil(x) - ceiling of x
  • math.floor(x) - floor of x

ConstantsEdit

  • math.pi
  • math.e

note: there are more functions than what is listed at the link below

See also Edit

Around Wikia's network

Random Wiki