Real constants and functions: Difference between revisions

From Rosetta Code
Content added Content deleted
(added common lisp and ruby)
Line 70: Line 70:
ceil(x); /* ceiling */
ceil(x); /* ceiling */
pow(x,y); /* power */</c>
pow(x,y); /* power */</c>

=={{header|Common Lisp}}==
<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</lisp>

=={{header|D}}==
=={{header|D}}==
<d>import std.math ; // need to import this module
<d>import std.math ; // need to import this module
Line 284: Line 296:
math.pi #pi
math.pi #pi
math.sqrt(x) #square root
math.sqrt(x) #square root
math.log(x) #natural logarithm--log base 10 also available (math.log10)
math.log(x) #natural logarithm
math.log10(x) #base 10 logarithm
math.exp(x) #exponential
math.exp(x) #exponential
abs(x) #absolute value
abs(x) #absolute value
Line 290: Line 303:
math.ceil(x) #ceiling
math.ceil(x) #ceiling
x ** y #power</python>
x ** y #power</python>

=={{header|Ruby}}==
<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</ruby>


=={{header|Scheme}}==
=={{header|Scheme}}==

Revision as of 03:56, 27 November 2008

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. <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 </ada>

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

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

C

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

Common Lisp

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

D

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

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, positive y only)
x ^^ y    -- power (e.g. rational exponentiation, 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: <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</java>

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

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: <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 *)</ocaml>

Perl

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

PHP

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

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

<python>import math

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 abs(x) #absolute value math.floor(x) #floor math.ceil(x) #ceiling x ** y #power</python>

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

Scheme

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