Real constants and functions: Difference between revisions
m →{{header|JavaScript}}: formatting |
→{{header|Tcl}}: + pi = acos(-1) |
||
Line 439: | Line 439: | ||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
<lang tcl>expr {exp(1)} ;# e |
<lang tcl>expr {exp(1)} ;# e |
||
expr {4 * atan(1)} ;# pi |
expr {4 * atan(1)} ;# pi -- also, simpler: expr acos(-1) |
||
expr {sqrt($x)} ;# square root |
expr {sqrt($x)} ;# square root |
||
expr {log($x)} ;# natural logarithm, also log10 |
expr {log($x)} ;# natural logarithm, also log10 |
Revision as of 10:55, 25 May 2009
![Task](http://static.miraheze.org/rosettacodewiki/thumb/b/ba/Rcode-button-task-crushed.png/64px-Rcode-button-task-crushed.png)
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).
AWK
The following scriptlets demonstrate some of the required items. abs() was trivially implemented. Missing are:
- floor(), ceil() -- could be easily implemented as well
The pow() example is from Exponentiation operator. <lang>$ awk 'func abs(x){return(x<0?-x:x)}BEGIN{print exp(1),atan2(1,1)*4,sqrt(2),log(2),abs(-42)}' 2.71828 3.14159 1.41421 0.693147 42
$ awk 'func pow(x,n){r=1;for(i=0;i<n;i++)r=r*x;return r}{print pow($1,$2)}' 2.5 2 6.25</lang>
BASIC
<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
Groovy
Math constants and functions are as outlined in the Java example, except as follows:
Absolute Value
In addition to the java.lang.Math.abs() method, each numeric type has an abs() method, which can be invoked directly on the number:
<lang groovy>println ((-22).abs())</lang>
Output:
22
Power
In addition to the java.lang.Math.pow() method, each numeric type works with the power operator (**), which can be invoked as an in-fix operator between two numbers:
<lang groovy>println 22**3.5</lang>
Output:
49943.547010599876
Power results are not defined for all possible pairs of operands. Any power operation that does not have a result returns a 64-bit IEEE NaN (Not a Number) value. <lang groovy>println ((-22)**3.5)</lang> Output:
NaN
Also note that at the moment (--21:58, 24 May 2009 (UTC)) Groovy (1.6.2) gives a mathematically incorrect result for "0**0". The correct result should be "NaN", but the Groovy operation result is "1".
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
<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)</lang>
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
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
Metafont
<lang metafont> show mexp(256); % outputs e; since MF uses mexp(x) = exp(x/256) show 3.14159; % no pi constant built in; of course we can define it
% in several ways... even computing % C/2r (which would be funny since MF handles paths, % and a circle is a path...)
show sqrt2; % 1.41422, or in general sqrt(a) show mexp(256*x); % see e. show abs(x); % returns |x| (the absolute value of the number x, or
% the length of the vector x); it is the same as % length(x); plain Metafont in fact says: % let abs = length;
show floor(x); % floor show ceiling(x); % ceiling show x**y; % ** is not a built in: it is defined in the basic macros
% set for Metafont (plain Metafont) as a primarydef
</lang>
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);
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>
Smalltalk
<lang smalltalk>Float e. Float pi. aNumber sqrt. aNumber log. "base 10 logarithm" aNumber ln. "natural logarithm" aNumber exp. "exponential" aNumber abs. "absolute value" aNumber floor. aNumber ceiling. aNumber raisedTo: anotherNumber</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 -- also, simpler: expr acos(-1) 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>