Real constants and functions: Difference between revisions

From Rosetta Code
Content added Content deleted
(added c)
(added haskell)
Line 22: Line 22:


=={{header|C}}==
=={{header|C}}==
Most of the following functions take a double; for each of them there are corresponding ones with "f" or "l" appended to the name that take a float or long double, respectively.
Most of the following functions take a double.
<c>#include <math.h>
<c>#include <math.h>


Line 35: Line 35:
ceil(x); /* ceiling */
ceil(x); /* ceiling */
pow(x,y); /* power */</c>
pow(x,y); /* power */</c>

=={{header|Haskell}}==
<pre>
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 (floating-point exponentiation)
x ^ y --power (integer exponentiation)
</pre>


=={{header|Java}}==
=={{header|Java}}==

Revision as of 03:45, 7 May 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 this number--not the same as truncate or int)
  • ceiling (smallest integer greater than this number--not the same as round up)
  • power (xy)

See also Trigonometric Functions

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>

Haskell

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 (floating-point exponentiation)
x ^ y --power (integer exponentiation)

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>

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;

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>

Python

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