Real constants and functions

From Rosetta Code
Revision as of 01:05, 22 April 2009 by rosettacode>Guga360 (added c#)
Task
Real constants and functions
You are encouraged to solve this task according to the task description, using any language you may know.

Show how to use the following math constants and functions in your language (if not available, note it):

  • 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

Ada

Most of the constants and functions used in this task are defined in the pre-defined Ada package Ada.Numerics.Elementary_Functions. <lang ada> 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 </lang>

ALGOL 68

REAL x:=exp(1), y:=4*atan(1);
printf(($g(-8,5)"; "$,
    exp(1),    # e #
    pi,        # pi #
    sqrt(x),   # square root #
    log(x),    # logarithm base 10 #
    ln(x),     # natural logarithm #
    exp(x),    # exponential #
    ABS x,     # absolute value #
    ENTIER x,  # floor #
   -ENTIER -x, # ceiling #
    x ** y     # power #
))

Output:

 2.71828;  3.14159;  1.64872;  0.43429;  1.00000; 15.15426;  2.71828;  2.00000;  3.00000; 23.14069; 

ALGOL 68 also includes assorted long, short and complex versions of the above, eg: long exp, long long exp, short exp, complex exp etc.

And assorted trig functions: sin(x), arcsin(x), cos(x), arccos(x), tan(x), arctan(x), arctan2(x,y), sinh(x), arcsinh(x), cosh(x), arccosh(x), tanh(x) AND arctanh(x).

BASIC

Works with: QuickBasic version 4.5

<lang qbasic>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</lang>

C

Most of the following functions take a double. <lang c>#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 */</lang>

C#

<lang csharp>using System;

class Program {

   static void Main(string[] args) {        
       Console.WriteLine(Math.E); //E
       Console.WriteLine(Math.PI); //PI
       Console.WriteLine(Math.Sqrt(10)); //Square Root
       Console.WriteLine(Math.Log(10)); // Logarithm
       Console.WriteLine(Math.Log10(10)); // Base 10 Logarithm
       Console.WriteLine(Math.Exp(10)); // Exponential
       Console.WriteLine(Math.Abs(10)); //Absolute value
       Console.WriteLine(Math.Floor(10.0)); //Floor
       Console.WriteLine(Math.Ceiling(10.0)); //Ceiling
       Console.WriteLine(Math.Pow(2, 5)); // Exponentiation
   }

}</lang>

Common Lisp

<lang lisp>pi ;pi constant (sqrt x) ;square root (log x) ;natural logarithm (log x 10) ;logarithm base 10 (exp x) ;exponential (abs x) ;absolute value (floor x) ;floor (ceiling x) ;ceiling (expt x y) ;power</lang>

D

<lang 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</lang>

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

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 )

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

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, nonnegative y only)
x ^^ y    -- power (e.g. integer exponentiation of rationals, also negative y)

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

Java

All of these functions are in Java's Math class which, does not require any imports: <lang java>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</lang>

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)

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

Modula-3

Modula-3 uses a module that is a wrapper around C's math.h.

Note that all of these procedures (except the built ins) take LONGREALs as their argument, and return LONGREALs.

Math.E
Math.Pi
Math.log(x);
Math.exp(x);
ABS(x); (* Built in function. *)
FLOOR(x); (* Built in function. *)
CEILING(x); (* Built in function. *)
Math.pow(x, y);

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

OCaml

Unless otherwise noted, the following functions are for floats only: <lang ocaml>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 *)</lang>

Perl

<lang 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</lang>

PHP

<lang 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</lang>

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;

Python

<lang python>import math

math.e # e math.pi # pi math.sqrt(x) # square root (Also commonly seen as x ** 0.5 to obviate importing the math module) math.log(x) # natural logarithm math.log10(x) # base 10 logarithm math.exp(x) # exponential abs(x) # absolute value math.floor(x) # floor math.ceil(x) # ceiling x ** y # exponentiation (and used with an exponent of 0.5 for square roots) </lang>

Ruby

<lang ruby>Math::E #e Math::PI #pi Math.sqrt(x) #square root Math.log(x) #natural logarithm Math.log10(x) #base 10 logarithm Math.exp(x) #exponential x.abs #absolute value x.floor #floor x.ceil #ceiling x ** y #power</lang>

Scheme

<lang 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</lang>

Standard ML

<lang sml>Math.e; (* e *) Math.pi; (* pi *) Math.sqrt x; (* square root *) Math.ln x; (* natural logarithm--log base 10 also available (Math.log10) *) Math.exp x; (* exponential *) abs x; (* absolute value *) floor x; (* floor *) ceil x; (* ceiling *) Math.pow (x, y); (* power *)</lang>


TCl

<lang tcl>expr {exp(1)} ;# e expr {4 * atan(1)} ; # pi expr {sqrt($x)} ; # square root expr {log($x)} ; # natural logarithm, also log10 expr {exp($x)} ; # exponential expr {abs($x)} ; # absolute value expr {floor($x)} ; # floor expr {ceil($x)} ; # ceiling expr {$x**$y} ;# power, also pow($x,$y)</lang>