Real constants and functions: Difference between revisions
m
syntax highlighting fixup automation
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
|||
Line 23:
{{trans|Python}}
<
math:pi // pi
sqrt(x) // square root
Line 32:
floor(x) // floor
ceil(x) // ceiling
x ^ y // exponentiation</
=={{header|6502 Assembly}}==
Line 38:
Absolute value can be handled like so:
<
eor #$ff
clc
adc #1
rts</
Line 48:
Only the last three are available as built in functions.
<
(ceiling 15 2)
(expt 15 2) ;; 15 squared</
Only the last three are available as built in functions.
Line 56:
define them using integer part:
<
if x < 0 then
-intof(x);
Line 66:
define ceiling(x);
-floor(-x);
enddefine;</
=={{header|Action!}}==
Line 72:
{{libheader|Action! Tool Kit}}
{{libheader|Action! Real Math}}
<
PROC Euler(REAL POINTER e)
Line 154:
PutE()
PrintE("There is no support in Action! for pi.")
RETURN</
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Real_constants_and_functions.png Screenshot from Atari 8-bit computer]
Line 176:
=={{header|ActionScript}}==
Actionscript has all the functions and constants mentioned in the task, available in the Math class.
<
Math.PI; //pi
Math.sqrt(u); //square root of u
Line 184:
Math.floor(u);//floor of u
Math.ceil(u); //ceiling of u
Math.pow(u,v);//u to the power of v</
The Math class also contains several other constants.
<
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</
=={{header|Ada}}==
Most of the constants and functions used in this task are defined in the pre-defined Ada package Ada.Numerics.Elementary_Functions.
<
Ada.Numerics.pi -- pi
sqrt(x) -- square root
Line 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</
=={{header|Aime}}==
<
exp(1);
# pi
Line 217:
floor(x);
ceil(x);
pow(x, y);</
=={{header|ALGOL 68}}==
<
printf(($g(-8,5)"; "$,
exp(1), # e #
Line 232:
-ENTIER -x, # ceiling #
x ** y # power #
))</
{{out}}
<pre> 2.71828; 3.14159; 1.64872; 0.43429; 1.00000; 15.15426; 2.71828; 2.00000; 3.00000; 23.14069; </pre>
Line 241:
=={{header|ALGOL W}}==
<
real t, u;
t := 10;
Line 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.</
=={{header|ARM Assembly}}==
{{omit from|ARM Assembly}}
<syntaxhighlight lang="text">
/* functions not availables */
</syntaxhighlight>
=={{header|Arturo}}==
<
print ["Pi:" pi]
Line 277:
print ["floor 23.536:" floor 23.536]
print ["ceil 23.536:" ceil 23.536]
print ["2 ^ 8:" 2 ^ 8]</
{{out}}
Line 293:
=={{header|Asymptote}}==
<
write("e = ", e);
write("pi = ", pi);
Line 310:
write("ceil = ", ceil(-e)); // ceiling
write("power = ", x ^ y); // power
write("power = ", x ** y); // power</
{{out}}
<pre>e = 2.71828182845905
Line 328:
=={{header|AutoHotkey}}==
The following math functions are built into AutoHotkey:
<
Log(Number) ; logarithm (base 10)
Ln(Number) ; natural logarithm (base e)
Line 335:
Floor(Number) ; floor
Ceil(Number) ; ceiling
x**y ; x to the power y</
No mathematical constants are built-in, but they can all be calculated:
<
pi:=2*asin(1)</
The following are additional trigonometric functions that are built into the AutoHotkey language:
<
Cos(Number) ; cosine
Tan(Number) ; tangent
ASin(Number) ; arcsine
ACos(Number) ; arccosine
ATan(Number) ; arctangent</
=={{header|AWK}}==
Awk has square root, logarithm, exponential and power.
<
print sqrt(2) # square root
print log(2) # logarithm base e
Line 356:
print 2 ^ -3.4 # power
}
# outputs 1.41421, 0.693147, 7.38906, 0.0947323</
<blockquote style="font-size: smaller;">'''Power's note:'''
Line 367:
Awk misses e, pi, absolute value, floor and ceiling; but these are all easy to implement:
<
E = exp(1)
PI = atan2(0, -1)
Line 393:
print ceil(-3.4) # ceiling
}
# outputs 2.71828, 3.14159, 3.4, -4, -3</
=={{header|Axe}}==
Line 400:
To take the square root of an integer X:
<syntaxhighlight lang
To take the square root of an 8.8 fixed-point number Y:
<syntaxhighlight lang
To take the base-2 logarithm of an integer X:
<syntaxhighlight lang
To take 2 raised to an integer X: (Note that the base is not Euler's number)
<syntaxhighlight lang
To take the absolute value of a signed integer X:
<syntaxhighlight lang
=={{header|BASIC}}==
{{works with|QuickBasic|4.5}}
<
sqr(x) 'square root
exp(x) 'exponential
log(x) 'natural logarithm
x ^ y 'power
'floor, ceiling, e, and pi not available</
==={{header|IS-BASIC}}===
<
110 PRINT EXP(1) ! value of e
120 PRINT PI ! value of Pi
Line 443:
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</
==={{header|Sinclair ZX81 BASIC}}===
Line 449:
Base of the natural logarithm:
<syntaxhighlight lang
<math>\pi</math>:
<syntaxhighlight lang
Square root:
<syntaxhighlight lang
Natural logarithm:
<syntaxhighlight lang
Exponential:
<syntaxhighlight lang
Absolute value:
<syntaxhighlight lang
Floor:
<syntaxhighlight lang
(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:
Power:
<syntaxhighlight lang
NB. Both <math>x</math> and <math>y</math> can be real numbers.
==={{header|BBC BASIC}}===
<
Pi = PI
Sqr2 = SQR(2)
Line 491:
DEF FNceil(n) = INT(n) - (INT(n) <> n)
</syntaxhighlight>
=={{header|BASIC256}}==
<
print "e = "; e
print "PI = "; PI
Line 509:
print "floor = "; floor(-e) # floor
print "ceil = "; ceil(-e) # ceiling
print "power = "; x ^ y # power</
{{out}}
<pre>e = 2.71828182846
Line 526:
The language has square root and power, but power only works if the exponent is an integer.
<
sqrt(2) /* 1.414213 square root */
4.3 ^ -2 /* .054083 power (integer exponent) */</
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}}
<
l(2) /* .693147 natural logarithm */
e(2) /* 7.389056 exponential */
Line 543:
e(l(2) * -3.4) /* .094734 2 to the power of -3.4 */
l(1024) / l(2) /* 10.000001 logarithm base 2 of 1024 */</
The missing functions are absolute value, floor and ceiling. You can implement these functions, if you know what to do.
{{trans|AWK}}
<
define v(x) {
if (x < 0) return (-x)
Line 582:
v(-3.4) /* 3.4 absolute value */
f(-3.4) /* -4 floor */
g(-3.4) /* -3 ceiling */</
=={{header|blz}}==
The constant e
<syntaxhighlight lang
The constant pi
<syntaxhighlight lang
Square root
<syntaxhighlight lang
Logarithm (base n)
<syntaxhighlight lang
Exponential
<syntaxhighlight lang
Absolute Value
<syntaxhighlight lang
Floor
<syntaxhighlight lang
Ceiling
<syntaxhighlight lang
Power x to the y
<syntaxhighlight lang
=={{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>
<
has the result
<
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:
=={{header|C}}==
Most of the following functions take a double.
<
M_E; /* e - not standard but offered by most implementations */
Line 646:
floor(x); /* floor */
ceil(x); /* ceiling */
pow(x,y); /* power */</
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}}==
<
class Program {
Line 666:
Console.WriteLine(Math.Pow(2, 5)); // Exponentiation
}
}</
=={{header|C++}}==
=== using Math macros ===
<
#include <cmath>
Line 697:
<< "\nceiling(4.5) = " << std::ceil(4.5)
<< "\npi^2 = " << std::pow(pi,2.0) << std::endl;
}</
=== using Boost ===
{{libheader|Boost}}
<
#include <iomanip>
#include <cmath>
Line 718:
<< "\nfloor(4.5) = " << std::floor(4.5)
<< "\nceiling(4.5) = " << std::ceil(4.5) << std::endl;
}</
{{out}}
<pre>e = 2.71828182845904509
Line 736:
=={{header|Clojure}}==
{{trans|Java}} which is directly available.
<
(Math/PI); //pi
(Math/sqrt x); //square root--cube root also available (cbrt)
Line 744:
(Math/floor x); //floor
(Math/ceil x); //ceiling
(Math/pow x y); //power</
Clojure does provide arbitrary precision versions as well:
<
(math/sqrt x)
(math/abs x)
(math/floor x)
(math/ceil x)
(math/expt x y) </
.. and as multimethods that can be defined for any type (e.g. complex numbers).
<
(generic/sqrt x)
(generic/log x)
Line 764:
(generic/floor x)
(generic/ceil x)
(generic/pow x y)</
=={{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:
<
PI *> Pi
SQRT(n) *> Sqaure root
Line 781:
MOVE INTEGER(N) TO Result
*> There is no pow function, although the COMPUTE verb does have an exponention operator.
COMPUTE Result = N ** 2 </
COBOL also has the following extra mathematical functions:
<
EXP10(n) *> 10 to the nth power
*> Trigonometric functions, including inverse ones, named as would be expected.</
=={{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.
<
(exp 1) ; e (Euler's number)
pi ; pi constant
Line 800:
(ceiling x) ; ceiling: restricted to real, two valued (second value gives residue)
(expt x y) ; power
</syntaxhighlight>
=={{header|Crystal}}==
<
y = 4
Line 823:
puts exp(x) # puts Math.exp(x) -- exponential
puts E**x # puts Math::E**x -- same
</syntaxhighlight>
{{0ut}}<pre>
Line 843:
=={{header|D}}==
<
E // Euler's number
PI // pi constant
Line 854:
floor(x) // floor
ceil(x) // ceiling
pow(x,y) // power</
=={{header|Delphi}}==
Delphi supports all basic Standard Pascal (ISO 7185) functions shown in [[#Pascal|§ Pascal]].
Furthermore, the following is possible, too:
<
LogN(BASE, x) // log of x for a specified base
Log2(x) // log of x for base 2
Line 865:
Floor(x); // floor
Ceil(x); // ceiling
Power(x, y); // power</
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:
=={{header|E}}==
<
# value: 2.7182818284590455
Line 897:
? 10 ** 6
# value: 1000000</
=={{header|Elena}}==
ELENA 4.x :
<
import extensions;
Line 916:
console.printLine(10.0r.ceil()); //Ceiling
console.printLine(2.power(5)); //Exponentiation
}</
=={{header|Elixir}}==
<
def main do
IO.puts :math.exp(1) # e
Line 934:
end
Real_constants_and_functions.main</
=={{header|Elm}}==
The following are all in the Basics module, which is imported by default:
<
pi -- pi
sqrt x -- square root
Line 946:
floor x -- floor
ceiling x -- ceiling
2 ^ 3 -- power</
=={{header|Erlang}}==
<
-module(math_constants).
-export([main/0]).
Line 984:
false -> T + 1
end.
</syntaxhighlight>
{{out}}
<pre>2.718281828459045
Line 1,001:
=={{header|ERRE}}==
<
FUNCTION CEILING(X)
Line 1,028:
PRINT(CEILING(X)) ! ceiling
PRINT(X^Y) ! power
END PROGRAM</
{{out}}
<pre> 2.718282
Line 1,045:
=={{header|F Sharp|F#}}==
{{trans|C#|C sharp}}
<
let main _ =
Line 1,059:
Console.WriteLine(Math.Pow(2.0, 5.0)); // Exponentiation
0</
=={{header|Factor}}==
<
pi ! π
sqrt ! square root
Line 1,072:
truncate ! remove the fractional part (i.e. round towards 0)
round ! round to next whole number
^ ! power</
=={{header|Fantom}}==
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>
<
Float.e
Float.pi
Line 1,090:
3.2f.round // nearest Int
3f.pow(2f) // power
</syntaxhighlight>
Note, . binds more tightly than -, so use brackets around negative numbers:
Line 1,102:
=={{header|Forth}}==
<
0e facos 2e f* fconstant pi \ predefined in gforth
fsqrt ( f -- f )
Line 1,110:
floor ( f -- f ) \ round towards -inf
: ceil ( f -- f ) fnegate floor fnegate ; \ not standard, though fround is available
f** ( f e -- f^e )</
=={{header|Fortran}}==
<
pi ! Not available. Can be calculated 4.0*ATAN(1.0)
SQRT(x) ! square root
Line 1,122:
FLOOR(x) ! floor - Fortran 90 or later only
CEILING(x) ! ceiling - Fortran 90 or later only
x**y ! x raised to the y power</
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:
=={{header|FreeBASIC}}==
<
#Include "crt/math.bi"
Line 1,143:
Print ceil(-2.5) '' ceiling function from C runtime library
Print 2.5 ^ 3.5 '' exponentiation operator built into FB
Sleep </
{{out}}
Line 1,164:
=={{header|Frink}}==
All of the following operations work for any numerical type, including rational numbers, complex numbers and intervals of real numbers.
<
e
pi, π // Unicode can also be written in ASCII programs as \u03C0
Line 1,175:
ceil[x] // Except for complex numbers where there's no good interpretation.
x^y
</syntaxhighlight>
=={{header|FutureBasic}}==
<
text ,,,,, 60// set tab width
Line 1,193:
print @"power:", 1.23 ^ 4
HandleEvents</
Output:
<pre>
Line 1,209:
=={{header|Go}}==
<
import (
Line 1,262:
fmt.Println("x:", x)
fmt.Println("abs(x):", y.Abs(x))
}</
{{out}}
<pre>
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:
<
{{out}}
<pre>22</pre>
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:
<syntaxhighlight lang
{{out}}
<pre>49943.547010599876</pre>
Line 1,305:
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.
<
{{out}}
<pre>NaN</pre>
Line 1,314:
=={{header|Haskell}}==
The operations are defined for the various numeric typeclasses, as defined in their type signature.
<
pi -- pi
sqrt x -- square root
Line 1,324:
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)</
=={{header|HicEst}}==
Except for x^y, this is identical to Fortran:
<
pi ! Not available. Can be calculated 4.0*ATAN(1.0)
x^0.5 ! square root
Line 1,338:
CEILING(x) ! ceiling
x**y ! x raised to the y power
x^y ! same as x**y</
=={{header|Icon}} and {{header|Unicon}}==
<
procedure main()
Line 1,355:
write("ceil(-2.2)=",ceil(-2.2))
write("power: 3^3=",3^3)
end</
{{libheader|Icon Programming Library}}
[http://www.cs.arizona.edu/icon/library/src/procs/numbers.icn numbers provides floor and ceiling]
Line 1,373:
=={{header|J}}==
The examples below require arguments (x and y) to be numeric nouns.
<
e =. ^ 1 NB. Euler's number, computed by exponentiation.
pi=. 1p1 NB. pi, specified as a numeric literal.
Line 1,386:
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</
=={{header|Java}}==
All of these functions are in Java's <tt>Math</tt> class which, does not require any imports:
<
Math.PI; //pi
Math.sqrt(x); //square root--cube root also available (cbrt)
Line 1,398:
Math.floor(x); //floor
Math.ceil(x); //ceiling
Math.pow(x,y); //power</
=={{header|JavaScript}}==
<
Math.PI
Math.sqrt(x)
Line 1,409:
Math.floor(x)
Math.ceil(x)
Math.pow(x,y)</
=={{header|jq}}==
Line 1,415:
In jq, "." refers to the output coming from the left in the pipeline.
In the following, comments appear after the "#":<
1 | exp # i.e. e
1 | atan * 4 # i.e. π
Line 1,424:
floor
ceil # requires jq >= 1.5
pow(x; y) # requires jq >= 1.5</
=={{header|Jsish}}==
<
var x, y;
Line 1,469:
Math.pow(x,y) ==> 100000
=!EXPECTEND!=
*/</
{{out}}
Line 1,494:
=={{header|Julia}}==
<
π, pi
sqrt(x)
Line 1,502:
floor(x)
ceil(x)
x^y</
Note that Julia supports Unicode identifiers, and allows either <code>π</code> or <code>pi</code> for that constant.
Line 1,508:
=={{header|Kotlin}}==
<
fun main(args: Array<String>) {
Line 1,521:
println(Math.ceil(-2.5)) // ceiling
println(Math.pow(2.5, 3.5)) // power
}</
{{out}}
Line 1,538:
=={{header|Lambdatalk}}==
<
{E} -> 2.718281828459045
{PI} -> 3.141592653589793
Line 1,548:
{ceil -2.5} -> -2
{pow 2.5 3.5} -> 24.705294220065465
</syntaxhighlight>
=={{header|Lasso}}==
<syntaxhighlight lang="lasso">//e
define e => 2.7182818284590452
Line 1,566:
1.64->floor
1.64->ceil
1.64->pow(10.0)</
=={{header|Liberty BASIC}}==
Line 1,574:
<br>
e & pi not available- calculate as shown.
<syntaxhighlight lang="lb">
print exp( 1) ' e not available
print 4 *atn( 1) ' pi not available
Line 1,607:
end if
end function
</syntaxhighlight>
=={{header|Lingo}}==
<
-- e (base of the natural logarithm)
Line 1,656:
-- power
put power(2, 8)
-- 256.00000000</
=={{header|LiveCode}}==
LC 7.1+, prior to this floor & ceil were not built-in.
<
pi: pi
square root: sqrt(x)
Line 1,668:
floor: floor(x)
ceiling: ceil(x)
power: x^y</
=={{header|Logo}}==
{{works with|UCB Logo}}
<
make "pi 2*(RADARCTAN 0 1)
sqrt :x
Line 1,678:
exp :x
; there is no standard abs, floor, or ceiling; only INT and ROUND.
power :x :y</
=={{header|Logtalk}}==
<
:- object(constants_and_functions).
Line 1,698:
:- end_object.
</syntaxhighlight>
{{out}}
<pre>
Line 1,715:
=={{header|Lua}}==
<
math.pi
math.sqrt(x)
Line 1,724:
math.floor(x)
math.ceil(x)
x^y</
=={{header|M2000 Interpreter}}==
<syntaxhighlight lang="m2000 interpreter">
Module Checkit {
Def exp(x)= 2.71828182845905^x
Line 1,766:
}
Checkit
</syntaxhighlight>
=={{header|Maple}}==
<
0</
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">E
Pi
Sqrt[x]
Line 1,782:
Floor[x]
Ceiling[x]
Power[x, y]</
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:
<
Log[10] => Log[10]
Log[10,100] => 2
Line 1,800:
Sqrt[-4] => 2I
Exp[2] => E^2
Exp[Log[4]] => 4</
=={{header|MATLAB}} / {{header|Octave}}==
<
pi % pi
sqrt(x) % square root
Line 1,813:
floor(x) % floor
ceil(x) % ceiling
x^y % power</
=={{header|MAXScript}}==
<
pi -- pi
log x -- natural logarithm
Line 1,824:
floor x -- floor
ceil x -- ceiling
pow x y -- power</
=={{header|Mercury}}==
<syntaxhighlight lang="text">
math.pi % Pi.
math.e % Euler's number.
Line 1,839:
math.floor(X) % Floor of X.
math.ceiling(X) % Ceiling of X.
math.pow(X, Y) % X raised to the power of Y.</
=={{header|Metafont}}==
<
show 3.14159; % no pi constant built in; of course we can define it
% in several ways... even computing
Line 1,856:
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</
=={{header|min}}==
{{works with|min|0.19.3}}
<
pi ; π
sqrt ; square root
Line 1,871:
trunc ; remove the fractional part (i.e. round towards 0)
round ; round number to nth decimal place
pow ; power</
=={{header|МК-61/52}}==
<syntaxhighlight lang="text">1 e^x С/П
пи С/П
Line 1,892:
x<0 14 ИП1 С/П ИП1 1 + С/П
x^y С/П</
=={{header|Modula-3}}==
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.
<
Math.Pi;
Math.sqrt(x);
Line 1,906:
FLOOR(x); (* Built in function. *)
CEILING(x); (* Built in function. *)
Math.pow(x, y);</
=={{header|Neko}}==
<syntaxhighlight lang="actionscript">/**
Real constants and functions, in Neko
Tectonics:
Line 1,936:
$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")</
{{out}}
Line 1,953:
=={{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.
<
options replace format comments java crossref symbols nobinary utf8
Line 2,002:
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>
{{out}}
Line 2,046:
=={{header|Nim}}==
<
var x, y = 12.5
Line 2,059:
echo floor(x)
echo ceil(x)
echo pow(x, y)</
=={{header|Objeck}}==
<
Float->E();
4.0->SquareRoot();
Line 2,070:
3.99->Floor();
3.99->Ceiling();
4.5->Ceiling(2.0);</
=={{header|OCaml}}==
Unless otherwise noted, the following functions are for floats only:
<
sqrt x (* square root *)
log x (* natural logarithm--log base 10 also available (log10) *)
Line 2,083:
ceil x (* ceiling *)
x ** y (* power *)
-. x (* negation for floats *)</
=={{header|Octave}}==
<
pi % pi
sqrt(pi) % square root
Line 2,094:
floor(pi) % floor
ceil(pi) % ceiling
e**pi % power</
=={{header|Oforth}}==
<
: testReal
Line 2,121:
-2.4 ceil println
-3.9 ceil println
-5.5 ceil println ;</
=={{header|ooRexx}}==
{{trans|NetRexx}}
{{uses from|OoRexx|RxMath}}
<
-- MathLoadFuncs & MathDropFuncs are no longer needed and are effectively NOPs
Line 2,183:
return arg(1)~trunc() + (arg(1) > 0) * (arg(1) \= arg(1)~trunc())
::requires 'RxMath' library</
{{out}}
<pre>
Line 2,226:
=={{header|Oz}}==
<
[
{Exp 1.} %% 2.7183 Euler's number: not predefined
Line 2,237:
{Pow 2 3} %% 8 power; both arguments must be of the same type
]
Show}</
=={{header|PARI/GP}}==
<
=={{header|Pascal}}==
''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:
<
exp(1)
{ principal square root of `x` }
Line 2,254:
exp(x)
{ absolute value }
abs(x)</
{{works with|Extended Pascal}}
Additionally, in Extended Pascal (ISO standard 10206) following operators and expressions can be used:
<
2 * arg(cmplx(0.0, maxReal))
{ power, yields same data type as `base`, `exponent` has to be an `integer` }
Line 2,264:
{ `exponent` are automatically promoted to an approximate `real` value, result }
{ is `complex` if `base` is `complex`, otherwise a `real` value }
base ** exponent</
<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}}==
<
exp(1); # e
Line 2,285:
use Math::Complex;
pi; # alternate way to get pi</
=={{header|Phix}}==
<!--<
<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>
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: #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>
<!--</
=={{header|PHP}}==
<
M_PI; //pi
sqrt(x); //square root
Line 2,315:
floor(x); //floor
ceil(x); //ceiling
pow(x,y); //power</
=={{header|Picat}}==
<
println(math.e),
println(math.pi),
Line 2,339:
println(math.pi**math.e), % power
println(pow(math.pi,math.e)), % power
nl.</
{{out}}
Line 2,367:
(about 16 digits). The default precision is six, and can be changed with
'[http://software-lab.de/doc/refS.html#scl scl]':
<
(load "@lib/math.l")
Line 2,382:
(prinl (abs -123))
(prinl (format (pow 3.0 4.0) *Scl)) # power</
{{out}}
<pre>2.718281828459
Line 2,395:
=={{header|PL/I}}==
<
/* pi not available other than by using a trig function such as: pi=4*atan(1) */
y = sqrt(x);
Line 2,410:
y = erfc(x); /* the error function complemented. */
y = gamma (x);
y = loggamma (x);</
=={{header|Pop11}}==
<
sqrt(x) ;;; Square root
log(x) ;;; Natural logarithm
exp(x) ;;; Exponential function
abs(x) ;;; Absolute value
x ** y ;;; x to the power y</
The number e is not provided directly, one has to compute 'exp(1)'
Line 2,427:
=={{header|PowerShell}}==
Since PowerShell has access to .NET all this can be achieved using the .NET Base Class Library:
<
Write-Host ([Math]::Pi)
Write-Host ([Math]::Sqrt(2))
Line 2,435:
Write-Host ([Math]::Floor(3.14))
Write-Host ([Math]::Ceiling(3.14))
Write-Host ([Math]::Pow(2, 3))</
=={{header|PureBasic}}==
<
Debug #PI
Debug Sqr(f)
Line 2,445:
Debug Log10(f)
Debug Abs(f)
Debug Pow(f,f)</
=={{header|Python}}==
<
math.e # e
Line 2,463:
# The math module constants and functions can, of course, be imported directly by:
# from math import e, pi, sqrt, log, log10, exp, floor, ceil</
=={{header|R}}==
<
pi # pi
sqrt(x) # square root
Line 2,476:
floor(x) # floor
ceiling(x) # ceiling
x^y # power</
=={{header|Racket}}==
<
pi ; pi
(sqrt x) ; square root
Line 2,487:
(floor x) ; floor
(ceiling x) ; ceiling
(expt x y) ; power</
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku"
say π; # or pi # pi
say τ; # or tau # tau
Line 2,528:
say pi.ceiling; # Ceiling
say e ** π\i + 1 ≅ 0; # :-)</
=={{header|REXX}}==
Line 2,535:
REXX doesn't have any built-in (math) constants.
===abs===
<
===exponentiation (**)===
<
/*Y may be negative, zero, positive.*/
/*X may be any real number. */</
===ceiling===
A ceiling function for REXX:
<
ceiling: procedure; parse arg x; t=trunc(x); return t+(x>0)*(x\=t)
</syntaxhighlight>
===floor===
A floor function for REXX:
<
floor: procedure; parse arg x; t=trunc(x); return t-(x<0)-(x\=t)
</syntaxhighlight>
===sqrt (optimized)===
A [principal] square root (SQRT) function for REXX (with arbitrary precision):
<
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,581:
/* [↓] normalize √ ──► original digits*/
numeric digits d /* [↓] make answer complex if X < 0. */
return (g/1)i /*normalize, and add possible I suffix.*/</
<
╔═╝ __ ╚═╗
║ √ ║
Line 2,607:
║ __ ║
╚═╗ √ ╔═╝
╚════════════════════════════════════════════════════════════════════╝</
===sqrt (simple)===
<
sqrt: procedure; arg x /*a simplistic SQRT subroutine.*/
if x=0 then return 0 /*handle special case of zero. */
Line 2,623:
end /*forever*/ /* [↑] ···'til we run out of digs*/
numeric digits d /*restore the original precision.*/
return g/1 /*normalize to old precision (d).*/</
===other===
Other mathematical-type functions supported are:
<
numeric fuzz fff /*arithmetic comparisons with FFF fuzzy*/
numeric form kkk /*exponential: scientific | engineering*/
Line 2,653:
bb=x2b(hhh) /*converts hexadecimal to binary (bits)*/
cc=x2c(hhh) /*converts hexadecimal to character. */
dd=x2d(hhh) /*converts hexadecimal to decimal. */</
=={{header|Ring}}==
<
See "Mathematical Functions" + nl
See "Sin(0) = " + sin(0) + nl
Line 2,702:
see "sqrt(16) = " + sqrt(16) + nl
</syntaxhighlight>
=={{header|RLaB}}==
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]].
<
e euler ln10 ln2 lnpi
log10e log2e pi pihalf piquarter
rpi sqrt2 sqrt2r sqrt3 sqrtpi
tworpi</
=== 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.
<
F G J L N
Na R0 Ry Tsp V0
Line 2,741:
therm tntton ton torr toz
tsp uam ukgal ukton uston
week yd</
=== Elementary Functions ===
<
>> sqrt(x)
2.23606798
Line 2,760:
5
>> x .^ 2
25</
=={{header|Ruby}}==
<
x.magnitude #absolute value
x.floor #floor
Line 2,776:
log10(x) #base 10 logarithm
exp(x) #exponential
</syntaxhighlight>
=={{header|Run BASIC}}==
<
print "PI:";chr$(9); 22/7
print "Sqr2:";chr$(9); SQR(2)
Line 2,787:
print "Floor:";chr$(9); INT(1.534)
print "ceil:";chr$(9); val(using("###",1.534))
print "Power:";chr$(9); 1.23^4</
<pre>exp: 2.71828183
PI: 3.14285707
Line 2,799:
=={{header|Rust}}==
<
fn main() {
Line 2,822:
assert_eq!(x, 4.0);
}</
=={{header|Scala}}==
<
println(math.E) // e
println(math.Pi) // pi
Line 2,836:
println(math.ceil(-2.5)) // ceiling
println(math.pow(2.5, 3.5)) // power
}</
=={{header|Scheme}}==
<
(log x) ;natural logarithm
(exp x) ;exponential
Line 2,845:
(floor x) ;floor
(ceiling x) ;ceiling
(expt x y) ;power</
=={{header|Seed7}}==
Line 2,875:
=={{header|Sidef}}==
<
Num.pi # pi
x.sqrt # square root
Line 2,884:
x.floor # floor
x.ceil # ceiling
x**y # exponentiation</
=={{header|Slate}}==
<
numerics Pi.
n sqrt.
Line 2,897:
n floor.
n ceiling.
n raisedTo: anotherNumber</
=={{header|Smalltalk}}==
<
Float pi.
aNumber sqrt.
Line 2,909:
aNumber floor.
aNumber ceiling.
aNumber raisedTo: anotherNumber</
=={{header|Sparkling}}==
<
print(M_E);
Line 2,942:
// power
let eighty_one = pow(3, 4);</
=={{header|Standard ML}}==
<
Math.pi; (* pi *)
Math.sqrt x; (* square root *)
Line 2,954:
ceil x; (* ceiling *)
Math.pow (x, y); (* power *)
~ x; (* negation *)</
=={{header|Stata}}==
<
scalar y=3
di exp(1)
Line 2,969:
di floor(x)
di ceil(x)
di x^y</
=={{header|Swift}}==
<
M_E // e
Line 2,982:
floor(x) // floor
ceil(x) // ceiling
pow(x,y) // power</
=={{header|Tcl}}==
<
expr {4 * atan(1)} ;# pi -- also, simpler: expr acos(-1)
expr {sqrt($x)} ;# square root
Line 2,993:
expr {floor($x)} ;# floor
expr {ceil($x)} ;# ceiling
expr {$x**$y} ;# power, also pow($x,$y)</
The constants <math>e</math> and <math>\pi</math> are also available with high precision in a support library.
{{tcllib|math::constants}}
<
math::constants::constants e pi
puts "e = $e, pi = $pi"</
=={{header|TI-89 BASIC}}==
Line 3,025:
=={{header|True BASIC}}==
<
IF x > 0 THEN
LET floor = INT(x)
Line 3,049:
PRINT "ceil = "; CEIL(x) ! ceiling
PRINT "power = "; x ^ y ! power
END</
=={{header|UNIX Shell}}==
{{works with|ksh93}}
ksh93 exposes math functions from the C math library
<
echo $(( acos(-1) )) # PI
x=5
Line 3,068:
echo $(( ceil(x) )) # ceiling
x=10 y=3
echo $(( pow(x,y) )) # power</
{{out}}
Line 3,084:
=={{header|Wren}}==
<
System.print("e = %(e)")
Line 3,094:
System.print("floor(e) = %(e.floor)")
System.print("ceil(e) = %(e.ceil)")
System.print("pow(e, 2) = %(e.pow(2))")</
{{out}}
Line 3,110:
=={{header|XPL0}}==
<
func real Power(X, Y); \X raised to the Y power
Line 3,135:
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</
{{out}}
Line 3,157:
=={{header|Yabasic}}==
<
print "pi = ", pi
Line 3,171:
print "ceil = ", ceil(-euler) // ceiling
print "power = ", x ^ y, " ", x ** y // power
end</
{{out}}
<pre>e = 2.71828
Line 3,185:
=={{header|Zig}}==
<
pub fn main() void {
Line 3,198:
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)});
}</
=={{header|zkl}}==
<
(0.0).e.pi // pi, yep, all floats
(2.0).sqrt() // square root
Line 3,210:
x.pow(y) // x raised to the y power
x.ceil() // ceiling
x.floor() // floor</
{{omit from|GUISS}}
|