Real constants and functions: Difference between revisions

Content added Content deleted
m (syntax highlighting fixup automation)
Line 23: Line 23:
{{trans|Python}}
{{trans|Python}}


<lang 11l>math:e // e
<syntaxhighlight lang="11l">math:e // e
math:pi // pi
math:pi // pi
sqrt(x) // square root
sqrt(x) // square root
Line 32: Line 32:
floor(x) // floor
floor(x) // floor
ceil(x) // ceiling
ceil(x) // ceiling
x ^ y // exponentiation</lang>
x ^ y // exponentiation</syntaxhighlight>


=={{header|6502 Assembly}}==
=={{header|6502 Assembly}}==
Line 38: Line 38:
Absolute value can be handled like so:
Absolute value can be handled like so:


<lang 6502asm>GetAbs: ;assumes value we want to abs() is loaded into accumulator
<syntaxhighlight lang="6502asm">GetAbs: ;assumes value we want to abs() is loaded into accumulator
eor #$ff
eor #$ff
clc
clc
adc #1
adc #1
rts</lang>
rts</syntaxhighlight>




Line 48: Line 48:
Only the last three are available as built in functions.
Only the last three are available as built in functions.


<lang Lisp>(floor 15 2) ;; This is the floor of 15/2
<syntaxhighlight lang="lisp">(floor 15 2) ;; This is the floor of 15/2
(ceiling 15 2)
(ceiling 15 2)
(expt 15 2) ;; 15 squared</lang> =={{header|ACL2}}==
(expt 15 2) ;; 15 squared</syntaxhighlight> =={{header|ACL2}}==
Only the last three are available as built in functions.
Only the last three are available as built in functions.


Line 56: Line 56:
define them using integer part:
define them using integer part:


<lang pop11>define floor(x);
<syntaxhighlight lang="pop11">define floor(x);
if x < 0 then
if x < 0 then
-intof(x);
-intof(x);
Line 66: Line 66:
define ceiling(x);
define ceiling(x);
-floor(-x);
-floor(-x);
enddefine;</lang>
enddefine;</syntaxhighlight>


=={{header|Action!}}==
=={{header|Action!}}==
Line 72: Line 72:
{{libheader|Action! Tool Kit}}
{{libheader|Action! Tool Kit}}
{{libheader|Action! Real Math}}
{{libheader|Action! Real Math}}
<lang Action!>INCLUDE "H6:REALMATH.ACT"
<syntaxhighlight lang="action!">INCLUDE "H6:REALMATH.ACT"


PROC Euler(REAL POINTER e)
PROC Euler(REAL POINTER e)
Line 154: Line 154:
PutE()
PutE()
PrintE("There is no support in Action! for pi.")
PrintE("There is no support in Action! for pi.")
RETURN</lang>
RETURN</syntaxhighlight>
{{out}}
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Real_constants_and_functions.png Screenshot from Atari 8-bit computer]
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Real_constants_and_functions.png Screenshot from Atari 8-bit computer]
Line 176: Line 176:
=={{header|ActionScript}}==
=={{header|ActionScript}}==
Actionscript has all the functions and constants mentioned in the task, available in the Math class.
Actionscript has all the functions and constants mentioned in the task, available in the Math class.
<lang ActionScript>Math.E; //e
<syntaxhighlight lang="actionscript">Math.E; //e
Math.PI; //pi
Math.PI; //pi
Math.sqrt(u); //square root of u
Math.sqrt(u); //square root of u
Line 184: Line 184:
Math.floor(u);//floor of u
Math.floor(u);//floor of u
Math.ceil(u); //ceiling of u
Math.ceil(u); //ceiling of u
Math.pow(u,v);//u to the power of v</lang>
Math.pow(u,v);//u to the power of v</syntaxhighlight>
The Math class also contains several other constants.
The Math class also contains several other constants.
<lang ActionScript>Math.LN10; // natural logarithm of 10
<syntaxhighlight lang="actionscript">Math.LN10; // natural logarithm of 10
Math.LN2; // natural logarithm of 2
Math.LN2; // natural logarithm of 2
Math.LOG10E; // base-10 logarithm of e
Math.LOG10E; // base-10 logarithm of e
Math.LOG2E; // base-2 logarithm of e
Math.LOG2E; // base-2 logarithm of e
Math.SQRT1_2;// square root of 1/2
Math.SQRT1_2;// square root of 1/2
Math.SQRT2; //square root of 2</lang>
Math.SQRT2; //square root of 2</syntaxhighlight>


=={{header|Ada}}==
=={{header|Ada}}==
Most of the constants and functions used in this task are defined in the pre-defined Ada package Ada.Numerics.Elementary_Functions.
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
<syntaxhighlight lang="ada">Ada.Numerics.e -- Euler's number
Ada.Numerics.pi -- pi
Ada.Numerics.pi -- pi
sqrt(x) -- square root
sqrt(x) -- square root
Line 203: Line 203:
S'floor(x) -- Produces the floor of an instance of subtype S
S'floor(x) -- Produces the floor of an instance of subtype S
S'ceiling(x) -- Produces the ceiling 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>
x**y -- x raised to the y power</syntaxhighlight>


=={{header|Aime}}==
=={{header|Aime}}==
<lang aime># e
<syntaxhighlight lang="aime"># e
exp(1);
exp(1);
# pi
# pi
Line 217: Line 217:
floor(x);
floor(x);
ceil(x);
ceil(x);
pow(x, y);</lang>
pow(x, y);</syntaxhighlight>


=={{header|ALGOL 68}}==
=={{header|ALGOL 68}}==
<lang algol68>REAL x:=exp(1), y:=4*atan(1);
<syntaxhighlight lang="algol68">REAL x:=exp(1), y:=4*atan(1);
printf(($g(-8,5)"; "$,
printf(($g(-8,5)"; "$,
exp(1), # e #
exp(1), # e #
Line 232: Line 232:
-ENTIER -x, # ceiling #
-ENTIER -x, # ceiling #
x ** y # power #
x ** y # power #
))</lang>
))</syntaxhighlight>
{{out}}
{{out}}
<pre> 2.71828; 3.14159; 1.64872; 0.43429; 1.00000; 15.15426; 2.71828; 2.00000; 3.00000; 23.14069; </pre>
<pre> 2.71828; 3.14159; 1.64872; 0.43429; 1.00000; 15.15426; 2.71828; 2.00000; 3.00000; 23.14069; </pre>
Line 241: Line 241:


=={{header|ALGOL W}}==
=={{header|ALGOL W}}==
<lang algolw>begin
<syntaxhighlight lang="algolw">begin
real t, u;
real t, u;
t := 10;
t := 10;
Line 257: Line 257:
% the raise-to-the-power operator is "**" - it only allows integers for the power %
% the raise-to-the-power operator is "**" - it only allows integers for the power %
write( " pi cubed: ", pi ** 3 ) % use exp( ln( x ) * y ) for general x^y %
write( " pi cubed: ", pi ** 3 ) % use exp( ln( x ) * y ) for general x^y %
end.</lang>
end.</syntaxhighlight>


=={{header|ARM Assembly}}==
=={{header|ARM Assembly}}==


{{omit from|ARM Assembly}}
{{omit from|ARM Assembly}}
<lang>
<syntaxhighlight lang="text">
/* functions not availables */
/* functions not availables */
</syntaxhighlight>
</lang>


=={{header|Arturo}}==
=={{header|Arturo}}==
<lang rebol>print ["Euler:" e]
<syntaxhighlight lang="rebol">print ["Euler:" e]
print ["Pi:" pi]
print ["Pi:" pi]


Line 277: Line 277:
print ["floor 23.536:" floor 23.536]
print ["floor 23.536:" floor 23.536]
print ["ceil 23.536:" ceil 23.536]
print ["ceil 23.536:" ceil 23.536]
print ["2 ^ 8:" 2 ^ 8]</lang>
print ["2 ^ 8:" 2 ^ 8]</syntaxhighlight>


{{out}}
{{out}}
Line 293: Line 293:


=={{header|Asymptote}}==
=={{header|Asymptote}}==
<lang Asymptote>real e = exp(1); // e not available
<syntaxhighlight lang="asymptote">real e = exp(1); // e not available
write("e = ", e);
write("e = ", e);
write("pi = ", pi);
write("pi = ", pi);
Line 310: Line 310:
write("ceil = ", ceil(-e)); // ceiling
write("ceil = ", ceil(-e)); // ceiling
write("power = ", x ^ y); // power
write("power = ", x ^ y); // power
write("power = ", x ** y); // power</lang>
write("power = ", x ** y); // power</syntaxhighlight>
{{out}}
{{out}}
<pre>e = 2.71828182845905
<pre>e = 2.71828182845905
Line 328: Line 328:
=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==
The following math functions are built into AutoHotkey:
The following math functions are built into AutoHotkey:
<lang autohotkey>Sqrt(Number) ; square root
<syntaxhighlight lang="autohotkey">Sqrt(Number) ; square root
Log(Number) ; logarithm (base 10)
Log(Number) ; logarithm (base 10)
Ln(Number) ; natural logarithm (base e)
Ln(Number) ; natural logarithm (base e)
Line 335: Line 335:
Floor(Number) ; floor
Floor(Number) ; floor
Ceil(Number) ; ceiling
Ceil(Number) ; ceiling
x**y ; x to the power y</lang>
x**y ; x to the power y</syntaxhighlight>
No mathematical constants are built-in, but they can all be calculated:
No mathematical constants are built-in, but they can all be calculated:
<lang autohotkey>e:=exp(1)
<syntaxhighlight lang="autohotkey">e:=exp(1)
pi:=2*asin(1)</lang>
pi:=2*asin(1)</syntaxhighlight>
The following are additional trigonometric functions that are built into the AutoHotkey language:
The following are additional trigonometric functions that are built into the AutoHotkey language:
<lang autohotkey>Sin(Number) ; sine
<syntaxhighlight lang="autohotkey">Sin(Number) ; sine
Cos(Number) ; cosine
Cos(Number) ; cosine
Tan(Number) ; tangent
Tan(Number) ; tangent
ASin(Number) ; arcsine
ASin(Number) ; arcsine
ACos(Number) ; arccosine
ACos(Number) ; arccosine
ATan(Number) ; arctangent</lang>
ATan(Number) ; arctangent</syntaxhighlight>


=={{header|AWK}}==
=={{header|AWK}}==
Awk has square root, logarithm, exponential and power.
Awk has square root, logarithm, exponential and power.


<lang awk>BEGIN {
<syntaxhighlight lang="awk">BEGIN {
print sqrt(2) # square root
print sqrt(2) # square root
print log(2) # logarithm base e
print log(2) # logarithm base e
Line 356: Line 356:
print 2 ^ -3.4 # power
print 2 ^ -3.4 # power
}
}
# outputs 1.41421, 0.693147, 7.38906, 0.0947323</lang>
# outputs 1.41421, 0.693147, 7.38906, 0.0947323</syntaxhighlight>


<blockquote style="font-size: smaller;">'''Power's note:'''
<blockquote style="font-size: smaller;">'''Power's note:'''
Line 367: Line 367:
Awk misses e, pi, absolute value, floor and ceiling; but these are all easy to implement:
Awk misses e, pi, absolute value, floor and ceiling; but these are all easy to implement:


<lang awk>BEGIN {
<syntaxhighlight lang="awk">BEGIN {
E = exp(1)
E = exp(1)
PI = atan2(0, -1)
PI = atan2(0, -1)
Line 393: Line 393:
print ceil(-3.4) # ceiling
print ceil(-3.4) # ceiling
}
}
# outputs 2.71828, 3.14159, 3.4, -4, -3</lang>
# outputs 2.71828, 3.14159, 3.4, -4, -3</syntaxhighlight>


=={{header|Axe}}==
=={{header|Axe}}==
Line 400: Line 400:


To take the square root of an integer X:
To take the square root of an integer X:
<lang axe>√(X)</lang>
<syntaxhighlight lang="axe">√(X)</syntaxhighlight>


To take the square root of an 8.8 fixed-point number Y:
To take the square root of an 8.8 fixed-point number Y:
<lang axe>√(Y)ʳ</lang>
<syntaxhighlight lang="axe">√(Y)ʳ</syntaxhighlight>


To take the base-2 logarithm of an integer X:
To take the base-2 logarithm of an integer X:
<lang axe>ln(X)</lang>
<syntaxhighlight lang="axe">ln(X)</syntaxhighlight>


To take 2 raised to an integer X: (Note that the base is not Euler's number)
To take 2 raised to an integer X: (Note that the base is not Euler's number)
<lang axe>e^(X)</lang>
<syntaxhighlight lang="axe">e^(X)</syntaxhighlight>


To take the absolute value of a signed integer X:
To take the absolute value of a signed integer X:
<lang axe>abs(X)</lang>
<syntaxhighlight lang="axe">abs(X)</syntaxhighlight>


=={{header|BASIC}}==
=={{header|BASIC}}==
{{works with|QuickBasic|4.5}}
{{works with|QuickBasic|4.5}}
<lang qbasic>abs(x) 'absolute value
<syntaxhighlight lang="qbasic">abs(x) 'absolute value
sqr(x) 'square root
sqr(x) 'square root
exp(x) 'exponential
exp(x) 'exponential
log(x) 'natural logarithm
log(x) 'natural logarithm
x ^ y 'power
x ^ y 'power
'floor, ceiling, e, and pi not available</lang>
'floor, ceiling, e, and pi not available</syntaxhighlight>


==={{header|IS-BASIC}}===
==={{header|IS-BASIC}}===
<lang IS-BASIC>100 LET X=2:LET Y=5
<syntaxhighlight lang="is-basic">100 LET X=2:LET Y=5
110 PRINT EXP(1) ! value of e
110 PRINT EXP(1) ! value of e
120 PRINT PI ! value of Pi
120 PRINT PI ! value of Pi
Line 443: Line 443:
270 PRINT MAX(X,Y) ! the bigger number of x and y
270 PRINT MAX(X,Y) ! the bigger number of x and y
280 PRINT EPS(X) ! the smallest quantity that can be added to or subtracted from x to make the interpreter register a change in the value of x
280 PRINT EPS(X) ! the smallest quantity that can be added to or subtracted from x to make the interpreter register a change in the value of x
290 PRINT INF ! The largest positive number the tinterpreter can handle. This number is 9.999999999*10^62</lang>
290 PRINT INF ! The largest positive number the tinterpreter can handle. This number is 9.999999999*10^62</syntaxhighlight>


==={{header|Sinclair ZX81 BASIC}}===
==={{header|Sinclair ZX81 BASIC}}===
Line 449: Line 449:


Base of the natural logarithm:
Base of the natural logarithm:
<lang basic>EXP 1</lang>
<syntaxhighlight lang="basic">EXP 1</syntaxhighlight>


<math>\pi</math>:
<math>\pi</math>:
<lang basic>PI</lang>
<syntaxhighlight lang="basic">PI</syntaxhighlight>


Square root:
Square root:
<lang basic>SQR X</lang>
<syntaxhighlight lang="basic">SQR X</syntaxhighlight>


Natural logarithm:
Natural logarithm:
<lang basic>LN X</lang>
<syntaxhighlight lang="basic">LN X</syntaxhighlight>


Exponential:
Exponential:
<lang basic>EXP X</lang>
<syntaxhighlight lang="basic">EXP X</syntaxhighlight>


Absolute value:
Absolute value:
<lang basic>ABS X</lang>
<syntaxhighlight lang="basic">ABS X</syntaxhighlight>


Floor:
Floor:
<lang basic>INT X</lang>
<syntaxhighlight lang="basic">INT X</syntaxhighlight>
(NB. Although this function is called <code>INT</code>, it corresponds to <code>floor</code>: e.g. <code>INT -3.1</code> returns -4 not -3.)
(NB. Although this function is called <code>INT</code>, it corresponds to <code>floor</code>: e.g. <code>INT -3.1</code> returns -4 not -3.)


Line 474: Line 474:


Power:
Power:
<lang basic>X**Y</lang>
<syntaxhighlight lang="basic">X**Y</syntaxhighlight>
NB. Both <math>x</math> and <math>y</math> can be real numbers.
NB. Both <math>x</math> and <math>y</math> can be real numbers.


==={{header|BBC BASIC}}===
==={{header|BBC BASIC}}===
<lang bbcbasic> e = EXP(1)
<syntaxhighlight lang="bbcbasic"> e = EXP(1)
Pi = PI
Pi = PI
Sqr2 = SQR(2)
Sqr2 = SQR(2)
Line 491: Line 491:
DEF FNceil(n) = INT(n) - (INT(n) <> n)
DEF FNceil(n) = INT(n) - (INT(n) <> n)
</syntaxhighlight>
</lang>


=={{header|BASIC256}}==
=={{header|BASIC256}}==
<lang basic256>e = exp(1) # e not available
<syntaxhighlight lang="basic256">e = exp(1) # e not available
print "e = "; e
print "e = "; e
print "PI = "; PI
print "PI = "; PI
Line 509: Line 509:
print "floor = "; floor(-e) # floor
print "floor = "; floor(-e) # floor
print "ceil = "; ceil(-e) # ceiling
print "ceil = "; ceil(-e) # ceiling
print "power = "; x ^ y # power</lang>
print "power = "; x ^ y # power</syntaxhighlight>
{{out}}
{{out}}
<pre>e = 2.71828182846
<pre>e = 2.71828182846
Line 526: Line 526:
The language has square root and power, but power only works if the exponent is an integer.
The language has square root and power, but power only works if the exponent is an integer.


<lang bc>scale = 6
<syntaxhighlight lang="bc">scale = 6
sqrt(2) /* 1.414213 square root */
sqrt(2) /* 1.414213 square root */
4.3 ^ -2 /* .054083 power (integer exponent) */</lang>
4.3 ^ -2 /* .054083 power (integer exponent) */</syntaxhighlight>


The standard library has natural logarithm and exponential functions. It can calculate e and pi: e comes from the exponential function, while pi is four times the arctangent of one. The usual formulas can calculate the powers with fractional exponents, and the logarithms with any base.
The standard library has natural logarithm and exponential functions. It can calculate e and pi: e comes from the exponential function, while pi is four times the arctangent of one. The usual formulas can calculate the powers with fractional exponents, and the logarithms with any base.


{{libheader|bc -l}}
{{libheader|bc -l}}
<lang bc>scale = 6
<syntaxhighlight lang="bc">scale = 6
l(2) /* .693147 natural logarithm */
l(2) /* .693147 natural logarithm */
e(2) /* 7.389056 exponential */
e(2) /* 7.389056 exponential */
Line 543: Line 543:


e(l(2) * -3.4) /* .094734 2 to the power of -3.4 */
e(l(2) * -3.4) /* .094734 2 to the power of -3.4 */
l(1024) / l(2) /* 10.000001 logarithm base 2 of 1024 */</lang>
l(1024) / l(2) /* 10.000001 logarithm base 2 of 1024 */</syntaxhighlight>


The missing functions are absolute value, floor and ceiling. You can implement these functions, if you know what to do.
The missing functions are absolute value, floor and ceiling. You can implement these functions, if you know what to do.


{{trans|AWK}}
{{trans|AWK}}
<lang bc>/* absolute value */
<syntaxhighlight lang="bc">/* absolute value */
define v(x) {
define v(x) {
if (x < 0) return (-x)
if (x < 0) return (-x)
Line 582: Line 582:
v(-3.4) /* 3.4 absolute value */
v(-3.4) /* 3.4 absolute value */
f(-3.4) /* -4 floor */
f(-3.4) /* -4 floor */
g(-3.4) /* -3 ceiling */</lang>
g(-3.4) /* -3 ceiling */</syntaxhighlight>


=={{header|blz}}==
=={{header|blz}}==
The constant e
The constant e
<lang blz>{e}</lang>
<syntaxhighlight lang="blz">{e}</syntaxhighlight>


The constant pi
The constant pi
<lang blz>{pi}</lang>
<syntaxhighlight lang="blz">{pi}</syntaxhighlight>


Square root
Square root
<lang blz>x ** 0.5</lang>
<syntaxhighlight lang="blz">x ** 0.5</syntaxhighlight>


Logarithm (base n)
Logarithm (base n)
<lang blz>x __ n</lang>
<syntaxhighlight lang="blz">x __ n</syntaxhighlight>


Exponential
Exponential
<lang blz>{e} ** x</lang>
<syntaxhighlight lang="blz">{e} ** x</syntaxhighlight>


Absolute Value
Absolute Value
<lang blz>abs(x)</lang>
<syntaxhighlight lang="blz">abs(x)</syntaxhighlight>


Floor
Floor
<lang blz>floor(x)</lang>
<syntaxhighlight lang="blz">floor(x)</syntaxhighlight>


Ceiling
Ceiling
<lang blz>ceil(x)</lang>
<syntaxhighlight lang="blz">ceil(x)</syntaxhighlight>


Power x to the y
Power x to the y
<lang blz>x ** y</lang>
<syntaxhighlight lang="blz">x ** y</syntaxhighlight>


=={{header|Bracmat}}==
=={{header|Bracmat}}==
Bracmat has no real number type, but the constants <code>e</code> and <code>pi</code>, together with <code>i</code> can be used as symbols with the intended mathematical meaning in exponential functions.
Bracmat has no real number type, but the constants <code>e</code> and <code>pi</code>, together with <code>i</code> can be used as symbols with the intended mathematical meaning in exponential functions.
For example, differentiation <code>10^x</code> to <code>x</code>
For example, differentiation <code>10^x</code> to <code>x</code>
<lang bracmat>x \D (10^x) { \D is the differentiation operator }</lang>
<syntaxhighlight lang="bracmat">x \D (10^x) { \D is the differentiation operator }</syntaxhighlight>
has the result
has the result
<lang bracmat>10^x*e\L10 { \L is the logarithm operator }</lang>
<syntaxhighlight lang="bracmat">10^x*e\L10 { \L is the logarithm operator }</syntaxhighlight>
Likewise <code>e^(i*pi)</code> evaluates to <code>-1</code> and <code>e^(1/2*i*pi)</code> evaluates to <code>i</code>.
Likewise <code>e^(i*pi)</code> evaluates to <code>-1</code> and <code>e^(1/2*i*pi)</code> evaluates to <code>i</code>.


Line 635: Line 635:
=={{header|C}}==
=={{header|C}}==
Most of the following functions take a double.
Most of the following functions take a double.
<lang c>#include <math.h>
<syntaxhighlight lang="c">#include <math.h>


M_E; /* e - not standard but offered by most implementations */
M_E; /* e - not standard but offered by most implementations */
Line 646: Line 646:
floor(x); /* floor */
floor(x); /* floor */
ceil(x); /* ceiling */
ceil(x); /* ceiling */
pow(x,y); /* power */</lang>
pow(x,y); /* power */</syntaxhighlight>


To access the M_PI, etc. constants in Visual Studio, you may need to add the line <code>#define _USE_MATH_DEFINES</code> before the <code>#include <math.h></code>.
To access the M_PI, etc. constants in Visual Studio, you may need to add the line <code>#define _USE_MATH_DEFINES</code> before the <code>#include <math.h></code>.


=={{header|C sharp}}==
=={{header|C sharp}}==
<lang csharp>using System;
<syntaxhighlight lang="csharp">using System;


class Program {
class Program {
Line 666: Line 666:
Console.WriteLine(Math.Pow(2, 5)); // Exponentiation
Console.WriteLine(Math.Pow(2, 5)); // Exponentiation
}
}
}</lang>
}</syntaxhighlight>


=={{header|C++}}==
=={{header|C++}}==
=== using Math macros ===
=== using Math macros ===
<lang cpp>#include <iostream>
<syntaxhighlight lang="cpp">#include <iostream>
#include <cmath>
#include <cmath>


Line 697: Line 697:
<< "\nceiling(4.5) = " << std::ceil(4.5)
<< "\nceiling(4.5) = " << std::ceil(4.5)
<< "\npi^2 = " << std::pow(pi,2.0) << std::endl;
<< "\npi^2 = " << std::pow(pi,2.0) << std::endl;
}</lang>
}</syntaxhighlight>
=== using Boost ===
=== using Boost ===
{{libheader|Boost}}
{{libheader|Boost}}
<lang cpp>#include <iostream>
<syntaxhighlight lang="cpp">#include <iostream>
#include <iomanip>
#include <iomanip>
#include <cmath>
#include <cmath>
Line 718: Line 718:
<< "\nfloor(4.5) = " << std::floor(4.5)
<< "\nfloor(4.5) = " << std::floor(4.5)
<< "\nceiling(4.5) = " << std::ceil(4.5) << std::endl;
<< "\nceiling(4.5) = " << std::ceil(4.5) << std::endl;
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>e = 2.71828182845904509
<pre>e = 2.71828182845904509
Line 736: Line 736:
=={{header|Clojure}}==
=={{header|Clojure}}==
{{trans|Java}} which is directly available.
{{trans|Java}} which is directly available.
<lang lisp>(Math/E); //e
<syntaxhighlight lang="lisp">(Math/E); //e
(Math/PI); //pi
(Math/PI); //pi
(Math/sqrt x); //square root--cube root also available (cbrt)
(Math/sqrt x); //square root--cube root also available (cbrt)
Line 744: Line 744:
(Math/floor x); //floor
(Math/floor x); //floor
(Math/ceil x); //ceiling
(Math/ceil x); //ceiling
(Math/pow x y); //power</lang>
(Math/pow x y); //power</syntaxhighlight>


Clojure does provide arbitrary precision versions as well:
Clojure does provide arbitrary precision versions as well:


<lang lisp>(ns user (:require [clojure.contrib.math :as math]))
<syntaxhighlight lang="lisp">(ns user (:require [clojure.contrib.math :as math]))
(math/sqrt x)
(math/sqrt x)
(math/abs x)
(math/abs x)
(math/floor x)
(math/floor x)
(math/ceil x)
(math/ceil x)
(math/expt x y) </lang>
(math/expt x y) </syntaxhighlight>


.. and as multimethods that can be defined for any type (e.g. complex numbers).
.. and as multimethods that can be defined for any type (e.g. complex numbers).


<lang lisp>(ns user (:require [clojure.contrib.generic.math-functions :as generic]))
<syntaxhighlight lang="lisp">(ns user (:require [clojure.contrib.generic.math-functions :as generic]))
(generic/sqrt x)
(generic/sqrt x)
(generic/log x)
(generic/log x)
Line 764: Line 764:
(generic/floor x)
(generic/floor x)
(generic/ceil x)
(generic/ceil x)
(generic/pow x y)</lang>
(generic/pow x y)</syntaxhighlight>


=={{header|COBOL}}==
=={{header|COBOL}}==
Everything that follows can take any number (except for <code>SQRT</code> which expects a non-negative number).
Everything that follows can take any number (except for <code>SQRT</code> which expects a non-negative number).
The task constants and (intrinsic) functions:
The task constants and (intrinsic) functions:
<lang cobol>E *> e
<syntaxhighlight lang="cobol">E *> e
PI *> Pi
PI *> Pi
SQRT(n) *> Sqaure root
SQRT(n) *> Sqaure root
Line 781: Line 781:
MOVE INTEGER(N) TO Result
MOVE INTEGER(N) TO Result
*> There is no pow function, although the COMPUTE verb does have an exponention operator.
*> There is no pow function, although the COMPUTE verb does have an exponention operator.
COMPUTE Result = N ** 2 </lang>
COMPUTE Result = N ** 2 </syntaxhighlight>
COBOL also has the following extra mathematical functions:
COBOL also has the following extra mathematical functions:
<lang cobol>FACTORIAL(n) *> Factorial
<syntaxhighlight lang="cobol">FACTORIAL(n) *> Factorial
EXP10(n) *> 10 to the nth power
EXP10(n) *> 10 to the nth power
*> Trigonometric functions, including inverse ones, named as would be expected.</lang>
*> Trigonometric functions, including inverse ones, named as would be expected.</syntaxhighlight>


=={{header|Common Lisp}}==
=={{header|Common Lisp}}==
In Lisp we should really be talking about numbers rather than the type <code>real</code>. The types <code>real</code> and <code>complex</code> are subtypes of <code>number</code>. Math operations that accept or produce complex numbers generally do.
In Lisp we should really be talking about numbers rather than the type <code>real</code>. The types <code>real</code> and <code>complex</code> are subtypes of <code>number</code>. Math operations that accept or produce complex numbers generally do.
<lang lisp>
<syntaxhighlight lang="lisp">
(exp 1) ; e (Euler's number)
(exp 1) ; e (Euler's number)
pi ; pi constant
pi ; pi constant
Line 800: Line 800:
(ceiling x) ; ceiling: restricted to real, two valued (second value gives residue)
(ceiling x) ; ceiling: restricted to real, two valued (second value gives residue)
(expt x y) ; power
(expt x y) ; power
</syntaxhighlight>
</lang>


=={{header|Crystal}}==
=={{header|Crystal}}==
<lang ruby>x = 3.25
<syntaxhighlight lang="ruby">x = 3.25
y = 4
y = 4


Line 823: Line 823:
puts exp(x) # puts Math.exp(x) -- exponential
puts exp(x) # puts Math.exp(x) -- exponential
puts E**x # puts Math::E**x -- same
puts E**x # puts Math::E**x -- same
</syntaxhighlight>
</lang>


{{0ut}}<pre>
{{0ut}}<pre>
Line 843: Line 843:


=={{header|D}}==
=={{header|D}}==
<lang d>import std.math ; // need to import this module
<syntaxhighlight lang="d">import std.math ; // need to import this module
E // Euler's number
E // Euler's number
PI // pi constant
PI // pi constant
Line 854: Line 854:
floor(x) // floor
floor(x) // floor
ceil(x) // ceiling
ceil(x) // ceiling
pow(x,y) // power</lang>
pow(x,y) // power</syntaxhighlight>


=={{header|Delphi}}==
=={{header|Delphi}}==
Delphi supports all basic Standard Pascal (ISO 7185) functions shown in [[#Pascal|§&nbsp;Pascal]].
Delphi supports all basic Standard Pascal (ISO 7185) functions shown in [[#Pascal|§&nbsp;Pascal]].
Furthermore, the following is possible, too:
Furthermore, the following is possible, too:
<lang Delphi>Pi; // π (Pi)
<syntaxhighlight lang="delphi">Pi; // π (Pi)
LogN(BASE, x) // log of x for a specified base
LogN(BASE, x) // log of x for a specified base
Log2(x) // log of x for base 2
Log2(x) // log of x for base 2
Line 865: Line 865:
Floor(x); // floor
Floor(x); // floor
Ceil(x); // ceiling
Ceil(x); // ceiling
Power(x, y); // power</lang>
Power(x, y); // power</syntaxhighlight>
Note, <tt>Log</tt>, <tt>Floor</tt>, <tt>Ceil</tt> and <tt>Power</tt> are from the <tt>Math</tt> unit, which needs to be listed in the <tt>uses</tt>-clauses.
Note, <tt>Log</tt>, <tt>Floor</tt>, <tt>Ceil</tt> and <tt>Power</tt> are from the <tt>Math</tt> unit, which needs to be listed in the <tt>uses</tt>-clauses.


Line 872: Line 872:


=={{header|E}}==
=={{header|E}}==
<lang e>? 1.0.exp()
<syntaxhighlight lang="e">? 1.0.exp()
# value: 2.7182818284590455
# value: 2.7182818284590455


Line 897: Line 897:


? 10 ** 6
? 10 ** 6
# value: 1000000</lang>
# value: 1000000</syntaxhighlight>


=={{header|Elena}}==
=={{header|Elena}}==
ELENA 4.x :
ELENA 4.x :
<lang elena>import system'math;
<syntaxhighlight lang="elena">import system'math;
import extensions;
import extensions;
Line 916: Line 916:
console.printLine(10.0r.ceil()); //Ceiling
console.printLine(10.0r.ceil()); //Ceiling
console.printLine(2.power(5)); //Exponentiation
console.printLine(2.power(5)); //Exponentiation
}</lang>
}</syntaxhighlight>


=={{header|Elixir}}==
=={{header|Elixir}}==
<lang elixir>defmodule Real_constants_and_functions do
<syntaxhighlight lang="elixir">defmodule Real_constants_and_functions do
def main do
def main do
IO.puts :math.exp(1) # e
IO.puts :math.exp(1) # e
Line 934: Line 934:
end
end


Real_constants_and_functions.main</lang>
Real_constants_and_functions.main</syntaxhighlight>


=={{header|Elm}}==
=={{header|Elm}}==
The following are all in the Basics module, which is imported by default:
The following are all in the Basics module, which is imported by default:
<lang elm>e -- e
<syntaxhighlight lang="elm">e -- e
pi -- pi
pi -- pi
sqrt x -- square root
sqrt x -- square root
Line 946: Line 946:
floor x -- floor
floor x -- floor
ceiling x -- ceiling
ceiling x -- ceiling
2 ^ 3 -- power</lang>
2 ^ 3 -- power</syntaxhighlight>


=={{header|Erlang}}==
=={{header|Erlang}}==
<lang erlang>% Implemented by Arjun Sunel
<syntaxhighlight lang="erlang">% Implemented by Arjun Sunel
-module(math_constants).
-module(math_constants).
-export([main/0]).
-export([main/0]).
Line 984: Line 984:
false -> T + 1
false -> T + 1
end.
end.
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>2.718281828459045
<pre>2.718281828459045
Line 1,001: Line 1,001:


=={{header|ERRE}}==
=={{header|ERRE}}==
<lang ERRE>PROGRAM R_C_F
<syntaxhighlight lang="erre">PROGRAM R_C_F


FUNCTION CEILING(X)
FUNCTION CEILING(X)
Line 1,028: Line 1,028:
PRINT(CEILING(X)) ! ceiling
PRINT(CEILING(X)) ! ceiling
PRINT(X^Y) ! power
PRINT(X^Y) ! power
END PROGRAM</lang>
END PROGRAM</syntaxhighlight>
{{out}}
{{out}}
<pre> 2.718282
<pre> 2.718282
Line 1,045: Line 1,045:
=={{header|F Sharp|F#}}==
=={{header|F Sharp|F#}}==
{{trans|C#|C sharp}}
{{trans|C#|C sharp}}
<lang fsharp>open System
<syntaxhighlight lang="fsharp">open System


let main _ =
let main _ =
Line 1,059: Line 1,059:
Console.WriteLine(Math.Pow(2.0, 5.0)); // Exponentiation
Console.WriteLine(Math.Pow(2.0, 5.0)); // Exponentiation


0</lang>
0</syntaxhighlight>


=={{header|Factor}}==
=={{header|Factor}}==
<lang factor>e ! e
<syntaxhighlight lang="factor">e ! e
pi ! π
pi ! π
sqrt ! square root
sqrt ! square root
Line 1,072: Line 1,072:
truncate ! remove the fractional part (i.e. round towards 0)
truncate ! remove the fractional part (i.e. round towards 0)
round ! round to next whole number
round ! round to next whole number
^ ! power</lang>
^ ! power</syntaxhighlight>


=={{header|Fantom}}==
=={{header|Fantom}}==
Line 1,078: Line 1,078:
The <code>Float</code> class holds 64-bit floating point numbers, and contains most of the useful mathematical functions. A floating point number must be specified when entered with the suffix 'f', e.g. <code>9f</code>
The <code>Float</code> class holds 64-bit floating point numbers, and contains most of the useful mathematical functions. A floating point number must be specified when entered with the suffix 'f', e.g. <code>9f</code>


<lang fantom>
<syntaxhighlight lang="fantom">
Float.e
Float.e
Float.pi
Float.pi
Line 1,090: Line 1,090:
3.2f.round // nearest Int
3.2f.round // nearest Int
3f.pow(2f) // power
3f.pow(2f) // power
</syntaxhighlight>
</lang>


Note, . binds more tightly than -, so use brackets around negative numbers:
Note, . binds more tightly than -, so use brackets around negative numbers:
Line 1,102: Line 1,102:


=={{header|Forth}}==
=={{header|Forth}}==
<lang forth>1e fexp fconstant e
<syntaxhighlight lang="forth">1e fexp fconstant e
0e facos 2e f* fconstant pi \ predefined in gforth
0e facos 2e f* fconstant pi \ predefined in gforth
fsqrt ( f -- f )
fsqrt ( f -- f )
Line 1,110: Line 1,110:
floor ( f -- f ) \ round towards -inf
floor ( f -- f ) \ round towards -inf
: ceil ( f -- f ) fnegate floor fnegate ; \ not standard, though fround is available
: ceil ( f -- f ) fnegate floor fnegate ; \ not standard, though fround is available
f** ( f e -- f^e )</lang>
f** ( f e -- f^e )</syntaxhighlight>


=={{header|Fortran}}==
=={{header|Fortran}}==
<lang fortran> e ! Not available. Can be calculated EXP(1.0)
<syntaxhighlight lang="fortran"> e ! Not available. Can be calculated EXP(1.0)
pi ! Not available. Can be calculated 4.0*ATAN(1.0)
pi ! Not available. Can be calculated 4.0*ATAN(1.0)
SQRT(x) ! square root
SQRT(x) ! square root
Line 1,122: Line 1,122:
FLOOR(x) ! floor - Fortran 90 or later only
FLOOR(x) ! floor - Fortran 90 or later only
CEILING(x) ! ceiling - Fortran 90 or later only
CEILING(x) ! ceiling - Fortran 90 or later only
x**y ! x raised to the y power</lang>
x**y ! x raised to the y power</syntaxhighlight>


4*ATAN(1.0) will be calculated in single precision, likewise EXP(1.0) (not EXP(1), because 1 is an integer) and although double precision functions can be named explicitly, 4*DATAN(1.0) will be rejected because 1.0 is in single precision and DATAN expects double. Thus, 4*DATAN(1.0D0) or 4*DATAN(1D0) will do, as the D in the exponent form specifies double precision. Whereupon, the generic names can be returned to: 4*ATAN(1D0). Some systems go further and offer quadruple precision. Others allow that all constants will be deemed double precision as a compiler option.
4*ATAN(1.0) will be calculated in single precision, likewise EXP(1.0) (not EXP(1), because 1 is an integer) and although double precision functions can be named explicitly, 4*DATAN(1.0) will be rejected because 1.0 is in single precision and DATAN expects double. Thus, 4*DATAN(1.0D0) or 4*DATAN(1D0) will do, as the D in the exponent form specifies double precision. Whereupon, the generic names can be returned to: 4*ATAN(1D0). Some systems go further and offer quadruple precision. Others allow that all constants will be deemed double precision as a compiler option.
Line 1,129: Line 1,129:


=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
<lang freebasic>' FB 1.05.0 Win64
<syntaxhighlight lang="freebasic">' FB 1.05.0 Win64


#Include "crt/math.bi"
#Include "crt/math.bi"
Line 1,143: Line 1,143:
Print ceil(-2.5) '' ceiling function from C runtime library
Print ceil(-2.5) '' ceiling function from C runtime library
Print 2.5 ^ 3.5 '' exponentiation operator built into FB
Print 2.5 ^ 3.5 '' exponentiation operator built into FB
Sleep </lang>
Sleep </syntaxhighlight>


{{out}}
{{out}}
Line 1,164: Line 1,164:
=={{header|Frink}}==
=={{header|Frink}}==
All of the following operations work for any numerical type, including rational numbers, complex numbers and intervals of real numbers.
All of the following operations work for any numerical type, including rational numbers, complex numbers and intervals of real numbers.
<lang frink>
<syntaxhighlight lang="frink">
e
e
pi, π // Unicode can also be written in ASCII programs as \u03C0
pi, π // Unicode can also be written in ASCII programs as \u03C0
Line 1,175: Line 1,175:
ceil[x] // Except for complex numbers where there's no good interpretation.
ceil[x] // Except for complex numbers where there's no good interpretation.
x^y
x^y
</syntaxhighlight>
</lang>


=={{header|FutureBasic}}==
=={{header|FutureBasic}}==
<lang futurebasic>window 1
<syntaxhighlight lang="futurebasic">window 1


text ,,,,, 60// set tab width
text ,,,,, 60// set tab width
Line 1,193: Line 1,193:
print @"power:", 1.23 ^ 4
print @"power:", 1.23 ^ 4


HandleEvents</lang>
HandleEvents</syntaxhighlight>
Output:
Output:
<pre>
<pre>
Line 1,209: Line 1,209:


=={{header|Go}}==
=={{header|Go}}==
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 1,262: Line 1,262:
fmt.Println("x:", x)
fmt.Println("x:", x)
fmt.Println("abs(x):", y.Abs(x))
fmt.Println("abs(x):", y.Abs(x))
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,292: Line 1,292:


In addition to the java.lang.Math.abs() method, each numeric type has an abs() method, which can be invoked directly on the number:
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>
<syntaxhighlight lang="groovy">println ((-22).abs())</syntaxhighlight>
{{out}}
{{out}}
<pre>22</pre>
<pre>22</pre>
Line 1,299: Line 1,299:


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:
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>
<syntaxhighlight lang="groovy">println 22**3.5</syntaxhighlight>
{{out}}
{{out}}
<pre>49943.547010599876</pre>
<pre>49943.547010599876</pre>
Line 1,305: Line 1,305:
Power results are not defined for all possible pairs of operands.
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.
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>
<syntaxhighlight lang="groovy">println ((-22)**3.5)</syntaxhighlight>
{{out}}
{{out}}
<pre>NaN</pre>
<pre>NaN</pre>
Line 1,314: Line 1,314:
=={{header|Haskell}}==
=={{header|Haskell}}==
The operations are defined for the various numeric typeclasses, as defined in their type signature.
The operations are defined for the various numeric typeclasses, as defined in their type signature.
<lang haskell>exp 1 -- Euler number
<syntaxhighlight lang="haskell">exp 1 -- Euler number
pi -- pi
pi -- pi
sqrt x -- square root
sqrt x -- square root
Line 1,324: Line 1,324:
x ** y -- power (e.g. floating-point exponentiation)
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, nonnegative y only)
x ^^ y -- power (e.g. integer exponentiation of rationals, also negative y)</lang>
x ^^ y -- power (e.g. integer exponentiation of rationals, also negative y)</syntaxhighlight>


=={{header|HicEst}}==
=={{header|HicEst}}==
Except for x^y, this is identical to Fortran:
Except for x^y, this is identical to Fortran:
<lang HicEst>e ! Not available. Can be calculated EXP(1)
<syntaxhighlight lang="hicest">e ! Not available. Can be calculated EXP(1)
pi ! Not available. Can be calculated 4.0*ATAN(1.0)
pi ! Not available. Can be calculated 4.0*ATAN(1.0)
x^0.5 ! square root
x^0.5 ! square root
Line 1,338: Line 1,338:
CEILING(x) ! ceiling
CEILING(x) ! ceiling
x**y ! x raised to the y power
x**y ! x raised to the y power
x^y ! same as x**y</lang>
x^y ! same as x**y</syntaxhighlight>


=={{header|Icon}} and {{header|Unicon}}==
=={{header|Icon}} and {{header|Unicon}}==
<lang Icon>link numbers # for floor and ceil
<syntaxhighlight lang="icon">link numbers # for floor and ceil


procedure main()
procedure main()
Line 1,355: Line 1,355:
write("ceil(-2.2)=",ceil(-2.2))
write("ceil(-2.2)=",ceil(-2.2))
write("power: 3^3=",3^3)
write("power: 3^3=",3^3)
end</lang>
end</syntaxhighlight>
{{libheader|Icon Programming Library}}
{{libheader|Icon Programming Library}}
[http://www.cs.arizona.edu/icon/library/src/procs/numbers.icn numbers provides floor and ceiling]
[http://www.cs.arizona.edu/icon/library/src/procs/numbers.icn numbers provides floor and ceiling]
Line 1,373: Line 1,373:
=={{header|J}}==
=={{header|J}}==
The examples below require arguments (x and y) to be numeric nouns.
The examples below require arguments (x and y) to be numeric nouns.
<lang j>e =. 1x1 NB. Euler's number, specified as a numeric literal.
<syntaxhighlight lang="j">e =. 1x1 NB. Euler's number, specified as a numeric literal.
e =. ^ 1 NB. Euler's number, computed by exponentiation.
e =. ^ 1 NB. Euler's number, computed by exponentiation.
pi=. 1p1 NB. pi, specified as a numeric literal.
pi=. 1p1 NB. pi, specified as a numeric literal.
Line 1,386: Line 1,386:
square_root_of_x =. %:x NB. special form
square_root_of_x =. %:x NB. special form
square_root_of_x =. x^0.5 NB. exponential form
square_root_of_x =. x^0.5 NB. exponential form
x_to_the_y_power =. x^y</lang>
x_to_the_y_power =. x^y</syntaxhighlight>


=={{header|Java}}==
=={{header|Java}}==
All of these functions are in Java's <tt>Math</tt> class which, does not require any imports:
All of these functions are in Java's <tt>Math</tt> class which, does not require any imports:
<lang java>Math.E; //e
<syntaxhighlight lang="java">Math.E; //e
Math.PI; //pi
Math.PI; //pi
Math.sqrt(x); //square root--cube root also available (cbrt)
Math.sqrt(x); //square root--cube root also available (cbrt)
Line 1,398: Line 1,398:
Math.floor(x); //floor
Math.floor(x); //floor
Math.ceil(x); //ceiling
Math.ceil(x); //ceiling
Math.pow(x,y); //power</lang>
Math.pow(x,y); //power</syntaxhighlight>


=={{header|JavaScript}}==
=={{header|JavaScript}}==
<lang javascript>Math.E
<syntaxhighlight lang="javascript">Math.E
Math.PI
Math.PI
Math.sqrt(x)
Math.sqrt(x)
Line 1,409: Line 1,409:
Math.floor(x)
Math.floor(x)
Math.ceil(x)
Math.ceil(x)
Math.pow(x,y)</lang>
Math.pow(x,y)</syntaxhighlight>


=={{header|jq}}==
=={{header|jq}}==
Line 1,415: Line 1,415:
In jq, "." refers to the output coming from the left in the pipeline.
In jq, "." refers to the output coming from the left in the pipeline.


In the following, comments appear after the "#":<lang jq>
In the following, comments appear after the "#":<syntaxhighlight lang="jq">
1 | exp # i.e. e
1 | exp # i.e. e
1 | atan * 4 # i.e. π
1 | atan * 4 # i.e. π
Line 1,424: Line 1,424:
floor
floor
ceil # requires jq >= 1.5
ceil # requires jq >= 1.5
pow(x; y) # requires jq >= 1.5</lang>
pow(x; y) # requires jq >= 1.5</syntaxhighlight>


=={{header|Jsish}}==
=={{header|Jsish}}==
<lang javascript>/* real constants and functions, in JSI */
<syntaxhighlight lang="javascript">/* real constants and functions, in JSI */
var x, y;
var x, y;


Line 1,469: Line 1,469:
Math.pow(x,y) ==> 100000
Math.pow(x,y) ==> 100000
=!EXPECTEND!=
=!EXPECTEND!=
*/</lang>
*/</syntaxhighlight>


{{out}}
{{out}}
Line 1,494: Line 1,494:


=={{header|Julia}}==
=={{header|Julia}}==
<lang julia>e
<syntaxhighlight lang="julia">e
π, pi
π, pi
sqrt(x)
sqrt(x)
Line 1,502: Line 1,502:
floor(x)
floor(x)
ceil(x)
ceil(x)
x^y</lang>
x^y</syntaxhighlight>
Note that Julia supports Unicode identifiers, and allows either <code>π</code> or <code>pi</code> for that constant.
Note that Julia supports Unicode identifiers, and allows either <code>π</code> or <code>pi</code> for that constant.


Line 1,508: Line 1,508:


=={{header|Kotlin}}==
=={{header|Kotlin}}==
<lang scala>// version 1.0.6
<syntaxhighlight lang="scala">// version 1.0.6


fun main(args: Array<String>) {
fun main(args: Array<String>) {
Line 1,521: Line 1,521:
println(Math.ceil(-2.5)) // ceiling
println(Math.ceil(-2.5)) // ceiling
println(Math.pow(2.5, 3.5)) // power
println(Math.pow(2.5, 3.5)) // power
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 1,538: Line 1,538:


=={{header|Lambdatalk}}==
=={{header|Lambdatalk}}==
<lang scheme>
<syntaxhighlight lang="scheme">
{E} -> 2.718281828459045
{E} -> 2.718281828459045
{PI} -> 3.141592653589793
{PI} -> 3.141592653589793
Line 1,548: Line 1,548:
{ceil -2.5} -> -2
{ceil -2.5} -> -2
{pow 2.5 3.5} -> 24.705294220065465
{pow 2.5 3.5} -> 24.705294220065465
</syntaxhighlight>
</lang>


=={{header|Lasso}}==
=={{header|Lasso}}==
<syntaxhighlight lang="lasso">//e
<lang Lasso>//e
define e => 2.7182818284590452
define e => 2.7182818284590452


Line 1,566: Line 1,566:
1.64->floor
1.64->floor
1.64->ceil
1.64->ceil
1.64->pow(10.0)</lang>
1.64->pow(10.0)</syntaxhighlight>


=={{header|Liberty BASIC}}==
=={{header|Liberty BASIC}}==
Line 1,574: Line 1,574:
<br>
<br>
e & pi not available- calculate as shown.
e & pi not available- calculate as shown.
<syntaxhighlight lang="lb">
<lang lb>
print exp( 1) ' e not available
print exp( 1) ' e not available
print 4 *atn( 1) ' pi not available
print 4 *atn( 1) ' pi not available
Line 1,607: Line 1,607:
end if
end if
end function
end function
</syntaxhighlight>
</lang>


=={{header|Lingo}}==
=={{header|Lingo}}==
<lang lingo>the floatPrecision = 8
<syntaxhighlight lang="lingo">the floatPrecision = 8


-- e (base of the natural logarithm)
-- e (base of the natural logarithm)
Line 1,656: Line 1,656:
-- power
-- power
put power(2, 8)
put power(2, 8)
-- 256.00000000</lang>
-- 256.00000000</syntaxhighlight>


=={{header|LiveCode}}==
=={{header|LiveCode}}==
LC 7.1+, prior to this floor & ceil were not built-in.
LC 7.1+, prior to this floor & ceil were not built-in.
<lang LiveCode>e‬: exp(1)
<syntaxhighlight lang="livecode">e‬: exp(1)
pi: pi
pi: pi
square root: sqrt(x)
square root: sqrt(x)
Line 1,668: Line 1,668:
floor: floor(x)
floor: floor(x)
ceiling: ceil(x)
ceiling: ceil(x)
power: x^y</lang>
power: x^y</syntaxhighlight>


=={{header|Logo}}==
=={{header|Logo}}==
{{works with|UCB Logo}}
{{works with|UCB Logo}}
<lang logo>make "e exp 1
<syntaxhighlight lang="logo">make "e exp 1
make "pi 2*(RADARCTAN 0 1)
make "pi 2*(RADARCTAN 0 1)
sqrt :x
sqrt :x
Line 1,678: Line 1,678:
exp :x
exp :x
; there is no standard abs, floor, or ceiling; only INT and ROUND.
; there is no standard abs, floor, or ceiling; only INT and ROUND.
power :x :y</lang>
power :x :y</syntaxhighlight>


=={{header|Logtalk}}==
=={{header|Logtalk}}==
<lang logtalk>
<syntaxhighlight lang="logtalk">
:- object(constants_and_functions).
:- object(constants_and_functions).


Line 1,698: Line 1,698:


:- end_object.
:- end_object.
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 1,715: Line 1,715:


=={{header|Lua}}==
=={{header|Lua}}==
<lang lua>math.exp(1)
<syntaxhighlight lang="lua">math.exp(1)
math.pi
math.pi
math.sqrt(x)
math.sqrt(x)
Line 1,724: Line 1,724:
math.floor(x)
math.floor(x)
math.ceil(x)
math.ceil(x)
x^y</lang>
x^y</syntaxhighlight>


=={{header|M2000 Interpreter}}==
=={{header|M2000 Interpreter}}==
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Module Checkit {
Module Checkit {
Def exp(x)= 2.71828182845905^x
Def exp(x)= 2.71828182845905^x
Line 1,766: Line 1,766:
}
}
Checkit
Checkit
</syntaxhighlight>
</lang>


=={{header|Maple}}==
=={{header|Maple}}==
<lang Maple>> abs(ceil(floor(ln(exp(1)^sqrt(exp(Pi*I)+1)))));
<syntaxhighlight lang="maple">> abs(ceil(floor(ln(exp(1)^sqrt(exp(Pi*I)+1)))));
0</lang>
0</syntaxhighlight>


=={{header|Mathematica}}/{{header|Wolfram Language}}==
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">E
<lang Mathematica>E
Pi
Pi
Sqrt[x]
Sqrt[x]
Line 1,782: Line 1,782:
Floor[x]
Floor[x]
Ceiling[x]
Ceiling[x]
Power[x, y]</lang>
Power[x, y]</syntaxhighlight>
Where x is the number, and b the base.
Where x is the number, and b the base.
Exp[x] can also be inputted as E^x or E<sup>x</sup> and Power[x,y] can be also inputted as x^y or x<sup>y</sup>. All functions work with symbols, integers, floats and can be complex. Abs giving the modulus (|x|) if the argument is a complex number. Constant like E and Pi are kep unevaluated until someone explicitly tells it to give a numerical approximation: N[Pi,n] gives Pi to n-digit precision. Functions given an exact argument will be kept unevaluated if the answer can't be written more compact, approximate arguments will always be evaluated:
Exp[x] can also be inputted as E^x or E<sup>x</sup> and Power[x,y] can be also inputted as x^y or x<sup>y</sup>. All functions work with symbols, integers, floats and can be complex. Abs giving the modulus (|x|) if the argument is a complex number. Constant like E and Pi are kep unevaluated until someone explicitly tells it to give a numerical approximation: N[Pi,n] gives Pi to n-digit precision. Functions given an exact argument will be kept unevaluated if the answer can't be written more compact, approximate arguments will always be evaluated:
<lang Mathematica>Log[1.23] => 0.207014
<syntaxhighlight lang="mathematica">Log[1.23] => 0.207014
Log[10] => Log[10]
Log[10] => Log[10]
Log[10,100] => 2
Log[10,100] => 2
Line 1,800: Line 1,800:
Sqrt[-4] => 2I
Sqrt[-4] => 2I
Exp[2] => E^2
Exp[2] => E^2
Exp[Log[4]] => 4</lang>
Exp[Log[4]] => 4</syntaxhighlight>


=={{header|MATLAB}} / {{header|Octave}}==
=={{header|MATLAB}} / {{header|Octave}}==
<lang MATLAB>exp(1) % e
<syntaxhighlight lang="matlab">exp(1) % e
pi % pi
pi % pi
sqrt(x) % square root
sqrt(x) % square root
Line 1,813: Line 1,813:
floor(x) % floor
floor(x) % floor
ceil(x) % ceiling
ceil(x) % ceiling
x^y % power</lang>
x^y % power</syntaxhighlight>


=={{header|MAXScript}}==
=={{header|MAXScript}}==
<lang maxscript>e -- Euler's number
<syntaxhighlight lang="maxscript">e -- Euler's number
pi -- pi
pi -- pi
log x -- natural logarithm
log x -- natural logarithm
Line 1,824: Line 1,824:
floor x -- floor
floor x -- floor
ceil x -- ceiling
ceil x -- ceiling
pow x y -- power</lang>
pow x y -- power</syntaxhighlight>


=={{header|Mercury}}==
=={{header|Mercury}}==
<lang>
<syntaxhighlight lang="text">
math.pi % Pi.
math.pi % Pi.
math.e % Euler's number.
math.e % Euler's number.
Line 1,839: Line 1,839:
math.floor(X) % Floor of X.
math.floor(X) % Floor of X.
math.ceiling(X) % Ceiling of X.
math.ceiling(X) % Ceiling of X.
math.pow(X, Y) % X raised to the power of Y.</lang>
math.pow(X, Y) % X raised to the power of Y.</syntaxhighlight>


=={{header|Metafont}}==
=={{header|Metafont}}==
<lang metafont>show mexp(256); % outputs e; since MF uses mexp(x) = exp(x/256)
<syntaxhighlight 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
show 3.14159; % no pi constant built in; of course we can define it
% in several ways... even computing
% in several ways... even computing
Line 1,856: Line 1,856:
show ceiling(x); % ceiling
show ceiling(x); % ceiling
show x**y; % ** is not a built in: it is defined in the basic macros
show x**y; % ** is not a built in: it is defined in the basic macros
% set for Metafont (plain Metafont) as a primarydef</lang>
% set for Metafont (plain Metafont) as a primarydef</syntaxhighlight>


=={{header|min}}==
=={{header|min}}==
{{works with|min|0.19.3}}
{{works with|min|0.19.3}}
<lang min>e ; e
<syntaxhighlight lang="min">e ; e
pi ; π
pi ; π
sqrt ; square root
sqrt ; square root
Line 1,871: Line 1,871:
trunc ; remove the fractional part (i.e. round towards 0)
trunc ; remove the fractional part (i.e. round towards 0)
round ; round number to nth decimal place
round ; round number to nth decimal place
pow ; power</lang>
pow ; power</syntaxhighlight>


=={{header|МК-61/52}}==
=={{header|МК-61/52}}==
<lang>1 e^x С/П
<syntaxhighlight lang="text">1 e^x С/П


пи С/П
пи С/П
Line 1,892: Line 1,892:
x<0 14 ИП1 С/П ИП1 1 + С/П
x<0 14 ИП1 С/П ИП1 1 + С/П


x^y С/П</lang>
x^y С/П</syntaxhighlight>


=={{header|Modula-3}}==
=={{header|Modula-3}}==
Line 1,898: Line 1,898:


Note that all of these procedures (except the built ins) take <tt>LONGREAL</tt>s as their argument, and return <tt>LONGREAL</tt>s.
Note that all of these procedures (except the built ins) take <tt>LONGREAL</tt>s as their argument, and return <tt>LONGREAL</tt>s.
<lang modula3>Math.E;
<syntaxhighlight lang="modula3">Math.E;
Math.Pi;
Math.Pi;
Math.sqrt(x);
Math.sqrt(x);
Line 1,906: Line 1,906:
FLOOR(x); (* Built in function. *)
FLOOR(x); (* Built in function. *)
CEILING(x); (* Built in function. *)
CEILING(x); (* Built in function. *)
Math.pow(x, y);</lang>
Math.pow(x, y);</syntaxhighlight>


=={{header|Neko}}==
=={{header|Neko}}==
<syntaxhighlight lang="actionscript">/**
<lang ActionScript>/**
Real constants and functions, in Neko
Real constants and functions, in Neko
Tectonics:
Tectonics:
Line 1,936: Line 1,936:
$print("Floor(-2.2): ", math_floor(-2.2), "\n")
$print("Floor(-2.2): ", math_floor(-2.2), "\n")
$print("Ceil(-2.2) : ", math_ceil(-2.2), "\n")
$print("Ceil(-2.2) : ", math_ceil(-2.2), "\n")
$print("Pow(2, 8) : ", math_pow(2, 8), "\n")</lang>
$print("Pow(2, 8) : ", math_pow(2, 8), "\n")</syntaxhighlight>


{{out}}
{{out}}
Line 1,953: Line 1,953:
=={{header|NetRexx}}==
=={{header|NetRexx}}==
All the required constants and functions (and more) are in [[Java|Java's]] <tt>Math</tt> class. NetRexx also provides a limited set of built in numeric manipulation functions for it's Rexx object.
All the required constants and functions (and more) are in [[Java|Java's]] <tt>Math</tt> class. NetRexx also provides a limited set of built in numeric manipulation functions for it's Rexx object.
<lang NetRexx>/* NetRexx */
<syntaxhighlight lang="netrexx">/* NetRexx */
options replace format comments java crossref symbols nobinary utf8
options replace format comments java crossref symbols nobinary utf8


Line 2,002: Line 2,002:
say Rexx(' Truncate' x 'by' y':').left(pad) x.trunc(y)
say Rexx(' Truncate' x 'by' y':').left(pad) x.trunc(y)
say Rexx(' Format (with rounding)' x 'by' y':').left(pad) x.format(y, 0)
say Rexx(' Format (with rounding)' x 'by' y':').left(pad) x.format(y, 0)
</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 2,046: Line 2,046:


=={{header|Nim}}==
=={{header|Nim}}==
<lang nim>import math
<syntaxhighlight lang="nim">import math


var x, y = 12.5
var x, y = 12.5
Line 2,059: Line 2,059:
echo floor(x)
echo floor(x)
echo ceil(x)
echo ceil(x)
echo pow(x, y)</lang>
echo pow(x, y)</syntaxhighlight>


=={{header|Objeck}}==
=={{header|Objeck}}==
<lang objeck>Float->Pi();
<syntaxhighlight lang="objeck">Float->Pi();
Float->E();
Float->E();
4.0->SquareRoot();
4.0->SquareRoot();
Line 2,070: Line 2,070:
3.99->Floor();
3.99->Floor();
3.99->Ceiling();
3.99->Ceiling();
4.5->Ceiling(2.0);</lang>
4.5->Ceiling(2.0);</syntaxhighlight>


=={{header|OCaml}}==
=={{header|OCaml}}==
Unless otherwise noted, the following functions are for floats only:
Unless otherwise noted, the following functions are for floats only:
<lang ocaml>Float.pi (* pi *)
<syntaxhighlight lang="ocaml">Float.pi (* pi *)
sqrt x (* square root *)
sqrt x (* square root *)
log x (* natural logarithm--log base 10 also available (log10) *)
log x (* natural logarithm--log base 10 also available (log10) *)
Line 2,083: Line 2,083:
ceil x (* ceiling *)
ceil x (* ceiling *)
x ** y (* power *)
x ** y (* power *)
-. x (* negation for floats *)</lang>
-. x (* negation for floats *)</syntaxhighlight>


=={{header|Octave}}==
=={{header|Octave}}==
<lang octave>e % e
<syntaxhighlight lang="octave">e % e
pi % pi
pi % pi
sqrt(pi) % square root
sqrt(pi) % square root
Line 2,094: Line 2,094:
floor(pi) % floor
floor(pi) % floor
ceil(pi) % ceiling
ceil(pi) % ceiling
e**pi % power</lang>
e**pi % power</syntaxhighlight>


=={{header|Oforth}}==
=={{header|Oforth}}==


<lang Oforth>import: math
<syntaxhighlight lang="oforth">import: math


: testReal
: testReal
Line 2,121: Line 2,121:
-2.4 ceil println
-2.4 ceil println
-3.9 ceil println
-3.9 ceil println
-5.5 ceil println ;</lang>
-5.5 ceil println ;</syntaxhighlight>


=={{header|ooRexx}}==
=={{header|ooRexx}}==
{{trans|NetRexx}}
{{trans|NetRexx}}
{{uses from|OoRexx|RxMath}}
{{uses from|OoRexx|RxMath}}
<lang ooRexx>/* Rexx */
<syntaxhighlight lang="oorexx">/* Rexx */


-- MathLoadFuncs & MathDropFuncs are no longer needed and are effectively NOPs
-- MathLoadFuncs & MathDropFuncs are no longer needed and are effectively NOPs
Line 2,183: Line 2,183:
return arg(1)~trunc() + (arg(1) > 0) * (arg(1) \= arg(1)~trunc())
return arg(1)~trunc() + (arg(1) > 0) * (arg(1) \= arg(1)~trunc())


::requires 'RxMath' library</lang>
::requires 'RxMath' library</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 2,226: Line 2,226:


=={{header|Oz}}==
=={{header|Oz}}==
<lang oz>{ForAll
<syntaxhighlight lang="oz">{ForAll
[
[
{Exp 1.} %% 2.7183 Euler's number: not predefined
{Exp 1.} %% 2.7183 Euler's number: not predefined
Line 2,237: Line 2,237:
{Pow 2 3} %% 8 power; both arguments must be of the same type
{Pow 2 3} %% 8 power; both arguments must be of the same type
]
]
Show}</lang>
Show}</syntaxhighlight>


=={{header|PARI/GP}}==
=={{header|PARI/GP}}==
<lang parigp>[exp(1), Pi, sqrt(2), log(2), abs(2), floor(2), ceil(2), 2^3]</lang>
<syntaxhighlight lang="parigp">[exp(1), Pi, sqrt(2), log(2), abs(2), floor(2), ceil(2), 2^3]</syntaxhighlight>


=={{header|Pascal}}==
=={{header|Pascal}}==
''See also [[#Delphi|Delphi]] and [[#Free Pascal|Free Pascal]]''<br/>
''See also [[#Delphi|Delphi]] and [[#Free Pascal|Free Pascal]]''<br/>
Following functions are defined by ISO standard 7185, Standard “Unextended” Pascal, and supported by any processor:
Following functions are defined by ISO standard 7185, Standard “Unextended” Pascal, and supported by any processor:
<lang pascal> { Euler’s constant }
<syntaxhighlight lang="pascal"> { Euler’s constant }
exp(1)
exp(1)
{ principal square root of `x` }
{ principal square root of `x` }
Line 2,254: Line 2,254:
exp(x)
exp(x)
{ absolute value }
{ absolute value }
abs(x)</lang>
abs(x)</syntaxhighlight>
{{works with|Extended Pascal}}
{{works with|Extended Pascal}}
Additionally, in Extended Pascal (ISO standard 10206) following operators and expressions can be used:
Additionally, in Extended Pascal (ISO standard 10206) following operators and expressions can be used:
<lang pascal> { Pi }
<syntaxhighlight lang="pascal"> { Pi }
2 * arg(cmplx(0.0, maxReal))
2 * arg(cmplx(0.0, maxReal))
{ power, yields same data type as `base`, `exponent` has to be an `integer` }
{ power, yields same data type as `base`, `exponent` has to be an `integer` }
Line 2,264: Line 2,264:
{ `exponent` are automatically promoted to an approximate `real` value, result }
{ `exponent` are automatically promoted to an approximate `real` value, result }
{ is `complex` if `base` is `complex`, otherwise a `real` value }
{ is `complex` if `base` is `complex`, otherwise a `real` value }
base ** exponent</lang>
base ** exponent</syntaxhighlight>
<tt>Exp</tt>, <tt>sqrt</tt>, <tt>ln</tt>, and <tt>exp</tt> return <tt>real</tt> values, but a <tt>complex</tt> value if supplied with a <tt>complex</tt> value.
<tt>Exp</tt>, <tt>sqrt</tt>, <tt>ln</tt>, and <tt>exp</tt> return <tt>real</tt> values, but a <tt>complex</tt> value if supplied with a <tt>complex</tt> value.
<tt>Abs</tt> returns an <tt>integer</tt> value if supplied with an <tt>integer</tt>, otherwise a <tt>real</tt> value.
<tt>Abs</tt> returns an <tt>integer</tt> value if supplied with an <tt>integer</tt>, otherwise a <tt>real</tt> value.


=={{header|Perl}}==
=={{header|Perl}}==
<lang perl>use POSIX; # for floor() and ceil()
<syntaxhighlight lang="perl">use POSIX; # for floor() and ceil()


exp(1); # e
exp(1); # e
Line 2,285: Line 2,285:


use Math::Complex;
use Math::Complex;
pi; # alternate way to get pi</lang>
pi; # alternate way to get pi</syntaxhighlight>


=={{header|Phix}}==
=={{header|Phix}}==
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #0000FF;">?</span><span style="color: #004600;">E</span> <span style="color: #000080;font-style:italic;">-- Euler number</span>
<span style="color: #0000FF;">?</span><span style="color: #004600;">E</span> <span style="color: #000080;font-style:italic;">-- Euler number</span>
<span style="color: #0000FF;">?</span><span style="color: #004600;">PI</span> <span style="color: #000080;font-style:italic;">-- pi</span>
<span style="color: #0000FF;">?</span><span style="color: #004600;">PI</span> <span style="color: #000080;font-style:italic;">-- pi</span>
Line 2,304: Line 2,304:
<span style="color: #0000FF;">?</span><span style="color: #004600;">INVLN10</span> <span style="color: #000080;font-style:italic;">-- displays 0.434..</span>
<span style="color: #0000FF;">?</span><span style="color: #004600;">INVLN10</span> <span style="color: #000080;font-style:italic;">-- displays 0.434..</span>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">exp</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">/</span><span style="color: #004600;">INVLN10</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- displays 10.0</span>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">exp</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">/</span><span style="color: #004600;">INVLN10</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- displays 10.0</span>
<!--</lang>-->
<!--</syntaxhighlight>-->


=={{header|PHP}}==
=={{header|PHP}}==
<lang php>M_E; //e
<syntaxhighlight lang="php">M_E; //e
M_PI; //pi
M_PI; //pi
sqrt(x); //square root
sqrt(x); //square root
Line 2,315: Line 2,315:
floor(x); //floor
floor(x); //floor
ceil(x); //ceiling
ceil(x); //ceiling
pow(x,y); //power</lang>
pow(x,y); //power</syntaxhighlight>


=={{header|Picat}}==
=={{header|Picat}}==
<lang Picat>main =>
<syntaxhighlight lang="picat">main =>
println(math.e),
println(math.e),
println(math.pi),
println(math.pi),
Line 2,339: Line 2,339:
println(math.pi**math.e), % power
println(math.pi**math.e), % power
println(pow(math.pi,math.e)), % power
println(pow(math.pi,math.e)), % power
nl.</lang>
nl.</syntaxhighlight>


{{out}}
{{out}}
Line 2,367: Line 2,367:
(about 16 digits). The default precision is six, and can be changed with
(about 16 digits). The default precision is six, and can be changed with
'[http://software-lab.de/doc/refS.html#scl scl]':
'[http://software-lab.de/doc/refS.html#scl scl]':
<lang PicoLisp>(scl 12) # 12 places after decimal point
<syntaxhighlight lang="picolisp">(scl 12) # 12 places after decimal point
(load "@lib/math.l")
(load "@lib/math.l")


Line 2,382: Line 2,382:
(prinl (abs -123))
(prinl (abs -123))


(prinl (format (pow 3.0 4.0) *Scl)) # power</lang>
(prinl (format (pow 3.0 4.0) *Scl)) # power</syntaxhighlight>
{{out}}
{{out}}
<pre>2.718281828459
<pre>2.718281828459
Line 2,395: Line 2,395:


=={{header|PL/I}}==
=={{header|PL/I}}==
<lang pli>/* e not available other than by using exp(1q0).*/
<syntaxhighlight lang="pli">/* e not available other than by using exp(1q0).*/
/* pi not available other than by using a trig function such as: pi=4*atan(1) */
/* pi not available other than by using a trig function such as: pi=4*atan(1) */
y = sqrt(x);
y = sqrt(x);
Line 2,410: Line 2,410:
y = erfc(x); /* the error function complemented. */
y = erfc(x); /* the error function complemented. */
y = gamma (x);
y = gamma (x);
y = loggamma (x);</lang>
y = loggamma (x);</syntaxhighlight>


=={{header|Pop11}}==
=={{header|Pop11}}==
<lang pop11>pi ;;; Number Pi
<syntaxhighlight lang="pop11">pi ;;; Number Pi
sqrt(x) ;;; Square root
sqrt(x) ;;; Square root
log(x) ;;; Natural logarithm
log(x) ;;; Natural logarithm
exp(x) ;;; Exponential function
exp(x) ;;; Exponential function
abs(x) ;;; Absolute value
abs(x) ;;; Absolute value
x ** y ;;; x to the power y</lang>
x ** y ;;; x to the power y</syntaxhighlight>


The number e is not provided directly, one has to compute 'exp(1)'
The number e is not provided directly, one has to compute 'exp(1)'
Line 2,427: Line 2,427:
=={{header|PowerShell}}==
=={{header|PowerShell}}==
Since PowerShell has access to .NET all this can be achieved using the .NET Base Class Library:
Since PowerShell has access to .NET all this can be achieved using the .NET Base Class Library:
<lang powershell>Write-Host ([Math]::E)
<syntaxhighlight lang="powershell">Write-Host ([Math]::E)
Write-Host ([Math]::Pi)
Write-Host ([Math]::Pi)
Write-Host ([Math]::Sqrt(2))
Write-Host ([Math]::Sqrt(2))
Line 2,435: Line 2,435:
Write-Host ([Math]::Floor(3.14))
Write-Host ([Math]::Floor(3.14))
Write-Host ([Math]::Ceiling(3.14))
Write-Host ([Math]::Ceiling(3.14))
Write-Host ([Math]::Pow(2, 3))</lang>
Write-Host ([Math]::Pow(2, 3))</syntaxhighlight>


=={{header|PureBasic}}==
=={{header|PureBasic}}==
<lang PureBasic>Debug #E
<syntaxhighlight lang="purebasic">Debug #E
Debug #PI
Debug #PI
Debug Sqr(f)
Debug Sqr(f)
Line 2,445: Line 2,445:
Debug Log10(f)
Debug Log10(f)
Debug Abs(f)
Debug Abs(f)
Debug Pow(f,f)</lang>
Debug Pow(f,f)</syntaxhighlight>


=={{header|Python}}==
=={{header|Python}}==
<lang python>import math
<syntaxhighlight lang="python">import math


math.e # e
math.e # e
Line 2,463: Line 2,463:


# The math module constants and functions can, of course, be imported directly by:
# The math module constants and functions can, of course, be imported directly by:
# from math import e, pi, sqrt, log, log10, exp, floor, ceil</lang>
# from math import e, pi, sqrt, log, log10, exp, floor, ceil</syntaxhighlight>


=={{header|R}}==
=={{header|R}}==
<lang R>exp(1) # e
<syntaxhighlight lang="r">exp(1) # e
pi # pi
pi # pi
sqrt(x) # square root
sqrt(x) # square root
Line 2,476: Line 2,476:
floor(x) # floor
floor(x) # floor
ceiling(x) # ceiling
ceiling(x) # ceiling
x^y # power</lang>
x^y # power</syntaxhighlight>


=={{header|Racket}}==
=={{header|Racket}}==
<lang racket>(exp 1) ; e
<syntaxhighlight lang="racket">(exp 1) ; e
pi ; pi
pi ; pi
(sqrt x) ; square root
(sqrt x) ; square root
Line 2,487: Line 2,487:
(floor x) ; floor
(floor x) ; floor
(ceiling x) ; ceiling
(ceiling x) ; ceiling
(expt x y) ; power</lang>
(expt x y) ; power</syntaxhighlight>


=={{header|Raku}}==
=={{header|Raku}}==
(formerly Perl 6)
(formerly Perl 6)
<lang perl6>say e; # e
<syntaxhighlight lang="raku" line>say e; # e
say π; # or pi # pi
say π; # or pi # pi
say τ; # or tau # tau
say τ; # or tau # tau
Line 2,528: Line 2,528:
say pi.ceiling; # Ceiling
say pi.ceiling; # Ceiling


say e ** π\i + 1 ≅ 0; # :-)</lang>
say e ** π\i + 1 ≅ 0; # :-)</syntaxhighlight>


=={{header|REXX}}==
=={{header|REXX}}==
Line 2,535: Line 2,535:
REXX doesn't have any built-in (math) constants.
REXX doesn't have any built-in (math) constants.
===abs===
===abs===
<lang rexx>a=abs(y) /*takes the absolute value of y.*/</lang>
<syntaxhighlight lang="rexx">a=abs(y) /*takes the absolute value of y.*/</syntaxhighlight>
===exponentiation (**)===
===exponentiation (**)===
<lang rexx>r=x**y /*REXX only supports integer powers.*/
<syntaxhighlight lang="rexx">r=x**y /*REXX only supports integer powers.*/
/*Y may be negative, zero, positive.*/
/*Y may be negative, zero, positive.*/
/*X may be any real number. */</lang>
/*X may be any real number. */</syntaxhighlight>


===ceiling===
===ceiling===
A ceiling function for REXX:
A ceiling function for REXX:
<lang rexx>
<syntaxhighlight lang="rexx">
ceiling: procedure; parse arg x; t=trunc(x); return t+(x>0)*(x\=t)
ceiling: procedure; parse arg x; t=trunc(x); return t+(x>0)*(x\=t)
</syntaxhighlight>
</lang>


===floor===
===floor===
A floor function for REXX:
A floor function for REXX:
<lang rexx>
<syntaxhighlight lang="rexx">
floor: procedure; parse arg x; t=trunc(x); return t-(x<0)-(x\=t)
floor: procedure; parse arg x; t=trunc(x); return t-(x<0)-(x\=t)
</syntaxhighlight>
</lang>


===sqrt (optimized)===
===sqrt (optimized)===
A [principal] square root (SQRT) function for REXX &nbsp; (with arbitrary precision):
A [principal] square root (SQRT) function for REXX &nbsp; (with arbitrary precision):
<lang rexx>/*──────────────────────────────────SQRT subroutine───────────────────────────*/
<syntaxhighlight lang="rexx">/*──────────────────────────────────SQRT subroutine───────────────────────────*/
sqrt: procedure; parse arg x; if x=0 then return 0 /*handle 0 case.*/
sqrt: procedure; parse arg x; if x=0 then return 0 /*handle 0 case.*/
if \datatype(x,'N') then return '[n/a]' /*Not Applicable ───if not numeric.*/
if \datatype(x,'N') then return '[n/a]' /*Not Applicable ───if not numeric.*/
Line 2,581: Line 2,581:
/* [↓] normalize √ ──► original digits*/
/* [↓] normalize √ ──► original digits*/
numeric digits d /* [↓] make answer complex if X < 0. */
numeric digits d /* [↓] make answer complex if X < 0. */
return (g/1)i /*normalize, and add possible I suffix.*/</lang>
return (g/1)i /*normalize, and add possible I suffix.*/</syntaxhighlight>
<lang rexx> ╔════════════════════════════════════════════════════════════════════╗
<syntaxhighlight lang="rexx"> ╔════════════════════════════════════════════════════════════════════╗
╔═╝ __ ╚═╗
╔═╝ __ ╚═╗
║ √ ║
║ √ ║
Line 2,607: Line 2,607:
║ __ ║
║ __ ║
╚═╗ √ ╔═╝
╚═╗ √ ╔═╝
╚════════════════════════════════════════════════════════════════════╝</lang>
╚════════════════════════════════════════════════════════════════════╝</syntaxhighlight>


===sqrt (simple)===
===sqrt (simple)===
<lang rexx>/*──────────────────────────────────SQRT subroutine─────────────────────*/
<syntaxhighlight lang="rexx">/*──────────────────────────────────SQRT subroutine─────────────────────*/
sqrt: procedure; arg x /*a simplistic SQRT subroutine.*/
sqrt: procedure; arg x /*a simplistic SQRT subroutine.*/
if x=0 then return 0 /*handle special case of zero. */
if x=0 then return 0 /*handle special case of zero. */
Line 2,623: Line 2,623:
end /*forever*/ /* [↑] ···'til we run out of digs*/
end /*forever*/ /* [↑] ···'til we run out of digs*/
numeric digits d /*restore the original precision.*/
numeric digits d /*restore the original precision.*/
return g/1 /*normalize to old precision (d).*/</lang>
return g/1 /*normalize to old precision (d).*/</syntaxhighlight>


===other===
===other===
Other mathematical-type functions supported are:
Other mathematical-type functions supported are:
<lang rexx>numeric digits ddd /*sets the current precision to DDD */
<syntaxhighlight lang="rexx">numeric digits ddd /*sets the current precision to DDD */
numeric fuzz fff /*arithmetic comparisons with FFF fuzzy*/
numeric fuzz fff /*arithmetic comparisons with FFF fuzzy*/
numeric form kkk /*exponential: scientific | engineering*/
numeric form kkk /*exponential: scientific | engineering*/
Line 2,653: Line 2,653:
bb=x2b(hhh) /*converts hexadecimal to binary (bits)*/
bb=x2b(hhh) /*converts hexadecimal to binary (bits)*/
cc=x2c(hhh) /*converts hexadecimal to character. */
cc=x2c(hhh) /*converts hexadecimal to character. */
dd=x2d(hhh) /*converts hexadecimal to decimal. */</lang>
dd=x2d(hhh) /*converts hexadecimal to decimal. */</syntaxhighlight>


=={{header|Ring}}==
=={{header|Ring}}==
<lang ring>
<syntaxhighlight lang="ring">
See "Mathematical Functions" + nl
See "Mathematical Functions" + nl
See "Sin(0) = " + sin(0) + nl
See "Sin(0) = " + sin(0) + nl
Line 2,702: Line 2,702:


see "sqrt(16) = " + sqrt(16) + nl
see "sqrt(16) = " + sqrt(16) + nl
</syntaxhighlight>
</lang>


=={{header|RLaB}}==
=={{header|RLaB}}==
Line 2,709: Line 2,709:


RLaB has a number of mathematical constants built-in within the list ''const''. These facilities are provided through the Gnu Science Library [[http://www.gnu.org/software/gsl]].
RLaB has a number of mathematical constants built-in within the list ''const''. These facilities are provided through the Gnu Science Library [[http://www.gnu.org/software/gsl]].
<lang RLaB>>> const
<syntaxhighlight lang="rlab">>> const
e euler ln10 ln2 lnpi
e euler ln10 ln2 lnpi
log10e log2e pi pihalf piquarter
log10e log2e pi pihalf piquarter
rpi sqrt2 sqrt2r sqrt3 sqrtpi
rpi sqrt2 sqrt2r sqrt3 sqrtpi
tworpi</lang>
tworpi</syntaxhighlight>


=== Physical Constants ===
=== Physical Constants ===
Another list of physical constants and unit conversion factors exists and is called ''mks''.
Another list of physical constants and unit conversion factors exists and is called ''mks''.
Here the conversion goes between that particular unit and the equivalent unit in, one and only, metric system.
Here the conversion goes between that particular unit and the equivalent unit in, one and only, metric system.
<lang RLaB>>> mks
<syntaxhighlight lang="rlab">>> mks
F G J L N
F G J L N
Na R0 Ry Tsp V0
Na R0 Ry Tsp V0
Line 2,741: Line 2,741:
therm tntton ton torr toz
therm tntton ton torr toz
tsp uam ukgal ukton uston
tsp uam ukgal ukton uston
week yd</lang>
week yd</syntaxhighlight>


=== Elementary Functions ===
=== Elementary Functions ===
<lang RLaB>>> x = rand()
<syntaxhighlight lang="rlab">>> x = rand()
>> sqrt(x)
>> sqrt(x)
2.23606798
2.23606798
Line 2,760: Line 2,760:
5
5
>> x .^ 2
>> x .^ 2
25</lang>
25</syntaxhighlight>


=={{header|Ruby}}==
=={{header|Ruby}}==
<lang ruby>x.abs #absolute value
<syntaxhighlight lang="ruby">x.abs #absolute value
x.magnitude #absolute value
x.magnitude #absolute value
x.floor #floor
x.floor #floor
Line 2,776: Line 2,776:
log10(x) #base 10 logarithm
log10(x) #base 10 logarithm
exp(x) #exponential
exp(x) #exponential
</syntaxhighlight>
</lang>


=={{header|Run BASIC}}==
=={{header|Run BASIC}}==
<lang runbasic>print "exp:";chr$(9); EXP(1)
<syntaxhighlight lang="runbasic">print "exp:";chr$(9); EXP(1)
print "PI:";chr$(9); 22/7
print "PI:";chr$(9); 22/7
print "Sqr2:";chr$(9); SQR(2)
print "Sqr2:";chr$(9); SQR(2)
Line 2,787: Line 2,787:
print "Floor:";chr$(9); INT(1.534)
print "Floor:";chr$(9); INT(1.534)
print "ceil:";chr$(9); val(using("###",1.534))
print "ceil:";chr$(9); val(using("###",1.534))
print "Power:";chr$(9); 1.23^4</lang>
print "Power:";chr$(9); 1.23^4</syntaxhighlight>
<pre>exp: 2.71828183
<pre>exp: 2.71828183
PI: 3.14285707
PI: 3.14285707
Line 2,799: Line 2,799:


=={{header|Rust}}==
=={{header|Rust}}==
<lang rust>use std::f64::consts::*;
<syntaxhighlight lang="rust">use std::f64::consts::*;


fn main() {
fn main() {
Line 2,822: Line 2,822:


assert_eq!(x, 4.0);
assert_eq!(x, 4.0);
}</lang>
}</syntaxhighlight>


=={{header|Scala}}==
=={{header|Scala}}==
<lang scala>object RealConstantsFunctions extends App{
<syntaxhighlight lang="scala">object RealConstantsFunctions extends App{
println(math.E) // e
println(math.E) // e
println(math.Pi) // pi
println(math.Pi) // pi
Line 2,836: Line 2,836:
println(math.ceil(-2.5)) // ceiling
println(math.ceil(-2.5)) // ceiling
println(math.pow(2.5, 3.5)) // power
println(math.pow(2.5, 3.5)) // power
}</lang>
}</syntaxhighlight>


=={{header|Scheme}}==
=={{header|Scheme}}==
<lang scheme>(sqrt x) ;square root
<syntaxhighlight lang="scheme">(sqrt x) ;square root
(log x) ;natural logarithm
(log x) ;natural logarithm
(exp x) ;exponential
(exp x) ;exponential
Line 2,845: Line 2,845:
(floor x) ;floor
(floor x) ;floor
(ceiling x) ;ceiling
(ceiling x) ;ceiling
(expt x y) ;power</lang>
(expt x y) ;power</syntaxhighlight>


=={{header|Seed7}}==
=={{header|Seed7}}==
Line 2,875: Line 2,875:


=={{header|Sidef}}==
=={{header|Sidef}}==
<lang ruby>Num.e # e
<syntaxhighlight lang="ruby">Num.e # e
Num.pi # pi
Num.pi # pi
x.sqrt # square root
x.sqrt # square root
Line 2,884: Line 2,884:
x.floor # floor
x.floor # floor
x.ceil # ceiling
x.ceil # ceiling
x**y # exponentiation</lang>
x**y # exponentiation</syntaxhighlight>


=={{header|Slate}}==
=={{header|Slate}}==
<lang slate>numerics E.
<syntaxhighlight lang="slate">numerics E.
numerics Pi.
numerics Pi.
n sqrt.
n sqrt.
Line 2,897: Line 2,897:
n floor.
n floor.
n ceiling.
n ceiling.
n raisedTo: anotherNumber</lang>
n raisedTo: anotherNumber</syntaxhighlight>


=={{header|Smalltalk}}==
=={{header|Smalltalk}}==
<lang smalltalk>Float e.
<syntaxhighlight lang="smalltalk">Float e.
Float pi.
Float pi.
aNumber sqrt.
aNumber sqrt.
Line 2,909: Line 2,909:
aNumber floor.
aNumber floor.
aNumber ceiling.
aNumber ceiling.
aNumber raisedTo: anotherNumber</lang>
aNumber raisedTo: anotherNumber</syntaxhighlight>


=={{header|Sparkling}}==
=={{header|Sparkling}}==
<lang sparkling>// e:
<syntaxhighlight lang="sparkling">// e:
print(M_E);
print(M_E);


Line 2,942: Line 2,942:


// power
// power
let eighty_one = pow(3, 4);</lang>
let eighty_one = pow(3, 4);</syntaxhighlight>


=={{header|Standard ML}}==
=={{header|Standard ML}}==
<lang sml>Math.e; (* e *)
<syntaxhighlight lang="sml">Math.e; (* e *)
Math.pi; (* pi *)
Math.pi; (* pi *)
Math.sqrt x; (* square root *)
Math.sqrt x; (* square root *)
Line 2,954: Line 2,954:
ceil x; (* ceiling *)
ceil x; (* ceiling *)
Math.pow (x, y); (* power *)
Math.pow (x, y); (* power *)
~ x; (* negation *)</lang>
~ x; (* negation *)</syntaxhighlight>


=={{header|Stata}}==
=={{header|Stata}}==
<lang stata>scalar x=2
<syntaxhighlight lang="stata">scalar x=2
scalar y=3
scalar y=3
di exp(1)
di exp(1)
Line 2,969: Line 2,969:
di floor(x)
di floor(x)
di ceil(x)
di ceil(x)
di x^y</lang>
di x^y</syntaxhighlight>


=={{header|Swift}}==
=={{header|Swift}}==
<lang swift>import Darwin
<syntaxhighlight lang="swift">import Darwin


M_E // e
M_E // e
Line 2,982: Line 2,982:
floor(x) // floor
floor(x) // floor
ceil(x) // ceiling
ceil(x) // ceiling
pow(x,y) // power</lang>
pow(x,y) // power</syntaxhighlight>


=={{header|Tcl}}==
=={{header|Tcl}}==
<lang tcl>expr {exp(1)} ;# e
<syntaxhighlight lang="tcl">expr {exp(1)} ;# e
expr {4 * atan(1)} ;# pi -- also, simpler: expr acos(-1)
expr {4 * atan(1)} ;# pi -- also, simpler: expr acos(-1)
expr {sqrt($x)} ;# square root
expr {sqrt($x)} ;# square root
Line 2,993: Line 2,993:
expr {floor($x)} ;# floor
expr {floor($x)} ;# floor
expr {ceil($x)} ;# ceiling
expr {ceil($x)} ;# ceiling
expr {$x**$y} ;# power, also pow($x,$y)</lang>
expr {$x**$y} ;# power, also pow($x,$y)</syntaxhighlight>
The constants <math>e</math> and <math>\pi</math> are also available with high precision in a support library.
The constants <math>e</math> and <math>\pi</math> are also available with high precision in a support library.
{{tcllib|math::constants}}
{{tcllib|math::constants}}
<lang tcl>package require math::constants
<syntaxhighlight lang="tcl">package require math::constants
math::constants::constants e pi
math::constants::constants e pi
puts "e = $e, pi = $pi"</lang>
puts "e = $e, pi = $pi"</syntaxhighlight>


=={{header|TI-89 BASIC}}==
=={{header|TI-89 BASIC}}==
Line 3,025: Line 3,025:


=={{header|True BASIC}}==
=={{header|True BASIC}}==
<lang qbasic>FUNCTION floor(x)
<syntaxhighlight lang="qbasic">FUNCTION floor(x)
IF x > 0 THEN
IF x > 0 THEN
LET floor = INT(x)
LET floor = INT(x)
Line 3,049: Line 3,049:
PRINT "ceil = "; CEIL(x) ! ceiling
PRINT "ceil = "; CEIL(x) ! ceiling
PRINT "power = "; x ^ y ! power
PRINT "power = "; x ^ y ! power
END</lang>
END</syntaxhighlight>


=={{header|UNIX Shell}}==
=={{header|UNIX Shell}}==
{{works with|ksh93}}
{{works with|ksh93}}
ksh93 exposes math functions from the C math library
ksh93 exposes math functions from the C math library
<lang bash>echo $(( exp(1) )) # e
<syntaxhighlight lang="bash">echo $(( exp(1) )) # e
echo $(( acos(-1) )) # PI
echo $(( acos(-1) )) # PI
x=5
x=5
Line 3,068: Line 3,068:
echo $(( ceil(x) )) # ceiling
echo $(( ceil(x) )) # ceiling
x=10 y=3
x=10 y=3
echo $(( pow(x,y) )) # power</lang>
echo $(( pow(x,y) )) # power</syntaxhighlight>


{{out}}
{{out}}
Line 3,084: Line 3,084:


=={{header|Wren}}==
=={{header|Wren}}==
<lang ecmascript>var e = 1.exp
<syntaxhighlight lang="ecmascript">var e = 1.exp


System.print("e = %(e)")
System.print("e = %(e)")
Line 3,094: Line 3,094:
System.print("floor(e) = %(e.floor)")
System.print("floor(e) = %(e.floor)")
System.print("ceil(e) = %(e.ceil)")
System.print("ceil(e) = %(e.ceil)")
System.print("pow(e, 2) = %(e.pow(2))")</lang>
System.print("pow(e, 2) = %(e.pow(2))")</syntaxhighlight>


{{out}}
{{out}}
Line 3,110: Line 3,110:


=={{header|XPL0}}==
=={{header|XPL0}}==
<lang XPL0>include c:\cxpl\codes; \intrinsic 'code' declarations
<syntaxhighlight lang="xpl0">include c:\cxpl\codes; \intrinsic 'code' declarations


func real Power(X, Y); \X raised to the Y power
func real Power(X, Y); \X raised to the Y power
Line 3,135: Line 3,135:
RlOut(0, float(fix(1.001+0.5))); CrLf(0); \ceiling rounds toward +infinity
RlOut(0, float(fix(1.001+0.5))); CrLf(0); \ceiling rounds toward +infinity
RlOut(0, Power(sqrt(2.0), 4.0)); CrLf(0); \sqrt is an inline function and
RlOut(0, Power(sqrt(2.0), 4.0)); CrLf(0); \sqrt is an inline function and
] \ can be used for both reals & ints</lang>
] \ can be used for both reals & ints</syntaxhighlight>


{{out}}
{{out}}
Line 3,157: Line 3,157:


=={{header|Yabasic}}==
=={{header|Yabasic}}==
<lang yabasic>print "e = ", euler
<syntaxhighlight lang="yabasic">print "e = ", euler
print "pi = ", pi
print "pi = ", pi
Line 3,171: Line 3,171:
print "ceil = ", ceil(-euler) // ceiling
print "ceil = ", ceil(-euler) // ceiling
print "power = ", x ^ y, " ", x ** y // power
print "power = ", x ^ y, " ", x ** y // power
end</lang>
end</syntaxhighlight>
{{out}}
{{out}}
<pre>e = 2.71828
<pre>e = 2.71828
Line 3,185: Line 3,185:


=={{header|Zig}}==
=={{header|Zig}}==
<lang zig>const std = @import("std");
<syntaxhighlight lang="zig">const std = @import("std");


pub fn main() void {
pub fn main() void {
Line 3,198: Line 3,198:
std.debug.print("ceil(x) = {d}\n", .{std.math.ceil(x)});
std.debug.print("ceil(x) = {d}\n", .{std.math.ceil(x)});
std.debug.print("pow(f64, -x, x) = {d}\n", .{std.math.pow(f64, -x, x)});
std.debug.print("pow(f64, -x, x) = {d}\n", .{std.math.pow(f64, -x, x)});
}</lang>
}</syntaxhighlight>


=={{header|zkl}}==
=={{header|zkl}}==
<lang zkl>(0.0).e // Euler's number, a property of all floats
<syntaxhighlight lang="zkl">(0.0).e // Euler's number, a property of all floats
(0.0).e.pi // pi, yep, all floats
(0.0).e.pi // pi, yep, all floats
(2.0).sqrt() // square root
(2.0).sqrt() // square root
Line 3,210: Line 3,210:
x.pow(y) // x raised to the y power
x.pow(y) // x raised to the y power
x.ceil() // ceiling
x.ceil() // ceiling
x.floor() // floor</lang>
x.floor() // floor</syntaxhighlight>


{{omit from|GUISS}}
{{omit from|GUISS}}