Math constants and functions
From Rosetta Code
Programming Task
This is a programming task. It lays out a problem which Rosetta Code users are encouraged to solve, using languages they know.
- e (Euler's number)
- pi
- square root
- logarithm (any base allowed)
- exponential (ex)
- absolute value (a.k.a. "magnitude")
- floor (largest integer less than or equal to this number--not the same as truncate or int)
- ceiling (smallest integer not less than this number--not the same as round up)
- power (xy)
See also Trigonometric Functions
Contents |
[edit] Ada
Most of the constants and functions used in this task are defined in the pre-defined Ada package Ada.Numerics.Elementary_Functions.
Ada.Numerics.e -- Euler's number Ada.Numerics.pi -- pi sqrt(x) -- square root log(x, base) -- logarithm to any specified base exp(x) -- exponential abs(x) -- absolute value S'floor(x) -- Produces the floor of an instance of subtype S S'ceiling(x) -- Produces the ceiling of an instance of subtype S x**y -- x raised to the y power
[edit] BASIC
Works with: QuickBasic version 4.5
ABS(x) 'absolute value SQR(x) 'square root EXP(x) 'exponential LOG(x) 'natural logarithm x ^ y 'power 'floor, ceiling, e, and pi not available
[edit] C
Most of the following functions take a double.
#include <math.h> M_E; /* e - not standard but offered by most implementations */ M_PI; /* pi - not standard but offered by most implementations */ sqrt(x); /* square root--cube root also available in C99 (cbrt) */ log(x); /* natural logarithm--log base 10 also available (log10) */ exp(x); /* exponential */ abs(x); /* absolute value (for integers) */ fabs(x); /* absolute value (for doubles) */ floor(x); /* floor */ ceil(x); /* ceiling */ pow(x,y); /* power */
[edit] D
import std.math ; // need to import this module E // Euler's number PI // pi constant sqrt(x) // square root log(x) // natural logarithm log10(x) // logarithm base 10 log2(x) // logarithm base 2 exp(x) // exponential abs(x) // absolute value (= magnitude for complex) floor(x) // floor ceil(x) // ceiling pow(x,y) // power
[edit] E
? 1.0.exp() # value: 2.7182818284590455 ? 0.0.acos() * 2 # value: 3.141592653589793 ? 2.0.sqrt() # value: 1.4142135623730951 ? 2.0.log() # value: 0.6931471805599453 ? 5.0.exp() # value: 148.4131591025766 ? (-5).abs() # value: 5 ? 1.2.floor() # value: 1 ? 1.2.ceil() # value: 2 ? 10 ** 6 # value: 1000000
[edit] Forth
1e fexp fconstant e 0e facos 2e f* fconstant pi \ predefined in gforth fsqrt ( f -- f ) fln ( f -- f ) \ flog for base 10 fexp ( f -- f ) fabs ( f -- f ) floor ( f -- f ) \ round towards -inf : ceil ( f -- f ) fnegate floor fnegate ; \ not standard, though fround is available f** ( f e -- f^e )
[edit] Fortran
e ! Not available. Can be calculated EXP(1) pi ! Not available. Can be calculated 4.0*ATAN(1.0) SQRT(x) ! square root LOG(x) ! natural logarithm LOG10(x) ! logarithm to base 10 EXP(x) ! exponential ABS(x) ! absolute value FLOOR(x) ! floor - Fortran 90 or later only CEILING(x) ! ceiling - Fortran 90 or later only x**y ! x raised to the y power
[edit] Haskell
The operations are defined for the various numeric typeclasses, as defined in their type signature.
exp 1 -- Euler number pi -- pi sqrt x -- square root log x -- natural logarithm exp x -- exponential abs x -- absolute value floor x -- floor ceiling x -- ceiling x ** y -- power (e.g. floating-point exponentiation) x ^ y -- power (e.g. integer exponentiation, positive y only) x ^^ y -- power (e.g. rational exponentiation, also negative y)
[edit] J
The examples below require arguments (x and y) to be numeric nouns.
e =. 1x1 NB. Euler's number, specified as a numeric literal. e =. ^ 1 NB. Euler's number, computed by exponentiation. pi=. 1p1 NB. pi, specified as a numeric literal. pi=. o.1 NB. pi, computed trigonometrically. magnitude_of_x =. |x floor_of_x =. <.x ceiling_of_x =. >.x natural_log_of_x =. ^.x base_x_log_of_y =. x^.y x_squared =. *:x NB. special form x_squared =. x^2 NB. exponential form square_root_of_x =. %:x NB. special form square_root_of_x =. x^0.5 NB. exponential form x_to_the_y_power =. x^y
[edit] Java
All of these functions are in Java's Math class which, does not require any imports:
Math.E; //e Math.PI; //pi Math.sqrt(x); //square root--cube root also available (cbrt) Math.log(x); //natural logarithm--log base 10 also available (log10) Math.exp(x); //exponential Math.abs(x); //absolute value Math.floor(x); //floor Math.ceil(x); //ceiling Math.pow(x,y); //power
[edit] JavaScript
Math.E Math.PI Math.sqrt(x) Math.log(x) Math.exp(x) Math.abs(x) Math.floor(x) Math.ceil(x) Math.pow(x,y)
[edit] MAXScript
e -- Euler's number pi -- pi log x -- natural logarithm log10 x -- log base 10 exp x -- exponantial abs x -- absolute value floor x -- floor ceil x -- ceiling pow x y -- power
[edit] Logo
Works with: UCB Logo
make "e exp 1 make "pi 2*(RADARCTAN 0 1) sqrt :x ln :x exp :x ; there is no standard abs, floor, or ceiling; only INT and ROUND. power :x :y
[edit] OCaml
Unless otherwise noted, the following functions are for floats only:
sqrt x;; (* square root *) log x;; (* natural logarithm--log base 10 also available (log10) *) exp x;; (* exponential *) abs_float x;; (* absolute value *) abs x;; (* absolute value (for integers) *) floor x;; (* floor *) ceil x;; (* ceiling *) x ** y; (* power *)
[edit] Perl
use POSIX; exp(1); # e 4 * atan2(1, 1); # pi sqrt($x); # square root log($x); # natural logarithm exp($x); # exponential abs($x); # absolute value POSIX::floor($x); # floor POSIX::ceil($x); # ceiling $x**$y; # power
[edit] PHP
M_E; //e M_PI; //pi sqrt(x); //square root log(x); //natural logarithm--log base 10 also available (log10) exp(x); //exponential abs(x); //absolute value floor(x); //floor ceil(x); //ceiling pow(x,y); //power
[edit] Pop11
pi ;;; Number Pi sqrt(x) ;;; Square root log(x) ;;; Natural logarithm exp(x) ;;; Exponential function abs(x) ;;; Absolute value x ** y ;;; x to the power y
The number e is not provided directly, one has to compute 'exp(1)' instead. Also, floor and ceiling are not provided, one can define them using integer part:
define floor(x);
if x < 0 then
-intof(x);
else
intof(x);
endif;
enddefine;
define ceiling(x);
-floor(-x);
enddefine;
[edit] Python
import math math.e #e math.pi #pi math.sqrt(x) #square root math.log(x) #natural logarithm--log base 10 also available (math.log10) math.exp(x) #exponential abs(x) #absolute value math.floor(x) #floor math.ceil(x) #ceiling x ** y #power
[edit] Scheme
(sqrt x) ;square root (log x) ;natural logarithm (exp x) ;exponential (abs x) ;absolute value (floor x) ;floor (ceiling x) ;ceiling (expt x y) ;power
Categories: Programming Tasks | Basic language learning | Arithmetic operations | Ada | BASIC | C | D | E | Forth | Fortran | Haskell | J | Java | JavaScript | MAXScript | Logo | OCaml | Perl | PHP | Pop11 | Python | Scheme

