Real constants and functions: Difference between revisions

→‎{{header|BASIC}}: Added ANSI BASIC.
(Added 11l)
(→‎{{header|BASIC}}: Added ANSI BASIC.)
(36 intermediate revisions by 23 users not shown)
Line 23:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">math:e // e
math:pi // pi
sqrt(x) // square root
Line 32:
floor(x) // floor
ceil(x) // ceiling
x ^ y // exponentiation</langsyntaxhighlight>
 
=={{header|6502 Assembly}}==
None of these are built-in, irrational constants are best implemented with lookup tables.
Absolute value can be handled like so:
 
<syntaxhighlight lang="6502asm">GetAbs: ;assumes value we want to abs() is loaded into accumulator
eor #$ff
clc
adc #1
rts</syntaxhighlight>
 
 
=={{header|ACL2}}==
Only the last three are available as built in functions.
 
<langsyntaxhighlight Lisplang="lisp">(floor 15 2) ;; This is the floor of 15/2
(ceiling 15 2)
(expt 15 2) ;; 15 squared</langsyntaxhighlight> =={{header|ACL2}}==
Only the last three are available as built in functions.
 
Line 45 ⟶ 56:
define them using integer part:
 
<langsyntaxhighlight lang="pop11">define floor(x);
if x < 0 then
-intof(x);
Line 55 ⟶ 66:
define ceiling(x);
-floor(-x);
enddefine;</langsyntaxhighlight>
 
=={{header|Action!}}==
Part of the solution can be find in [http://www.rosettacode.org/wiki/Category:Action!_Real_Math REALMATH.ACT].
{{libheader|Action! Tool Kit}}
{{libheader|Action! Real Math}}
<syntaxhighlight lang="action!">INCLUDE "H6:REALMATH.ACT"
 
PROC Euler(REAL POINTER e)
REAL x
 
IntToReal(1,x)
Exp(x,e)
RETURN
 
PROC Main()
REAL a,b,c
INT i
 
Put(125) PutE() ;clear screen
MathInit()
 
Euler(a)
Print("e=") PrintR(a)
PrintE(" by Exp(1)")
 
ValR("2",a)
Sqrt(a,b)
Print("Sqrt(") PrintR(a)
Print(")=") PrintR(b)
Print(" by Power(") PrintR(a)
PrintE(",0.5)")
 
ValR("2.5",a)
Ln(a,b)
Print("Ln(") PrintR(a)
Print(")=") PrintRE(b)
 
ValR("14.2",a)
Log10(a,b)
Print("Log10(") PrintR(a)
Print(")=") PrintRE(b)
 
ValR("-3.7",a)
Exp(a,b)
Print("Exp(") PrintR(a)
Print(")=") PrintRE(b)
 
ValR("2.6",a)
Exp10(a,b)
Print("Exp10(") PrintR(a)
Print(")=") PrintRE(b)
 
ValR("25.3",a)
ValR("1.3",b)
Power(a,b,c)
Print("Power(") PrintR(a)
Print(",") PrintR(b)
Print(")=") PrintRE(c)
 
ValR("-32.5",a)
RealAbs(a,b)
Print("Abs(") PrintR(a)
Print(")=") PrintR(b)
PrintE(" by bit manipulation")
 
ValR("23.15",a)
i=Floor(a)
Print("Floor(") PrintR(a)
PrintF(")=%I by own function%E",i)
 
ValR("-23.15",a)
i=Floor(a)
Print("Floor(") PrintR(a)
PrintF(")=%I by own function%E",i)
 
ValR("23.15",a)
i=Ceiling(a)
Print("Ceiling(") PrintR(a)
PrintF(")=%I by own function%E",i)
 
ValR("-23.15",a)
i=Ceiling(a)
Print("Ceiling(") PrintR(a)
PrintF(")=%I by own function%E",i)
 
PutE()
PrintE("There is no support in Action! for pi.")
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Real_constants_and_functions.png Screenshot from Atari 8-bit computer]
<pre>
e=2.71828179 by Exp(1)
Sqrt(2)=1.41421355 by Power(2,0.5)
Ln(2.5)=.9162907319
Log10(14.2)=1.15228834
Exp(-3.7)=.0247235365
Exp10(2.6)=398.106988
Power(25.3,1.3)=66.6893784
Abs(-32.5)=32.5 by bit manipulation
Floor(23.15)=23 by own function
Floor(-23.15)=-24 by own function
Ceiling(23.15)=24 by own function
Ceiling(-23.15)=-23 by own function
 
There is no support in Action! for pi.
</pre>
 
=={{header|ActionScript}}==
Actionscript has all the functions and constants mentioned in the task, available in the Math class.
<langsyntaxhighlight ActionScriptlang="actionscript">Math.E; //e
Math.PI; //pi
Math.sqrt(u); //square root of u
Line 67 ⟶ 184:
Math.floor(u);//floor of u
Math.ceil(u); //ceiling of u
Math.pow(u,v);//u to the power of v</langsyntaxhighlight>
The Math class also contains several other constants.
<langsyntaxhighlight ActionScriptlang="actionscript">Math.LN10; // natural logarithm of 10
Math.LN2; // natural logarithm of 2
Math.LOG10E; // base-10 logarithm of e
Math.LOG2E; // base-2 logarithm of e
Math.SQRT1_2;// square root of 1/2
Math.SQRT2; //square root of 2</langsyntaxhighlight>
 
=={{header|Ada}}==
Most of the constants and functions used in this task are defined in the pre-defined Ada package Ada.Numerics.Elementary_Functions.
<langsyntaxhighlight lang="ada">Ada.Numerics.e -- Euler's number
Ada.Numerics.pi -- pi
sqrt(x) -- square root
Line 86 ⟶ 203:
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</langsyntaxhighlight>
 
=={{header|Aime}}==
<langsyntaxhighlight lang="aime"># e
exp(1);
# pi
Line 100 ⟶ 217:
floor(x);
ceil(x);
pow(x, y);</langsyntaxhighlight>
 
=={{header|ALGOL 68}}==
<langsyntaxhighlight lang="algol68">REAL x:=exp(1), y:=4*atan(1);
printf(($g(-8,5)"; "$,
exp(1), # e #
Line 115 ⟶ 232:
-ENTIER -x, # ceiling #
x ** y # power #
))</langsyntaxhighlight>
{{out}}
<pre> 2.71828; 3.14159; 1.64872; 0.43429; 1.00000; 15.15426; 2.71828; 2.00000; 3.00000; 23.14069; </pre>
Line 124 ⟶ 241:
 
=={{header|ALGOL W}}==
<langsyntaxhighlight lang="algolw">begin
real t, u;
t := 10;
Line 140 ⟶ 257:
% 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 %
end.</langsyntaxhighlight>
 
=={{header|ARM Assembly}}==
 
{{omit from|ARM Assembly}}
<syntaxhighlight lang="text">
/* functions not availables */
</syntaxhighlight>
</lang>
 
=={{header|Arturo}}==
<syntaxhighlight lang="rebol">print ["Euler:" e]
print ["Pi:" pi]
 
print ["sqrt 2.0:" sqrt 2.0]
print ["ln 100:" ln 100]
print ["log(10) 100:" log 100 10]
print ["exp 3:" exp 3]
print ["abs -1:" abs neg 1]
print ["floor 23.536:" floor 23.536]
print ["ceil 23.536:" ceil 23.536]
print ["2 ^ 8:" 2 ^ 8]</syntaxhighlight>
 
{{out}}
 
<pre>Euler: 2.718281828459045
Pi: 3.141592653589793
sqrt 2.0: 1.414213562373095
ln 100: 4.605170185988092
log(10) 100: 2.0
exp 3: 20.08553692318767
abs -1: 1
floor 23.536: 23
ceil 23.536: 24
2 ^ 8: 256</pre>
 
=={{header|Asymptote}}==
<syntaxhighlight lang="asymptote">real e = exp(1); // e not available
write("e = ", e);
write("pi = ", pi);
real x = 12.345;
real y = 1.23;
write("sqrt = ", sqrt(2)); // square root
write("ln = ", log(e)); // natural logarithm base e
write("log = ", log10(x)); // base 10 logarithm
write("log1p = ", log1p(x)); // log (1+x)
write("exp = ", exp(e)); // exponential
write("abs = ", abs(-1)); // absolute value
write("fabs = ", fabs(-1)); // absolute value
write("floor = ", floor(-e)); // floor
write("ceil = ", ceil(-e)); // ceiling
write("power = ", x ^ y); // power
write("power = ", x ** y); // power</syntaxhighlight>
{{out}}
<pre>e = 2.71828182845905
pi = 3.14159265358979
sqrt = 1.4142135623731
ln = 1
log = 1.09149109426795
log1p = 2.59114178285649
exp = 15.1542622414793
abs = 1
fabs = 1
floor = -3
ceil = -2
power = 22.0056421323763
power = 22.0056421323763</pre>
 
=={{header|AutoHotkey}}==
The following math functions are built into AutoHotkey:
<langsyntaxhighlight lang="autohotkey">Sqrt(Number) ; square root
Log(Number) ; logarithm (base 10)
Ln(Number) ; natural logarithm (base e)
Line 158 ⟶ 335:
Floor(Number) ; floor
Ceil(Number) ; ceiling
x**y ; x to the power y</langsyntaxhighlight>
No mathematical constants are built-in, but they can all be calculated:
<langsyntaxhighlight lang="autohotkey">e:=exp(1)
pi:=2*asin(1)</langsyntaxhighlight>
The following are additional trigonometric functions that are built into the AutoHotkey language:
<langsyntaxhighlight lang="autohotkey">Sin(Number) ; sine
Cos(Number) ; cosine
Tan(Number) ; tangent
ASin(Number) ; arcsine
ACos(Number) ; arccosine
ATan(Number) ; arctangent</langsyntaxhighlight>
 
=={{header|AWK}}==
Awk has square root, logarithm, exponential and power.
 
<langsyntaxhighlight lang="awk">BEGIN {
print sqrt(2) # square root
print log(2) # logarithm base e
Line 179 ⟶ 356:
print 2 ^ -3.4 # power
}
# outputs 1.41421, 0.693147, 7.38906, 0.0947323</langsyntaxhighlight>
 
<blockquote style="font-size: smaller;">'''Power's note:'''
Line 190 ⟶ 367:
Awk misses e, pi, absolute value, floor and ceiling; but these are all easy to implement:
 
<langsyntaxhighlight lang="awk">BEGIN {
E = exp(1)
PI = atan2(0, -1)
Line 216 ⟶ 393:
print ceil(-3.4) # ceiling
}
# outputs 2.71828, 3.14159, 3.4, -4, -3</langsyntaxhighlight>
 
=={{header|Axe}}==
Line 223 ⟶ 400:
 
To take the square root of an integer X:
<syntaxhighlight lang ="axe">√(X)</langsyntaxhighlight>
 
To take the square root of an 8.8 fixed-point number Y:
<syntaxhighlight lang ="axe">√(Y)ʳ</langsyntaxhighlight>
 
To take the base-2 logarithm of an integer X:
<syntaxhighlight lang ="axe">ln(X)</langsyntaxhighlight>
 
To take 2 raised to an integer X: (Note that the base is not Euler's number)
<syntaxhighlight lang ="axe">e^(X)</langsyntaxhighlight>
 
To take the absolute value of a signed integer X:
<syntaxhighlight lang ="axe">abs(X)</langsyntaxhighlight>
 
=={{header|BASIC}}==
==={{header|ANSI BASIC}}===
{{works with|QuickBasic|4.5}}
{{trans|True BASIC|In fact, True BASIC implements ANSI BASIC, but adds some other features.}}
<lang qbasic>abs(x) 'absolute value
{{works with|Decimal BASIC}}
sqr(x) 'square root
<syntaxhighlight lang="basic">
exp(x) 'exponential
100 REM Real constants and functions
log(x) 'natural logarithm
110 DECLARE EXTERNAL FUNCTION Floor
x ^ y 'power
'floor,120 ceiling,PRINT "e, and pi = "; EXP(1) ! e not available</lang>
130 PRINT "PI = "; PI
140 LET X = 12.345
150 LET Y = 1.23
160 PRINT "sqrt = "; SQR(X), X ^ 0.5 ! square root- NB the unusual name
170 PRINT "ln = "; LOG(X) ! natural logarithm base e
180 PRINT "log2 = "; LOG2(X) ! base 2 logarithm
190 PRINT "log10 = "; LOG10(X) ! base 10 logarithm
200 PRINT "log = "; LOG(X) / LOG(Y) ! arbitrary base logarithm
210 PRINT "exp = "; EXP(X) ! exponential
220 PRINT "abs = "; ABS(-1) ! absolute value
230 PRINT "floor = "; Floor(X) ! floor easily implemented as functions
240 PRINT "ceil = "; CEIL(X) ! ceiling
250 PRINT "power = "; X ^ Y ! power
260 END
270 REM **
280 EXTERNAL FUNCTION Floor(X)
290 IF X > 0 THEN
300 LET Floor = INT(X)
310 ELSE
320 IF X <> INT(X) THEN LET Floor = INT(X) - 1 ELSE LET Floor = INT(X)
330 END IF
340 END FUNCTION
</syntaxhighlight>
 
==={{header|Applesoft BASIC}}===
{{works with|Chipmunk Basic}}
{{works with|GW-BASIC}}
{{works with|MSX Basic}}
{{works with|QBasic}}
<syntaxhighlight lang="qbasic">100 HOME : REM 10 CLS for GW-BASIC or MSX Basic or QBasic
110 PRINT "e = ";EXP(1) :REM e not available
120 PRINT "PI = ";4 * ATN(1) :REM pi not available
130 X = 12.345
140 Y = 1.23
150 PRINT "sqrt = ";SQR(X) :REM square root
160 PRINT "ln = ";LOG(X) :REM natural logarithm base e
170 PRINT "log10 = ";LOG(X)/LOG(10) :REM base 10 logarithm
180 PRINT "log = ";LOG(X)/LOG(Y) :REM arbitrary base logarithm
190 PRINT "exp = ";EXP(X) :REM exponential
200 PRINT "abs = ";ABS(-X) :REM absolute value
210 PRINT "floor = "; :REM floor not available
220 IF X > 0 THEN PRINT INT(X)
230 IF X <= 0 AND X <> INT(X) THEN PRINT INT(X)-1
240 IF X <= 0 AND X = INT(X) THEN PRINT INT(X)
250 PRINT "ceil = "; :REM ceil not available
260 IF X < 0 THEN PRINT INT(X)
270 IF X >= 0 THEN PRINT INT(X)+1
280 PRINT "power = ";X^Y :REM power
290 END</syntaxhighlight>
 
==={{header|BASIC256}}===
<syntaxhighlight lang="basic256">e = exp(1) # e not available
print "e = "; e
print "PI = "; PI
 
x = 12.345
y = 1.23
 
print "sqrt = "; sqr(x) # square root
print "ln = "; log(e) # natural logarithm base e
print "log10 = "; log10(e) # base 10 logarithm
print "log = "; log(x)/log(y) # arbitrary base logarithm
print "exp = "; exp(e) # exponential
print "abs = "; abs(-1) # absolute value
print "floor = "; floor(-e) # floor
print "ceil = "; ceil(-e) # ceiling
print "power = "; x ^ y # power</syntaxhighlight>
{{out}}
<pre>e = 2.71828182846
PI = 3.14159265359
sqrt = 3.51354521815
ln = 1.0
log10 = 0.4342944819
log = 12.1404787425
exp = 15.1542622415
abs = 1
floor = -3
ceil = -2
power = 22.0056421324</pre>
 
==={{header|BBC BASIC}}===
<syntaxhighlight lang="bbcbasic"> e = EXP(1)
Pi = PI
Sqr2 = SQR(2)
Ln2 = LN(2)
Log2 = LOG(2) : REM Base 10
Exp2 = EXP(2)
Abs2 = ABS(-2)
Floor = INT(1.234)
Ceil = FNceil(1.234)
Power = 1.23^4
END
DEF FNceil(n) = INT(n) - (INT(n) <> n)
</syntaxhighlight>
 
==={{header|Chipmunk Basic}}===
{{works with|Chipmunk Basic|3.6.4}}
<syntaxhighlight lang="qbasic">100 cls
110 print "e = ";exp(1)' e not available
120 print "PI = ";4*arctan(1)' pi not available
130 x = 12.345
140 y = 1.23
150 print "sqrt = ";sqr(x)' square root
160 print "ln = ";log(x)' natural logarithm base e
170 print "log10 = ";log(x)/log(10)' base 10 logarithm
180 print "log = ";log(x)/log(y)' arbitrary base logarithm
190 print "exp = ";exp(x)' exponential
200 print "abs = ";abs(-x)' absolute value
210 print "floor = ";floor(x)' floor
220 print "ceil = "; : ceil(x)' ceil easily implemented as function
230 print "power = ";x^y ' power
240 end
250 sub ceil(x)
260 if x < 0 then print int(x) else print int(x)+1
270 end sub</syntaxhighlight>
 
==={{header|FreeBASIC}}===
<syntaxhighlight lang="freebasic">' FB 1.05.0 Win64
 
#Include "crt/math.bi"
 
Print M_E '' constant "e" from C runtime library
Print M_PI '' constant "pi" from C runtime library
Print Sqr(2) '' square root function built into FB
Print Log(M_E) '' log to base "e" built into FB
Print log10(10) '' log to base 10 from C runtime library
Print Exp(1) '' exponential function built into FB
Print Abs(-1) '' absolute value function (integers or floats) built into FB
Print Int(-2.5) '' floor function built into FB
Print ceil(-2.5) '' ceiling function from C runtime library
Print 2.5 ^ 3.5 '' exponentiation operator built into FB
Sleep </syntaxhighlight>
{{out}}
<pre>
2.718281828459045
3.141592653589793
1.414213562373095
1
1
2.718281828459045
1
-3
-2
24.70529422006547
</pre>
 
==={{header|FutureBasic}}===
<syntaxhighlight lang="futurebasic">window 1
 
text ,,,,, 60// set tab width
 
print @"exp:", exp(1)
print @"pi:", pi
print @"sqr:", sqr(2)
print @"log:", log(2)
print @"log2:", log2(2)
print @"log10", log10(2)
print @"abs:", abs(-2)
print @"floor:", int(1.534)
print @"ceil:", val( using"###"; 1.534 )
print @"power:", 1.23 ^ 4
 
HandleEvents</syntaxhighlight>
Output:
<pre>
exp: 2.7182818285
pi: 3.1415926536
sqr: 1.4142135624
log: 0.6931471806
log2: 1
log10 0.3010299957
abs: 2
floor: 2
ceil: 2
power: 2.28886641
</pre>
 
==={{header|Gambas}}===
<syntaxhighlight lang="vbnet">Public Sub Main()
Dim e As Float = Exp(1)
Print "e = "; e ' e not available
Print "π = "; Pi ' pi
Dim x As Float = 12.345
Dim y As Float = 1.23
Print "sqrt = "; Sqr(x)' square root
Print "ln = "; Log(x)' natural logarithm base e
Print "log10 = "; Log(x) / Log(10) ' base 10 logarithm
Print "log = "; Log(x) / Log(y)' arbitrary base logarithm
Print "exp = "; Exp(x) ' exponential
Print "abs = "; Abs(-x)' absolute value
Print "floor = "; Floor(x)' floor
Print "ceil = "; Ceil(x)' ceil
Print "power = "; x ^ y ' power
End</syntaxhighlight>
 
==={{header|GW-BASIC}}===
{{works with|BASICA}}
{{works with|Chipmunk Basic}}
{{works with|MSX_BASIC}}
{{works with|QBasic}}
{{works with|PC-BASIC|any}}
<syntaxhighlight lang="qbasic">100 CLS
110 PRINT "e = "; EXP(1) ' e not available
120 PRINT "PI = "; 4 * ATN(1) ' pi not available
130 X = 12.345
140 Y = 1.23
150 PRINT "sqrt = "; SQR(X) ' square root
160 PRINT "ln = "; LOG(X) ' natural logarithm base e
170 PRINT "log10 = "; LOG(X)/LOG(10) ' base 10 logarithm
180 PRINT "log = "; LOG(X)/LOG(Y) ' arbitrary base logarithm
190 PRINT "exp = "; EXP(X) ' exponential
200 PRINT "abs = "; ABS(-X) ' absolute value
210 PRINT "floor = "; floor(X) ' floor
220 PRINT "ceil = "; : ' ceil not available
230 IF X < 0 THEN PRINT INT(X) ELSE PRINT INT(X)+1
240 PRINT "power = "; X ^Y ' power
250 END</syntaxhighlight>
 
==={{header|IS-BASIC}}===
<langsyntaxhighlight ISlang="is-BASICbasic">100 LET X=2:LET Y=5
110 PRINT EXP(1) ! value of e
120 PRINT PI ! value of Pi
Line 266 ⟶ 664:
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
290 PRINT INF ! The largest positive number the tinterpreter can handle. This number is 9.999999999*10^62</langsyntaxhighlight>
 
==={{header|Liberty BASIC}}===
{{works with|Just BASIC}}
Ceiling and floor easily implemented as functions.
<br>
sqr( is the LB function for square root.
<br>
e & pi not available- calculate as shown.
<syntaxhighlight lang="lb">
print exp(1) ' e not available
print 4 *atn(1) ' pi not available
 
x =12.345: y =1.23
 
print sqr(x), x^0.5 ' square root- NB the unusual name
print log(x) ' natural logarithm base e
print log(x) /2.303 ' base 10 logarithm
print log(x) / log(y) ' arbitrary base logarithm
print exp(x) ' exponential
print abs(x) ' absolute value
print floor(x) ' floor
print ceiling(x) ' ceiling
print x^y ' power
end
 
function floor(x)
if x >0 then
floor =int(x)
else
if x <>int(x) then floor =int(x) -1 else floor =int(x)
end if
end function
 
function ceiling(x)
if x <0 then
ceiling =int(x)
else
ceiling =int(x) +1
end if
end function
</syntaxhighlight>
 
==={{header|MSX Basic}}===
{{works with|MSX Basic|any}}
{{works with|Chipmunk Basic}}
<syntaxhighlight lang="qbasic">100 CLS
110 E = EXP(1)
120 PRINT "e = "; E ' e not available
130 PI = 4 * ATN(1)
140 PRINT "PI = "; PI ' pi not available
150 X = 12.345
160 Y = 1.23
170 PRINT "sqrt = "; SQR(X) ' square root
180 PRINT "ln = "; LOG(X) ' natural logarithm base e
190 PRINT "log10 = "; LOG(X)/LOG(10) ' base 10 logarithm
200 PRINT "log = "; LOG(X)/LOG(Y) ' arbitrary base logarithm
210 PRINT "exp = "; EXP(X) ' exponential
220 PRINT "abs = "; ABS(-X) ' absolute value
230 PRINT "floor = "; floor(X) ' floor
240 PRINT "ceil = "; : ' ceil not available
250 IF X < 0 THEN PRINT INT(X) ELSE PRINT INT(X)+1
260 PRINT "power = "; X ^Y ' power
270 END
</syntaxhighlight>
 
==={{header|PureBasic}}===
<syntaxhighlight lang="purebasic">Debug #E
Debug #PI
Debug Sqr(f)
Debug Log(f)
Debug Exp(f)
Debug Log10(f)
Debug Abs(f)
Debug Pow(f,f)</syntaxhighlight>
 
==={{header|QBasic}}===
{{works with|QBasic|1.1}}
{{works with|QuickBasic|4.5}}
<syntaxhighlight lang="qbasic">'ceiling and floor easily implemented as functions.
'e & pi not available- calculate as shown.
 
FUNCTION ceiling (x)
IF x < 0 THEN ceiling = INT(x) ELSE ceiling = INT(x) + 1
END FUNCTION
 
FUNCTION floor (x)
IF x > 0 THEN
floor = INT(x)
ELSE
IF x <> INT(x) THEN floor = INT(x) - 1 ELSE floor = INT(x)
END IF
END FUNCTION
 
PRINT "e = "; EXP(1) ' e not available
PRINT "PI = "; 4 * ATN(1) ' pi not available
x = 12.345: y = 1.23
PRINT "sqrt = "; SQR(x), x ^ .5 ' square root- NB the unusual name
PRINT "ln = "; LOG(x) ' natural logarithm base e
PRINT "log10 = "; LOG(x) / 2.303 ' base 10 logarithm
PRINT "log = "; LOG(x) / LOG(y) ' arbitrary base logarithm
PRINT "exp = "; EXP(x) ' exponential
PRINT "abs = "; ABS(-x) ' absolute value
PRINT "floor = "; floor(x) ' floor
PRINT "ceil = "; ceiling(x) ' ceiling
PRINT "power = "; x ^ y ' power
END</syntaxhighlight>
 
==={{header|Run BASIC}}===
{{works with|Just BASIC}}
<syntaxhighlight lang="runbasic">print "exp:";chr$(9); EXP(1)
print "PI:";chr$(9); 4 * ATN(1)
print "Sqr2:";chr$(9); SQR(2)
print "Log2:";chr$(9); LOG(2) : REM Base 10
print "Exp2:";chr$(9); EXP(2)
print "Abs2:";chr$(9); ABS(-2)
print "Floor:";chr$(9); INT(1.534)
print "ceil:";chr$(9); val(using("###",1.534))
print "Power:";chr$(9); 1.23^4</syntaxhighlight>
<pre>exp: 2.71828183
PI: 3.14285707
Sqr2: 1.41421356
Log2: 0.693147181
Exp2: 7.3890561
Abs2: 2
Floor: 1
ceil: 2
Power: 2.28886641</pre>
 
==={{header|Sinclair ZX81 BASIC}}===
Line 272 ⟶ 799:
 
Base of the natural logarithm:
<syntaxhighlight lang ="basic">EXP 1</langsyntaxhighlight>
 
<math>\pi</math>:
<syntaxhighlight lang ="basic">PI</langsyntaxhighlight>
 
Square root:
<syntaxhighlight lang ="basic">SQR X</langsyntaxhighlight>
 
Natural logarithm:
<syntaxhighlight lang ="basic">LN X</langsyntaxhighlight>
 
Exponential:
<syntaxhighlight lang ="basic">EXP X</langsyntaxhighlight>
 
Absolute value:
<syntaxhighlight lang ="basic">ABS X</langsyntaxhighlight>
 
Floor:
<syntaxhighlight lang ="basic">INT X</langsyntaxhighlight>
(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 297 ⟶ 824:
 
Power:
<syntaxhighlight lang ="basic">X**Y</langsyntaxhighlight>
NB. Both <math>x</math> and <math>y</math> can be real numbers.
 
==={{header|BBCTI-89 BASIC}}===
{|
<lang bbcbasic> e = EXP(1)
|-
Pi = PI
! Mathematical !! TI-89 !! Notes
Sqr2 = SQR(2)
|-
Ln2 = LN(2)
| <math>e </math> || <code style="font-family:'TI Uni'">ℯ </code> || (U+212F SCRIPT SMALL E)
Log2 = LOG(2) : REM Base 10
|-
Exp2 = EXP(2)
| <math>\pi </math> || <code style="font-family:'TI Uni'">π </code> || (U+03C0 GREEK SMALL LETTER PI)
Abs2 = ABS(-2)
|-
Floor = INT(1.234)
| <math>\sqrt{x} </math> || <code style="font-family:'TI Uni'">√(x) </code> || (U+221A SQUARE ROOT)
Ceil = FNceil(1.234)
|-
Power = 1.23^4
| <math>\log_e(x) </math> || <code style="font-family:'TI Uni'">ln(x) </code>
END
|-
| <math>\log_{10}(x) </math> || <code style="font-family:'TI Uni'">log(x) </code>
DEF FNceil(n) = INT(n) - (INT(n) <> n)
|-
</lang>
| <math>\log_b(x) </math> || <code style="font-family:'TI Uni'">log(b, x) </code> || The optional base argument comes ''first''
|-
| <math>\lfloor x\rfloor</math> || <code style="font-family:'TI Uni'">floor(x) </code>
|-
| <math>\lceil x\rceil </math> || <code style="font-family:'TI Uni'">ceiling(x)</code>
|-
| <math>x^y </math> || <code style="font-family:'TI Uni'">x^y </code>
|}
 
==={{header|True BASIC}}===
{{works with|Decimal BASIC}}
<syntaxhighlight lang="qbasic">FUNCTION floor(x)
IF x > 0 THEN
LET floor = INT(x)
ELSE
IF x <> INT(x) THEN LET floor = INT(x) - 1 ELSE LET floor = INT(x)
END IF
END FUNCTION
 
PRINT "e = "; exp(1) ! e not available
PRINT "PI = "; PI
 
LET x = 12.345
LET y = 1.23
 
PRINT "sqrt = "; SQR(x), x^0.5 ! square root- NB the unusual name
PRINT "ln = "; LOG(x) ! natural logarithm base e
PRINT "log2 = "; LOG2(x) ! base 2 logarithm
PRINT "log10 = "; LOG10(x) ! base 10 logarithm
PRINT "log = "; LOG(x)/LOG(y) ! arbitrary base logarithm
PRINT "exp = "; EXP(x) ! exponential
PRINT "abs = "; ABS(-1) ! absolute value
PRINT "floor = "; floor(x) ! floor easily implemented as functions
PRINT "ceil = "; CEIL(x) ! ceiling
PRINT "power = "; x ^ y ! power
END</syntaxhighlight>
 
==={{header|XBasic}}===
{{works with|Windows XBasic}}
<syntaxhighlight lang="qbasic">PROGRAM "realcaf"
VERSION "0.0000"
 
IMPORT "xma"
 
DECLARE FUNCTION Entry ()
DECLARE FUNCTION Ceil (x)
DECLARE FUNCTION Floor (x)
 
FUNCTION Entry ()
DOUBLE e, pi, x, y
e = EXP(1)
PRINT "e = "; e ' e not available
pi = 4 * ATAN(1)
PRINT "pi = "; pi ' pi not available
x = 12.345
y = 1.23
PRINT "sqrt = "; SQRT (x) ' square root
PRINT "ln = "; LOG (x) ' natural logarithm base e
PRINT "log10 = "; LOG10 (x) ' base 10 logarithm
PRINT "log = "; LOG (x) / LOG (y) ' arbitrary base logarithm
PRINT "exp = "; EXP (x) ' exponential
PRINT "abs = "; ABS (-x) ' absolute value
PRINT "Floor = "; Floor (x) ' Floor easily implemented as functions
PRINT "Ceil = "; Ceil (x) ' Ceil easily implemented as functions
PRINT "power = "; x ** y ' power
END FUNCTION
 
FUNCTION Ceil (x)
IF x < 0 THEN RETURN INT(x) ELSE RETURN INT(x) + 1
END FUNCTION
 
FUNCTION Floor (x)
IF x > 0 THEN
RETURN INT(x)
ELSE
IF x <> INT(x) THEN RETURN INT(x) - 1 ELSE RETURN INT(x)
END IF
END FUNCTION
END PROGRAM
</syntaxhighlight>
 
==={{header|Yabasic}}===
<syntaxhighlight lang="yabasic">print "e = ", euler
print "pi = ", pi
x = 12.345
y = 1.23
print "sqrt = ", sqrt(2) // square root
print "ln = ", log(euler) // natural logarithm base e
print "log = ", log(x, y) // arbitrary base y logarithm
print "exp = ", exp(euler) // exponential
print "abs = ", abs(-1) // absolute value
print "floor = ", floor(-euler) // floor
print "ceil = ", ceil(-euler) // ceiling
print "power = ", x ^ y, " ", x ** y // power
end</syntaxhighlight>
{{out}}
<pre>e = 2.71828
pi = 3.14159
sqrt = 1.41421
ln = 1
log = 12.1405
exp = 15.1543
abs = 1
floor = -3
ceil = -2
power = 22.0056 22.0056</pre>
 
=={{header|bc}}==
The language has square root and power, but power only works if the exponent is an integer.
 
<langsyntaxhighlight lang="bc">scale = 6
sqrt(2) /* 1.414213 square root */
4.3 ^ -2 /* .054083 power (integer exponent) */</langsyntaxhighlight>
 
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}}
<langsyntaxhighlight lang="bc">scale = 6
l(2) /* .693147 natural logarithm */
e(2) /* 7.389056 exponential */
Line 336 ⟶ 971:
 
e(l(2) * -3.4) /* .094734 2 to the power of -3.4 */
l(1024) / l(2) /* 10.000001 logarithm base 2 of 1024 */</langsyntaxhighlight>
 
The missing functions are absolute value, floor and ceiling. You can implement these functions, if you know what to do.
 
{{trans|AWK}}
<langsyntaxhighlight lang="bc">/* absolute value */
define v(x) {
if (x < 0) return (-x)
Line 375 ⟶ 1,010:
v(-3.4) /* 3.4 absolute value */
f(-3.4) /* -4 floor */
g(-3.4) /* -3 ceiling */</langsyntaxhighlight>
 
=={{header|blz}}==
The constant e
<syntaxhighlight lang ="blz">{e}</langsyntaxhighlight>
 
The constant pi
<syntaxhighlight lang ="blz">{pi}</langsyntaxhighlight>
 
Square root
<syntaxhighlight lang ="blz">x ** 0.5</langsyntaxhighlight>
 
Logarithm (base n)
<syntaxhighlight lang ="blz">x __ n</langsyntaxhighlight>
 
Exponential
<syntaxhighlight lang ="blz">{e} ** x</langsyntaxhighlight>
 
Absolute Value
<syntaxhighlight lang ="blz">abs(x)</langsyntaxhighlight>
 
Floor
<syntaxhighlight lang ="blz">floor(x)</langsyntaxhighlight>
 
Ceiling
<syntaxhighlight lang ="blz">ceil(x)</langsyntaxhighlight>
 
Power x to the y
<syntaxhighlight lang ="blz">x ** y</langsyntaxhighlight>
 
=={{header|BQN}}==
BQN has all the required constants, either within functions or available as single symbols:
<syntaxhighlight lang="bqn">
⋆1 # e and e^x
2.718281828459045
π # pi
3.141592653589793
√2 # square root
1.414213562373095
2⋆⁼10 # logarithm base n (inverse power)
3.321928094887363
|-1 # absolute value
1
⌊3.2 # floor
3
⌈3.2 # ceiling
4
3⋆4 # power
81</syntaxhighlight>
 
=={{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.
For example, differentiation <code>10^x</code> to <code>x</code>
<langsyntaxhighlight lang="bracmat">x \D (10^x) { \D is the differentiation operator }</langsyntaxhighlight>
has the result
<langsyntaxhighlight lang="bracmat">10^x*e\L10 { \L is the logarithm operator }</langsyntaxhighlight>
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 428 ⟶ 1,083:
=={{header|C}}==
Most of the following functions take a double.
<langsyntaxhighlight lang="c">#include <math.h>
 
M_E; /* e - not standard but offered by most implementations */
Line 439 ⟶ 1,094:
floor(x); /* floor */
ceil(x); /* ceiling */
pow(x,y); /* power */</langsyntaxhighlight>
 
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}}==
<langsyntaxhighlight lang="csharp">using System;
 
class Program {
Line 459 ⟶ 1,114:
Console.WriteLine(Math.Pow(2, 5)); // Exponentiation
}
}</langsyntaxhighlight>
 
=={{header|C++}}==
=== using Math macros ===
<langsyntaxhighlight lang="cpp">#include <iostream>
#include <cmath>
 
Line 490 ⟶ 1,145:
<< "\nceiling(4.5) = " << std::ceil(4.5)
<< "\npi^2 = " << std::pow(pi,2.0) << std::endl;
}</langsyntaxhighlight>
=== using Boost ===
{{libheader|Boost}}
<langsyntaxhighlight lang="cpp">#include <iostream>
#include <iomanip>
#include <cmath>
Line 511 ⟶ 1,166:
<< "\nfloor(4.5) = " << std::floor(4.5)
<< "\nceiling(4.5) = " << std::ceil(4.5) << std::endl;
}</langsyntaxhighlight>
{{out}}
<pre>e = 2.71828182845904509
Line 529 ⟶ 1,184:
=={{header|Clojure}}==
{{trans|Java}} which is directly available.
<langsyntaxhighlight lang="lisp">(Math/E); //e
(Math/PI); //pi
(Math/sqrt x); //square root--cube root also available (cbrt)
Line 537 ⟶ 1,192:
(Math/floor x); //floor
(Math/ceil x); //ceiling
(Math/pow x y); //power</langsyntaxhighlight>
 
Clojure does provide arbitrary precision versions as well:
 
<langsyntaxhighlight lang="lisp">(ns user (:require [clojure.contrib.math :as math]))
(math/sqrt x)
(math/abs x)
(math/floor x)
(math/ceil x)
(math/expt x y) </langsyntaxhighlight>
 
.. and as multimethods that can be defined for any type (e.g. complex numbers).
 
<langsyntaxhighlight lang="lisp">(ns user (:require [clojure.contrib.generic.math-functions :as generic]))
(generic/sqrt x)
(generic/log x)
Line 557 ⟶ 1,212:
(generic/floor x)
(generic/ceil x)
(generic/pow x y)</langsyntaxhighlight>
 
=={{header|COBOL}}==
Everything that follows can take any number (except for <code>SQRT</code> which expects a non-negative number).
The task constants and (intrinsic) functions:
<langsyntaxhighlight lang="cobol">E *> e
PI *> Pi
SQRT(n) *> Sqaure root
Line 574 ⟶ 1,229:
MOVE INTEGER(N) TO Result
*> There is no pow function, although the COMPUTE verb does have an exponention operator.
COMPUTE Result = N ** 2 </langsyntaxhighlight>
COBOL also has the following extra mathematical functions:
<langsyntaxhighlight lang="cobol">FACTORIAL(n) *> Factorial
EXP10(n) *> 10 to the nth power
*> Trigonometric functions, including inverse ones, named as would be expected.</langsyntaxhighlight>
 
=={{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.
<langsyntaxhighlight lang="lisp">
(exp 1) ; e (Euler's number)
pi ; pi constant
Line 593 ⟶ 1,248:
(ceiling x) ; ceiling: restricted to real, two valued (second value gives residue)
(expt x y) ; power
</syntaxhighlight>
</lang>
 
=={{header|Crystal}}==
<langsyntaxhighlight lang="ruby">x = 3.25
y = 4
 
Line 616 ⟶ 1,271:
puts exp(x) # puts Math.exp(x) -- exponential
puts E**x # puts Math::E**x -- same
</syntaxhighlight>
</lang>
 
{{0ut}}<pre>
Line 636 ⟶ 1,291:
 
=={{header|D}}==
<langsyntaxhighlight lang="d">import std.math ; // need to import this module
E // Euler's number
PI // pi constant
Line 647 ⟶ 1,302:
floor(x) // floor
ceil(x) // ceiling
pow(x,y) // power</langsyntaxhighlight>
 
=={{header|Delphi}}==
Delphi supports all basic Standard Pascal (ISO 7185) functions shown in [[#Pascal|§&nbsp;Pascal]].
Log, Floor, Ceil and Power functions defined in Math.pas.
Furthermore, the following is possible, too:
 
<langsyntaxhighlight Delphilang="delphi">Exp(1)Pi; // eπ (Euler's numberPi)
Pi; // π (Pi)
Sqrt(x); // square root
LogN(BASE, x) // log of x for a specified base
Log2(x) // log of x for base 2
Log10(x) // log of x for base 10
Ln(x); // natural logarithm (for good measure)
Exp(x); // exponential
Abs(x); // absolute value (a.k.a. "magnitude")
Floor(x); // floor
Ceil(x); // ceiling
Power(x, y); // power</langsyntaxhighlight>
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.
 
=={{header|DWScript}}==
See [[#Delphi|Delphi]].
 
See [[Real_constants_and_functions#Delphi|Delphi]].
 
=={{header|E}}==
<langsyntaxhighlight lang="e">? 1.0.exp()
# value: 2.7182818284590455
 
Line 695 ⟶ 1,345:
 
? 10 ** 6
# value: 1000000</langsyntaxhighlight>
 
=={{header|EasyLang}}==
<syntaxhighlight lang="easylang">
# e is not available
# pi
print pi
# square root
print sqrt 2
# logarithm base 10
print log10 1000
# exponential is not available
# absolute value
print abs -20
# floor
print floor 1.5
# ceiling is not available
# power
print pow 2 10
</syntaxhighlight>
 
=={{header|Elena}}==
ELENA 4.x :
<langsyntaxhighlight lang="elena">import system'math;
import extensions;
Line 714 ⟶ 1,383:
console.printLine(10.0r.ceil()); //Ceiling
console.printLine(2.power(5)); //Exponentiation
}</langsyntaxhighlight>
 
=={{header|Elixir}}==
<langsyntaxhighlight lang="elixir">defmodule Real_constants_and_functions do
def main do
IO.puts :math.exp(1) # e
Line 732 ⟶ 1,401:
end
 
Real_constants_and_functions.main</langsyntaxhighlight>
 
=={{header|Elm}}==
The following are all in the Basics module, which is imported by default:
<langsyntaxhighlight lang="elm">e -- e
pi -- pi
sqrt x -- square root
Line 744 ⟶ 1,413:
floor x -- floor
ceiling x -- ceiling
2 ^ 3 -- power</langsyntaxhighlight>
 
=={{header|Erlang}}==
<langsyntaxhighlight lang="erlang">% Implemented by Arjun Sunel
-module(math_constants).
-export([main/0]).
Line 782 ⟶ 1,451:
false -> T + 1
end.
</syntaxhighlight>
</lang>
{{out}}
<pre>2.718281828459045
Line 799 ⟶ 1,468:
 
=={{header|ERRE}}==
<langsyntaxhighlight ERRElang="erre">PROGRAM R_C_F
 
FUNCTION CEILING(X)
Line 826 ⟶ 1,495:
PRINT(CEILING(X)) ! ceiling
PRINT(X^Y) ! power
END PROGRAM</langsyntaxhighlight>
{{out}}
<pre> 2.718282
Line 843 ⟶ 1,512:
=={{header|F Sharp|F#}}==
{{trans|C#|C sharp}}
<langsyntaxhighlight lang="fsharp">open System
 
let main _ =
Line 857 ⟶ 1,526:
Console.WriteLine(Math.Pow(2.0, 5.0)); // Exponentiation
 
0</langsyntaxhighlight>
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">e ! e
pi ! π
sqrt ! square root
Line 870 ⟶ 1,539:
truncate ! remove the fractional part (i.e. round towards 0)
round ! round to next whole number
^ ! power</langsyntaxhighlight>
 
=={{header|Fantom}}==
Line 876 ⟶ 1,545:
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>
 
<langsyntaxhighlight lang="fantom">
Float.e
Float.pi
Line 888 ⟶ 1,557:
3.2f.round // nearest Int
3f.pow(2f) // power
</syntaxhighlight>
</lang>
 
Note, . binds more tightly than -, so use brackets around negative numbers:
Line 900 ⟶ 1,569:
 
=={{header|Forth}}==
<langsyntaxhighlight lang="forth">1e fexp fconstant e
0e facos 2e f* fconstant pi \ predefined in gforth
fsqrt ( f -- f )
Line 908 ⟶ 1,577:
floor ( f -- f ) \ round towards -inf
: ceil ( f -- f ) fnegate floor fnegate ; \ not standard, though fround is available
f** ( f e -- f^e )</langsyntaxhighlight>
 
=={{header|Fortran}}==
<langsyntaxhighlight lang="fortran"> e ! Not available. Can be calculated EXP(1.0)
pi ! Not available. Can be calculated 4.0*ATAN(1.0)
SQRT(x) ! square root
Line 920 ⟶ 1,589:
FLOOR(x) ! floor - Fortran 90 or later only
CEILING(x) ! ceiling - Fortran 90 or later only
x**y ! x raised to the y power</langsyntaxhighlight>
 
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 926 ⟶ 1,595:
The 4 need not be named as 4.0, or 4D0, as 4 the integer will be converted by the compiler to double precision, because it is to meet a known double precision value in simple multiplication and so will be promoted. Hopefully, at compile time.
 
=={{header|FreeBASICFree Pascal}}==
''See [[#Delphi|Delphi]]''
<lang freebasic>' FB 1.05.0 Win64
 
#Include "crt/math.bi"
 
Print M_E '' constant "e" from C runtime library
Print M_PI '' constant "pi" from C runtime library
Print Sqr(2) '' square root function built into FB
Print Log(M_E) '' log to base "e" built into FB
Print log10(10) '' log to base 10 from C runtime library
Print Exp(1) '' exponential function built into FB
Print Abs(-1) '' absolute value function (integers or floats) built into FB
Print Int(-2.5) '' floor function built into FB
Print ceil(-2.5) '' ceiling function from C runtime library
Print 2.5 ^ 3.5 '' exponentiation operator built into FB
Sleep </lang>
 
{{out}}
<pre>
2.718281828459045
3.141592653589793
1.414213562373095
1
1
2.718281828459045
1
-3
-2
24.70529422006547
</pre>
 
=={{header|Frink}}==
All of the following operations work for any numerical type, including rational numbers, complex numbers and intervals of real numbers.
<langsyntaxhighlight lang="frink">
e
pi, π // Unicode can also be written in ASCII programs as \u03C0
Line 970 ⟶ 1,611:
ceil[x] // Except for complex numbers where there's no good interpretation.
x^y
</syntaxhighlight>
</lang>
 
=={{header|FutureBasic}}==
<lang futurebasic>
include "ConsoleWindow"
 
// Set width of tab
def tab 8
 
print "exp:", exp(1)
print "pi:", pi
print "sqr:", sqr(2)
print "log:", log(2)
print "log2:", log2(2)
print "log10", log10(2)
print "abs:", abs(-2)
print "floor:", int(1.534)
print "ceil:", val( using"###"; 1.534 )
print "power:", 1.23 ^ 4
</lang>
Output:
<pre>
exp: 2.7182818285
pi: 3.1415926536
sqr: 1.4142135624
log: 0.6931471806
log2: 1
log10 0.3010299957
abs: 2
floor: 2
ceil: 2
power: 2.28886641
</pre>
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,058 ⟶ 1,667:
fmt.Println("x:", x)
fmt.Println("abs(x):", y.Abs(x))
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,088 ⟶ 1,697:
 
In addition to the java.lang.Math.abs() method, each numeric type has an abs() method, which can be invoked directly on the number:
<langsyntaxhighlight lang="groovy">println ((-22).abs())</langsyntaxhighlight>
{{out}}
<pre>22</pre>
Line 1,095 ⟶ 1,704:
 
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:
<syntaxhighlight lang ="groovy">println 22**3.5</langsyntaxhighlight>
{{out}}
<pre>49943.547010599876</pre>
Line 1,101 ⟶ 1,710:
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.
<langsyntaxhighlight lang="groovy">println ((-22)**3.5)</langsyntaxhighlight>
{{out}}
<pre>NaN</pre>
Line 1,110 ⟶ 1,719:
=={{header|Haskell}}==
The operations are defined for the various numeric typeclasses, as defined in their type signature.
<langsyntaxhighlight lang="haskell">exp 1 -- Euler number
pi -- pi
sqrt x -- square root
Line 1,120 ⟶ 1,729:
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)</langsyntaxhighlight>
 
=={{header|HicEst}}==
Except for x^y, this is identical to Fortran:
<langsyntaxhighlight HicEstlang="hicest">e ! Not available. Can be calculated EXP(1)
pi ! Not available. Can be calculated 4.0*ATAN(1.0)
x^0.5 ! square root
Line 1,134 ⟶ 1,743:
CEILING(x) ! ceiling
x**y ! x raised to the y power
x^y ! same as x**y</langsyntaxhighlight>
 
=={{header|Icon}} and {{header|Unicon}}==
<langsyntaxhighlight Iconlang="icon">link numbers # for floor and ceil
 
procedure main()
Line 1,151 ⟶ 1,760:
write("ceil(-2.2)=",ceil(-2.2))
write("power: 3^3=",3^3)
end</langsyntaxhighlight>
{{libheader|Icon Programming Library}}
[http://www.cs.arizona.edu/icon/library/src/procs/numbers.icn numbers provides floor and ceiling]
Line 1,169 ⟶ 1,778:
=={{header|J}}==
The examples below require arguments (x and y) to be numeric nouns.
<langsyntaxhighlight lang="j">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.
Line 1,182 ⟶ 1,791:
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</langsyntaxhighlight>
 
=={{header|Java}}==
All of these functions are in Java's <tt>Math</tt> class which, does not require any imports:
<langsyntaxhighlight lang="java">Math.E; //e
Math.PI; //pi
Math.sqrt(x); //square root--cube root also available (cbrt)
Line 1,194 ⟶ 1,803:
Math.floor(x); //floor
Math.ceil(x); //ceiling
Math.pow(x,y); //power</langsyntaxhighlight>
 
=={{header|JavaScript}}==
<langsyntaxhighlight lang="javascript">Math.E
Math.PI
Math.sqrt(x)
Line 1,205 ⟶ 1,814:
Math.floor(x)
Math.ceil(x)
Math.pow(x,y)</langsyntaxhighlight>
 
=={{header|jq}}==
Line 1,211 ⟶ 1,820:
In jq, "." refers to the output coming from the left in the pipeline.
 
In the following, comments appear after the "#":<langsyntaxhighlight lang="jq">
1 | exp # i.e. e
1 | atan * 4 # i.e. π
Line 1,217 ⟶ 1,826:
log # Naperian log
exp
iflength . < 0 then -. else . end # absolute value if the argument is numeric
floor
ceil # requires jq >= 1.5
# jq does not currently have a ceiling function
pow(x; y) # requires jq >= 1.5</syntaxhighlight>
# jq does not currently have a function to compute x^y</lang>
 
=={{header|Jsish}}==
<langsyntaxhighlight lang="javascript">/* real constants and functions, in JSI */
var x, y;
 
Line 1,265 ⟶ 1,874:
Math.pow(x,y) ==> 100000
=!EXPECTEND!=
*/</langsyntaxhighlight>
 
{{out}}
Line 1,290 ⟶ 1,899:
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">e
π, pi
sqrt(x)
Line 1,298 ⟶ 1,907:
floor(x)
ceil(x)
x^y</langsyntaxhighlight>
Note that Julia supports Unicode identifiers, and allows either <code>π</code> or <code>pi</code> for that constant.
 
Line 1,304 ⟶ 1,913:
 
=={{header|Kotlin}}==
All math constants are in <code>kotlin.math</code>.
<lang scala>// version 1.0.6
<syntaxhighlight lang="kotlin">import kotlin.math.*
 
fun main(args: Array<String>) {
println(Math.E) // e
println(Math.PI) // pi
println(Math.sqrt(2.0)) // square root
println(Math.logln(Math.E)) // log to base e
println(Math.log10(10.0)) // log to base 10
println(Math.explog(14.0, 2.0)) // log base 2 of // exponential4
println(Math.absexp(-1.0)) // absolute value
println(Math.floorabs(-2.51)) // floor
println(Math.ceilfloor(-2.5)) // ceiling
println(Math.powceil(-2.5, 3.5)) // power
println(2.5.pow(3.5)) // 2.5^3.5
}</lang>
}</syntaxhighlight>
 
{{out}}
Line 1,326 ⟶ 1,937:
1.0
1.0
2.0
2.718281828459045
1
Line 1,334 ⟶ 1,946:
 
=={{header|Lambdatalk}}==
<langsyntaxhighlight lang="scheme">
{E} -> 2.718281828459045
{PI} -> 3.141592653589793
Line 1,344 ⟶ 1,956:
{ceil -2.5} -> -2
{pow 2.5 3.5} -> 24.705294220065465
</syntaxhighlight>
</lang>
 
=={{header|Lasso}}==
<syntaxhighlight lang="lasso">//e
<lang Lasso>//e
define e => 2.7182818284590452
 
Line 1,362 ⟶ 1,974:
1.64->floor
1.64->ceil
1.64->pow(10.0)</langsyntaxhighlight>
 
=={{header|Liberty BASIC}}==
Ceiling and floor easily implemented as functions.
<br>
sqr( is the LB function for square root.
<br>
e & pi not available- calculate as shown.
<lang lb>
print exp( 1) ' e not available
print 4 *atn( 1) ' pi not available
 
x =12.345: y =1.23
 
print sqr( x), x^0.5 ' square root- NB the unusual name
print log( x) ' natural logarithm base e
print log( x) /2.303 ' base 10 logarithm
print log( x) /log( y) ' arbitrary base logarithm
print exp( x) ' exponential
print abs( x) ' absolute value
print floor( x) ' floor
print ceiling( x) ' ceiling
print x^y ' power
 
end
 
function floor( x)
if x >0 then
floor =int( x)
else
if x <>int( x) then floor =int( x) -1 else floor =int( x)
end if
end function
 
function ceiling( x)
if x <0 then
ceiling =int( x)
else
ceiling =int( x) +1
end if
end function
</lang>
 
=={{header|Lingo}}==
<langsyntaxhighlight lang="lingo">the floatPrecision = 8
 
-- e (base of the natural logarithm)
Line 1,452 ⟶ 2,023:
-- power
put power(2, 8)
-- 256.00000000</langsyntaxhighlight>
 
=={{header|LiveCode}}==
LC 7.1+, prior to this floor & ceil were not built-in.
<langsyntaxhighlight LiveCodelang="livecode">e‬: exp(1)
pi: pi
square root: sqrt(x)
Line 1,464 ⟶ 2,035:
floor: floor(x)
ceiling: ceil(x)
power: x^y</langsyntaxhighlight>
 
=={{header|Logo}}==
{{works with|UCB Logo}}
<langsyntaxhighlight lang="logo">make "e exp 1
make "pi 2*(RADARCTAN 0 1)
sqrt :x
Line 1,474 ⟶ 2,045:
exp :x
; there is no standard abs, floor, or ceiling; only INT and ROUND.
power :x :y</langsyntaxhighlight>
 
=={{header|Logtalk}}==
<langsyntaxhighlight lang="logtalk">
:- object(constants_and_functions).
 
Line 1,494 ⟶ 2,065:
 
:- end_object.
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,511 ⟶ 2,082:
 
=={{header|Lua}}==
<langsyntaxhighlight lang="lua">math.exp(1)
math.pi
math.sqrt(x)
Line 1,520 ⟶ 2,091:
math.floor(x)
math.ceil(x)
x^y</langsyntaxhighlight>
 
=={{header|M2000 Interpreter}}==
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Module Checkit {
Def exp(x)= 2.71828182845905^x
Line 1,545 ⟶ 2,116:
Print exptype$(exp(1%))="Double"
Print exptype$(Ln(1212%))="Double"
\\ power return type Double ^ and ** (are the same)
Print exptype$(2&^2&)="Long" ' for Version <=11 is "Double"
Print exptype$(2&**2&)="Long" ' for Version <=11 is "Double"
Print exptype$(2&*2&)="Long"
\\ 64bit Long Long (from Version 12)
Print exptype$(2&&^2&&)="Long Long"
Print exptype$(2&&**2&&)="Long Long"
Print exptype$(2&&*2&)="Long Long"
Print 2**2=4, 2^2=4, 2^2^2=16, 2**2**2=16
\\ floor() and Int() is the same
Line 1,562 ⟶ 2,137:
}
Checkit
</syntaxhighlight>
</lang>
 
=={{header|Maple}}==
<langsyntaxhighlight Maplelang="maple">> abs(ceil(floor(ln(exp(1)^sqrt(exp(Pi*I)+1)))));
0</langsyntaxhighlight>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">E
<lang Mathematica>E
Pi
Sqrt[x]
Line 1,578 ⟶ 2,153:
Floor[x]
Ceiling[x]
Power[x, y]</langsyntaxhighlight>
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:
<langsyntaxhighlight Mathematicalang="mathematica">Log[1.23] => 0.207014
Log[10] => Log[10]
Log[10,100] => 2
Line 1,596 ⟶ 2,171:
Sqrt[-4] => 2I
Exp[2] => E^2
Exp[Log[4]] => 4</langsyntaxhighlight>
 
=={{header|MATLAB}} / {{header|Octave}}==
<langsyntaxhighlight MATLABlang="matlab">exp(1) % e
pi % pi
sqrt(x) % square root
Line 1,609 ⟶ 2,184:
floor(x) % floor
ceil(x) % ceiling
x^y % power</langsyntaxhighlight>
 
=={{header|MAXScript}}==
<langsyntaxhighlight lang="maxscript">e -- Euler's number
pi -- pi
log x -- natural logarithm
Line 1,620 ⟶ 2,195:
floor x -- floor
ceil x -- ceiling
pow x y -- power</langsyntaxhighlight>
 
=={{header|Mercury}}==
<syntaxhighlight lang="text">
math.pi % Pi.
math.e % Euler's number.
Line 1,635 ⟶ 2,210:
math.floor(X) % Floor of X.
math.ceiling(X) % Ceiling of X.
math.pow(X, Y) % X raised to the power of Y.</langsyntaxhighlight>
 
=={{header|Metafont}}==
<langsyntaxhighlight 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
Line 1,652 ⟶ 2,227:
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</langsyntaxhighlight>
 
=={{header|min}}==
{{works with|min|0.19.3}}
<langsyntaxhighlight lang="min">e ; e
pi ; π
sqrt ; square root
Line 1,667 ⟶ 2,242:
trunc ; remove the fractional part (i.e. round towards 0)
round ; round number to nth decimal place
pow ; power</langsyntaxhighlight>
 
=={{header|МК-61/52}}==
<syntaxhighlight lang="text">1 e^x С/П
 
пи С/П
Line 1,688 ⟶ 2,263:
x<0 14 ИП1 С/П ИП1 1 + С/П
 
x^y С/П</langsyntaxhighlight>
 
=={{header|Modula-3}}==
Line 1,694 ⟶ 2,269:
 
Note that all of these procedures (except the built ins) take <tt>LONGREAL</tt>s as their argument, and return <tt>LONGREAL</tt>s.
<langsyntaxhighlight lang="modula3">Math.E;
Math.Pi;
Math.sqrt(x);
Line 1,702 ⟶ 2,277:
FLOOR(x); (* Built in function. *)
CEILING(x); (* Built in function. *)
Math.pow(x, y);</langsyntaxhighlight>
 
=={{header|Neko}}==
<syntaxhighlight lang="actionscript">/**
<lang ActionScript>/**
Real constants and functions, in Neko
Tectonics:
Line 1,732 ⟶ 2,307:
$print("Floor(-2.2): ", math_floor(-2.2), "\n")
$print("Ceil(-2.2) : ", math_ceil(-2.2), "\n")
$print("Pow(2, 8) : ", math_pow(2, 8), "\n")</langsyntaxhighlight>
 
{{out}}
Line 1,749 ⟶ 2,324:
=={{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.
<langsyntaxhighlight NetRexxlang="netrexx">/* NetRexx */
options replace format comments java crossref symbols nobinary utf8
 
Line 1,798 ⟶ 2,373:
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)
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,842 ⟶ 2,417:
 
=={{header|Nim}}==
<langsyntaxhighlight lang="nim">import math
 
var x, y = 12.5
 
echo E
echo PiiPI
echo sqrt(x)
echo ln(x)
Line 1,855 ⟶ 2,430:
echo floor(x)
echo ceil(x)
echo pow(x, y)</langsyntaxhighlight>
 
=={{header|Objeck}}==
<langsyntaxhighlight lang="objeck">Float->Pi();
Float->E();
4.0->SquareRoot();
Line 1,866 ⟶ 2,441:
3.99->Floor();
3.99->Ceiling();
4.5->Ceiling(2.0);</langsyntaxhighlight>
 
=={{header|OCaml}}==
Unless otherwise noted, the following functions are for floats only:
<langsyntaxhighlight lang="ocaml">Float.pi (* pi *)
sqrt x (* square root *)
log x (* natural logarithm--log base 10 also available (log10) *)
Line 1,879 ⟶ 2,454:
ceil x (* ceiling *)
x ** y (* power *)
-. x (* negation for floats *)</langsyntaxhighlight>
 
=={{header|Octave}}==
<langsyntaxhighlight lang="octave">e % e
pi % pi
sqrt(pi) % square root
Line 1,890 ⟶ 2,465:
floor(pi) % floor
ceil(pi) % ceiling
e**pi % power</langsyntaxhighlight>
 
=={{header|Oforth}}==
 
<langsyntaxhighlight Oforthlang="oforth">import: math
 
: testReal
Line 1,917 ⟶ 2,492:
-2.4 ceil println
-3.9 ceil println
-5.5 ceil println ;</langsyntaxhighlight>
 
=={{header|ooRexx}}==
{{trans|NetRexx}}
{{uses from|OoRexx|RxMath}}
<langsyntaxhighlight ooRexxlang="oorexx">/* Rexx */
 
-- MathLoadFuncs & MathDropFuncs are no longer needed and are effectively NOPs
Line 1,979 ⟶ 2,554:
return arg(1)~trunc() + (arg(1) > 0) * (arg(1) \= arg(1)~trunc())
 
::requires 'RxMath' library</langsyntaxhighlight>
{{out}}
<pre>
Line 2,022 ⟶ 2,597:
 
=={{header|Oz}}==
<langsyntaxhighlight lang="oz">{ForAll
[
{Exp 1.} %% 2.7183 Euler's number: not predefined
Line 2,033 ⟶ 2,608:
{Pow 2 3} %% 8 power; both arguments must be of the same type
]
Show}</langsyntaxhighlight>
 
=={{header|PARI/GP}}==
<langsyntaxhighlight lang="parigp">[exp(1), Pi, sqrt(2), log(2), abs(2), floor(2), ceil(2), 2^3]</langsyntaxhighlight>
 
=={{header|Pascal}}==
''See also [[#Delphi|Delphi]] and [[#Free Pascal|Free Pascal]]''<br/>
See [[Real_constants_and_functions#Delphi | Delphi]]
Following functions are defined by ISO standard 7185, Standard “Unextended” Pascal, and supported by any processor:
<syntaxhighlight lang="pascal"> { Euler’s constant }
exp(1)
{ principal square root of `x` }
sqrt(x)
{ natural logarithm }
ln(x)
{ exponential }
exp(x)
{ absolute value }
abs(x)</syntaxhighlight>
{{works with|Extended Pascal}}
Additionally, in Extended Pascal (ISO standard 10206) following operators and expressions can be used:
<syntaxhighlight lang="pascal"> { Pi }
2 * arg(cmplx(0.0, maxReal))
{ power, yields same data type as `base`, `exponent` has to be an `integer` }
base pow exponent
{ `real` power, `exponent` may be an `integer` or `real` value, yet `base` and }
{ `exponent` are automatically promoted to an approximate `real` value, result }
{ is `complex` if `base` is `complex`, otherwise a `real` value }
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>Abs</tt> returns an <tt>integer</tt> value if supplied with an <tt>integer</tt>, otherwise a <tt>real</tt> value.
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">use POSIX; # for floor() and ceil()
 
exp(1); # e
Line 2,058 ⟶ 2,656:
 
use Math::Complex;
pi; # alternate way to get pi</langsyntaxhighlight>
 
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>?E -- Euler number
<span style="color: #0000FF;">?</span><span style="color: #004600;">E</span> <span style="color: #000080;font-style:italic;">-- Euler number</span>
?PI -- pi
<span style="color: #0000FF;">?</span><span style="color: #004600;">PI</span> <span style="color: #000080;font-style:italic;">-- pi</span>
?log(E) -- natural logarithm
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">log</span><span style="color: #0000FF;">(</span><span style="color: #004600;">E</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- natural logarithm</span>
?log10(10) -- base 10 logarithm
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">log10</span><span style="color: #0000FF;">(</span><span style="color: #000000;">10</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- base 10 logarithm</span>
?exp(log(5)) -- exponential
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">exp</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">log</span><span style="color: #0000FF;">(</span><span style="color: #000000;">5</span><span style="color: #0000FF;">))</span> <span style="color: #000080;font-style:italic;">-- exponential</span>
?sqrt(5) -- square root
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">sqrt</span><span style="color: #0000FF;">(</span><span style="color: #000000;">5</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- square root</span>
?abs(-1.2) -- absolute value
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">abs</span><span style="color: #0000FF;">(-</span><span style="color: #000000;">1.2</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- absolute value</span>
?floor(-1.2) -- floor, -2
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(-</span><span style="color: #000000;">1.2</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- floor, -2</span>
?ceil(-1.2) -- ceiling, -1
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">ceil</span><span style="color: #0000FF;">(-</span><span style="color: #000000;">1.2</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- ceiling, -1</span>
?round(-1.8) -- rounded, -2
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">round</span><span style="color: #0000FF;">(-</span><span style="color: #000000;">1.8</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- rounded, -2</span>
?trunc(-1.8) -- truncate, -1
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">trunc</span><span style="color: #0000FF;">(-</span><span style="color: #000000;">1.8</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- truncate, -1</span>
?power(E,log(5)) -- displays 5.0
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #004600;">E</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">log</span><span style="color: #0000FF;">(</span><span style="color: #000000;">5</span><span style="color: #0000FF;">))</span> <span style="color: #000080;font-style:italic;">-- displays 5.0</span>
?power(10,log10(5)) -- displays 5.0
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">log10</span><span style="color: #0000FF;">(</span><span style="color: #000000;">5</span><span style="color: #0000FF;">))</span> <span style="color: #000080;font-style:italic;">-- displays 5.0</span>
?INVLN10 -- displays 0.434..
<span style="color: #0000FF;">?</span><span style="color: #004600;">INVLN10</span> <span style="color: #000080;font-style:italic;">-- displays 0.434..</span>
?exp(1/INVLN10) -- displays 10.0</lang>
<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>
<!--</syntaxhighlight>-->
 
=={{header|PHP}}==
<langsyntaxhighlight lang="php">M_E; //e
M_PI; //pi
sqrt(x); //square root
Line 2,086 ⟶ 2,686:
floor(x); //floor
ceil(x); //ceiling
pow(x,y); //power</langsyntaxhighlight>
 
=={{header|Picat}}==
<syntaxhighlight lang="picat">main =>
println(math.e),
println(math.pi),
nl,
println(sqrt(2)),
nl,
println(log(10)), % base e
println(log(math.pi,10)), % some base, here pi
println(log2(10)), % base 2
println(exp(2.302585092994046)),
nl,
println(abs(- math.e)),
nl,
println(floor(sqrt(101))),
println(ceiling(sqrt(101))),
nl,
println(math.pi**math.e), % power
println(pow(math.pi,math.e)), % power
nl.</syntaxhighlight>
 
{{out}}
<pre>2.718281828459045
3.141592653589793
 
1.414213562373095
 
2.302585092994046
2.011465867588061
3.321928094887362
10.000000000000002
 
2.718281828459045
 
10
11
 
22.459157718361041
22.459157718361041</pre>
 
 
=={{header|PicoLisp}}==
Line 2,094 ⟶ 2,738:
(about 16 digits). The default precision is six, and can be changed with
'[http://software-lab.de/doc/refS.html#scl scl]':
<langsyntaxhighlight PicoLisplang="picolisp">(scl 12) # 12 places after decimal point
(load "@lib/math.l")
 
Line 2,109 ⟶ 2,753:
(prinl (abs -123))
 
(prinl (format (pow 3.0 4.0) *Scl)) # power</langsyntaxhighlight>
{{out}}
<pre>2.718281828459
Line 2,122 ⟶ 2,766:
 
=={{header|PL/I}}==
<langsyntaxhighlight 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) */
y = sqrt(x);
Line 2,137 ⟶ 2,781:
y = erfc(x); /* the error function complemented. */
y = gamma (x);
y = loggamma (x);</langsyntaxhighlight>
 
=={{header|Plain English}}==
Plain English comes with these constants and functions in the noodle (Euler's number, logarithms, exponential, ceiling, and floor are not available):
<syntaxhighlight lang="text">
The pizza pie is a fraction equal to 355/113. \ equivalent to pi
 
\ square root
To find a square root of a number: \ rounds down
Privatize the number.
De-sign the number.
If the number is 0, put 0 into the square root; exit.
If the number is 1, put 1 into the square root; exit.
Put 1 into a square number.
Put 3 into a delta number.
Loop.
If the square is greater than the number, break.
Add the delta to the square.
Add 2 to the delta.
Repeat.
Put the delta divided by 2 minus 1 into the square root.
 
A square root is a number.
 
\ absolute value
To put a fraction's absolute value into another fraction:
Put the fraction into the other fraction.
De-sign the other fraction.
 
To put a number's absolute value into another number:
Put the number into the other number.
De-sign the other number.
 
To put a pair's absolute value into another pair:
Put the pair into the other pair.
De-sign the other pair.
 
\ power
To raise a number to another number:
If the other number is 0, put 1 into the number; exit.
If the other number is less than 0, put 0 into the number; exit. \ should be 1/the raised result, but always comes out 0 with numbers
Put 1 into a result number.
Loop.
If a counter is past the other number, break.
Multiply the result by the number.
Repeat.
Put the result into the number.
</syntaxhighlight>
 
=={{header|Pop11}}==
<langsyntaxhighlight 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</langsyntaxhighlight>
 
The number e is not provided directly, one has to compute 'exp(1)'
Line 2,154 ⟶ 2,845:
=={{header|PowerShell}}==
Since PowerShell has access to .NET all this can be achieved using the .NET Base Class Library:
<langsyntaxhighlight lang="powershell">Write-Host ([Math]::E)
Write-Host ([Math]::Pi)
Write-Host ([Math]::Sqrt(2))
Line 2,162 ⟶ 2,853:
Write-Host ([Math]::Floor(3.14))
Write-Host ([Math]::Ceiling(3.14))
Write-Host ([Math]::Pow(2, 3))</langsyntaxhighlight>
 
=={{header|PureBasic}}==
<lang PureBasic>Debug #E
Debug #PI
Debug Sqr(f)
Debug Log(f)
Debug Exp(f)
Debug Log10(f)
Debug Abs(f)
Debug Pow(f,f)</lang>
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">import math
 
math.e # e
Line 2,190 ⟶ 2,871:
 
# The math module constants and functions can, of course, be imported directly by:
# from math import e, pi, sqrt, log, log10, exp, floor, ceil</langsyntaxhighlight>
 
=={{header|R}}==
<langsyntaxhighlight Rlang="r">exp(1) # e
pi # pi
sqrt(x) # square root
Line 2,203 ⟶ 2,884:
floor(x) # floor
ceiling(x) # ceiling
x^y # power</langsyntaxhighlight>
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">(exp 1) ; e
pi ; pi
(sqrt x) ; square root
Line 2,214 ⟶ 2,895:
(floor x) ; floor
(ceiling x) ; ceiling
(expt x y) ; power</langsyntaxhighlight>
 
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku" perl6line>say e; # e
say π; # or pi # pi
say τ; # or tau # tau
Line 2,255 ⟶ 2,936:
say pi.ceiling; # Ceiling
 
say e ** π\i + 1 ≅ 0; # :-)</langsyntaxhighlight>
 
=={{header|REXX}}==
Line 2,262 ⟶ 2,943:
REXX doesn't have any built-in (math) constants.
===abs===
<langsyntaxhighlight lang="rexx">a=abs(y) /*takes the absolute value of y.*/</langsyntaxhighlight>
===exponentiation (**)===
<langsyntaxhighlight lang="rexx">r=x**y /*REXX only supports integer powers.*/
/*Y may be negative, zero, positive.*/
/*X may be any real number. */</langsyntaxhighlight>
 
===ceiling===
A ceiling function for REXX:
<langsyntaxhighlight lang="rexx">
ceiling: procedure; parse arg x; t=trunc(x); return t+(x>0)*(x\=t)
</syntaxhighlight>
</lang>
 
===floor===
A floor function for REXX:
<langsyntaxhighlight lang="rexx">
floor: procedure; parse arg x; t=trunc(x); return t-(x<0)-(x\=t)
</syntaxhighlight>
</lang>
 
===sqrt (optimized)===
A [principal] square root (SQRT) function for REXX &nbsp; (with arbitrary precision):
<langsyntaxhighlight lang="rexx">/*──────────────────────────────────SQRT subroutine───────────────────────────*/
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.*/
Line 2,308 ⟶ 2,989:
/* [↓] normalize √ ──► original digits*/
numeric digits d /* [↓] make answer complex if X < 0. */
return (g/1)i /*normalize, and add possible I suffix.*/</langsyntaxhighlight>
<langsyntaxhighlight lang="rexx"> ╔════════════════════════════════════════════════════════════════════╗
╔═╝ __ ╚═╗
║ √ ║
Line 2,334 ⟶ 3,015:
║ __ ║
╚═╗ √ ╔═╝
╚════════════════════════════════════════════════════════════════════╝</langsyntaxhighlight>
 
===sqrt (simple)===
<langsyntaxhighlight lang="rexx">/*──────────────────────────────────SQRT subroutine─────────────────────*/
sqrt: procedure; arg x /*a simplistic SQRT subroutine.*/
if x=0 then return 0 /*handle special case of zero. */
Line 2,350 ⟶ 3,031:
end /*forever*/ /* [↑] ···'til we run out of digs*/
numeric digits d /*restore the original precision.*/
return g/1 /*normalize to old precision (d).*/</langsyntaxhighlight>
 
===other===
Other mathematical-type functions supported are:
<langsyntaxhighlight lang="rexx">numeric digits ddd /*sets the current precision to DDD */
numeric fuzz fff /*arithmetic comparisons with FFF fuzzy*/
numeric form kkk /*exponential: scientific | engineering*/
Line 2,380 ⟶ 3,061:
bb=x2b(hhh) /*converts hexadecimal to binary (bits)*/
cc=x2c(hhh) /*converts hexadecimal to character. */
dd=x2d(hhh) /*converts hexadecimal to decimal. */</langsyntaxhighlight>
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
See "Mathematical Functions" + nl
See "Sin(0) = " + sin(0) + nl
Line 2,429 ⟶ 3,110:
 
see "sqrt(16) = " + sqrt(16) + nl
</syntaxhighlight>
</lang>
 
=={{header|RLaB}}==
Line 2,436 ⟶ 3,117:
 
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]].
<langsyntaxhighlight RLaBlang="rlab">>> const
e euler ln10 ln2 lnpi
log10e log2e pi pihalf piquarter
rpi sqrt2 sqrt2r sqrt3 sqrtpi
tworpi</langsyntaxhighlight>
 
=== Physical Constants ===
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.
<langsyntaxhighlight RLaBlang="rlab">>> mks
F G J L N
Na R0 Ry Tsp V0
Line 2,468 ⟶ 3,149:
therm tntton ton torr toz
tsp uam ukgal ukton uston
week yd</langsyntaxhighlight>
 
=== Elementary Functions ===
<langsyntaxhighlight RLaBlang="rlab">>> x = rand()
>> sqrt(x)
2.23606798
Line 2,487 ⟶ 3,168:
5
>> x .^ 2
25</langsyntaxhighlight>
 
=={{header|RPL}}==
{| class="wikitable"
! RPL code
! Comment
|-
|
'''e'''
'''π'''
x '''√'''
x '''LN'''
x '''LNP'''
x '''LOG'''
x '''EXP'''
x '''ABS'''
x '''FLOOR'''
x '''CEIL'''
x y '''^'''
|
e
π
square root
logarithm (base e)
equivalent to x 1 + LN - e.g. Log(x+1) - but more accurate for small values of x
logarithm (base 10)
exponential
absolute value
floor
ceiling
power
|}
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">x.abs #absolute value
x.magnitude #absolute value
x.floor #floor
Line 2,503 ⟶ 3,215:
log10(x) #base 10 logarithm
exp(x) #exponential
</syntaxhighlight>
</lang>
 
=={{header|Run BASIC}}==
<lang runbasic>print "exp:";chr$(9); EXP(1)
print "PI:";chr$(9); 22/7
print "Sqr2:";chr$(9); SQR(2)
print "Log2:";chr$(9); LOG(2) : REM Base 10
print "Exp2:";chr$(9); EXP(2)
print "Abs2:";chr$(9); ABS(-2)
print "Floor:";chr$(9); INT(1.534)
print "ceil:";chr$(9); val(using("###",1.534))
print "Power:";chr$(9); 1.23^4</lang>
<pre>exp: 2.71828183
PI: 3.14285707
Sqr2: 1.41421356
Log2: 0.693147181
Exp2: 7.3890561
Abs2: 2
Floor: 1
ceil: 2
Power: 2.28886641</pre>
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">use std::f64::consts::*;
 
fn main() {
Line 2,549 ⟶ 3,241:
 
assert_eq!(x, 4.0);
}</langsyntaxhighlight>
 
=={{header|Scala}}==
<langsyntaxhighlight lang="scala">object RealConstantsFunctions extends App{
println(math.E) // e
println(math.Pi) // pi
Line 2,563 ⟶ 3,255:
println(math.ceil(-2.5)) // ceiling
println(math.pow(2.5, 3.5)) // power
}</langsyntaxhighlight>
 
=={{header|Scheme}}==
<langsyntaxhighlight lang="scheme">(sqrt x) ;square root
(log x) ;natural logarithm
(exp x) ;exponential
Line 2,572 ⟶ 3,264:
(floor x) ;floor
(ceiling x) ;ceiling
(expt x y) ;power</langsyntaxhighlight>
 
=={{header|Seed7}}==
Line 2,602 ⟶ 3,294:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">Num.e # e
Num.pi # pi
x.sqrt # square root
Line 2,611 ⟶ 3,303:
x.floor # floor
x.ceil # ceiling
x**y # exponentiation</langsyntaxhighlight>
 
=={{header|Slate}}==
<langsyntaxhighlight lang="slate">numerics E.
numerics Pi.
n sqrt.
Line 2,624 ⟶ 3,316:
n floor.
n ceiling.
n raisedTo: anotherNumber</langsyntaxhighlight>
 
=={{header|Smalltalk}}==
<langsyntaxhighlight lang="smalltalk">Float e.
Float pi.
aNumber sqrt.
Line 2,636 ⟶ 3,328:
aNumber floor.
aNumber ceiling.
aNumber raisedTo: anotherNumber</langsyntaxhighlight>
 
=={{header|Sparkling}}==
<langsyntaxhighlight lang="sparkling">// e:
print(M_E);
 
Line 2,669 ⟶ 3,361:
 
// power
let eighty_one = pow(3, 4);</langsyntaxhighlight>
 
=={{header|Standard ML}}==
<langsyntaxhighlight lang="sml">Math.e; (* e *)
Math.pi; (* pi *)
Math.sqrt x; (* square root *)
Line 2,681 ⟶ 3,373:
ceil x; (* ceiling *)
Math.pow (x, y); (* power *)
~ x; (* negation *)</langsyntaxhighlight>
 
=={{header|Stata}}==
<langsyntaxhighlight lang="stata">scalar x=2
scalar y=3
di exp(1)
Line 2,696 ⟶ 3,388:
di floor(x)
di ceil(x)
di x^y</langsyntaxhighlight>
 
=={{header|Swift}}==
<langsyntaxhighlight lang="swift">import Darwin
 
M_E // e
Line 2,709 ⟶ 3,401:
floor(x) // floor
ceil(x) // ceiling
pow(x,y) // power</langsyntaxhighlight>
 
=={{header|Tcl}}==
<langsyntaxhighlight lang="tcl">expr {exp(1)} ;# e
expr {4 * atan(1)} ;# pi -- also, simpler: expr acos(-1)
expr {sqrt($x)} ;# square root
Line 2,720 ⟶ 3,412:
expr {floor($x)} ;# floor
expr {ceil($x)} ;# ceiling
expr {$x**$y} ;# power, also pow($x,$y)</langsyntaxhighlight>
The constants <math>e</math> and <math>\pi</math> are also available with high precision in a support library.
{{tcllib|math::constants}}
<langsyntaxhighlight lang="tcl">package require math::constants
math::constants::constants e pi
puts "e = $e, pi = $pi"</langsyntaxhighlight>
 
=={{header|TI-89 BASIC}}==
{|
|-
! Mathematical !! TI-89 !! Notes
|-
| <math>e </math> || <code style="font-family:'TI Uni'">ℯ </code> || (U+212F SCRIPT SMALL E)
|-
| <math>\pi </math> || <code style="font-family:'TI Uni'">π </code> || (U+03C0 GREEK SMALL LETTER PI)
|-
| <math>\sqrt{x} </math> || <code style="font-family:'TI Uni'">√(x) </code> || (U+221A SQUARE ROOT)
|-
| <math>\log_e(x) </math> || <code style="font-family:'TI Uni'">ln(x) </code>
|-
| <math>\log_{10}(x) </math> || <code style="font-family:'TI Uni'">log(x) </code>
|-
| <math>\log_b(x) </math> || <code style="font-family:'TI Uni'">log(b, x) </code> || The optional base argument comes ''first''
|-
| <math>\lfloor x\rfloor</math> || <code style="font-family:'TI Uni'">floor(x) </code>
|-
| <math>\lceil x\rceil </math> || <code style="font-family:'TI Uni'">ceiling(x)</code>
|-
| <math>x^y </math> || <code style="font-family:'TI Uni'">x^y </code>
|}
 
=={{header|UNIX Shell}}==
{{works with|ksh93}}
ksh93 exposes math functions from the C math library
<langsyntaxhighlight lang="bash">echo $(( exp(1) )) # e
echo $(( acos(-1) )) # PI
x=5
Line 2,768 ⟶ 3,436:
echo $(( ceil(x) )) # ceiling
x=10 y=3
echo $(( pow(x,y) )) # power</langsyntaxhighlight>
 
{{out}}
Line 2,782 ⟶ 3,450:
-5
1000</pre>
 
=={{header|V (Vlang)}}==
Many more in math module.
<syntaxhighlight lang="vlang">
import math
 
fn main() {
x := -1.2345
println("e = ${math.e}")
println("pi = ${math.pi}")
println("sqrt(4) = ${math.sqrt(4)}")
println("log(e) = ${math.log(math.e)}")
println("exp(x) = ${math.exp(x)}")
println("abs(x) = ${math.abs(x)}")
println("floor(x) = ${math.floor(x)}")
println("ceil(x) = ${math.ceil(x)}")
println("pow(-x, x) = ${math.pow(-x, x)}")
}
</syntaxhighlight>
 
=={{header|Wren}}==
<syntaxhighlight lang="wren">var e = 1.exp
<lang ecmascript>var e = 2.718281828459045 // not in standard library
 
System.print("e = %(e)")
Line 2,790 ⟶ 3,477:
System.print("sqrt(2) = %(2.sqrt)")
System.print("ln(3) = %(3.log)") // log base e
System.print("exp(2) = %(e2.pow(2)exp)") // exp not in standard library
System.print("abs(-e) = %((-e).abs)")
System.print("floor(e) = %(e.floor)")
System.print("ceil(e) = %(e.ceil)")
System.print("pow(e, 2) = %(e.pow(2))")</langsyntaxhighlight>
 
{{out}}
Line 2,802 ⟶ 3,489:
sqrt(2) = 1.4142135623731
ln(3) = 1.0986122886681
exp(2) = 7.38905609893063890560989307
abs(-e) = 2.718281828459
floor(e) = 2
Line 2,810 ⟶ 3,497:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">include c:\cxpl\codes; \intrinsic 'code' declarations
 
func real Power(X, Y); \X raised to the Y power
Line 2,835 ⟶ 3,522:
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
] \ can be used for both reals & ints</langsyntaxhighlight>
 
{{out}}
Line 2,855 ⟶ 3,542:
4.0000000000000000
</pre>
 
=={{header|Zig}}==
<syntaxhighlight lang="zig">const std = @import("std");
 
pub fn main() void {
var x: f64 = -1.2345;
std.debug.print("e = {d}\n", .{std.math.e});
std.debug.print("pi = {d}\n", .{std.math.pi});
std.debug.print("sqrt(4) = {d}\n", .{std.math.sqrt(4)});
std.debug.print("ln(e) = {d}\n", .{std.math.ln(std.math.e)});
std.debug.print("exp(x) = {d}\n", .{std.math.exp(x)});
std.debug.print("abs(x) = {d}\n", .{std.math.absFloat(x)});
std.debug.print("floor(x) = {d}\n", .{std.math.floor(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)});
}</syntaxhighlight>
 
=={{header|zkl}}==
<langsyntaxhighlight lang="zkl">(0.0).e // Euler's number, a property of all floats
(0.0).e.pi // pi, yep, all floats
(2.0).sqrt() // square root
Line 2,866 ⟶ 3,569:
x.pow(y) // x raised to the y power
x.ceil() // ceiling
x.floor() // floor</langsyntaxhighlight>
 
{{omit from|GUISS}}
511

edits