Anonymous user
Real constants and functions: Difference between revisions
m
Category:Simple, {{out}}
(→sqrt (optimized): added/changed comments and whitespace.) |
m (Category:Simple, {{out}}) |
||
Line 1:
{{task|Basic language learning}}
{{task|Basic language learning}}[[Category:Arithmetic operations]]Show how to use the following math constants and functions in your language (if not available, note it):▼
[[Category:Arithmetic operations]]
[[Category:Simple]]
▲
*<math>e</math> (base of the natural logarithm)
*<math>\pi</math>
Line 78 ⟶ 81:
x ** y # power #
))</lang>
{{out}}
<pre> 2.71828; 3.14159; 1.64872; 0.43429; 1.00000; 15.15426; 2.71828; 2.00000; 3.00000; 23.14069; </pre>
Line 117 ⟶ 120:
# outputs 1.41421, 0.693147, 7.38906, 0.0947323</lang>
<blockquote style="font-size: smaller;">'''Power's note:'''
With [[nawk]] or [[gawk]], <code>2 ** -3.4</code> acts like <code>2 ^ -3.4</code>.
With [[mawk]], <code>2 ** -3.4</code> is a syntax error.
Nawk allows <code>**</code>, but its manual page only has <code>^</code>.
Gawk's manual warns, ''"The POSIX standard only specifies the use of `^' for exponentiation.
For maximum portability, do not use the `**' operator."''</blockquote>
Awk misses e, pi, absolute value, floor and ceiling; but these are all easy to implement
<lang awk>BEGIN {
Line 150 ⟶ 158:
=={{header|Axe}}==
In general, Axe does not support many operations on real numbers.
However, there are a few special cases that it does support.
To take the square root of an integer X:
Line 691 ⟶ 700:
In addition to the java.lang.Math.abs() method, each numeric type has an abs() method, which can be invoked directly on the number:
<lang groovy>println ((-22).abs())</lang>
{{out}}
<pre>22</pre>
Line 698 ⟶ 707:
In addition to the java.lang.Math.pow() method, each numeric type works with the power operator (**), which can be invoked as an in-fix operator between two numbers:
<lang groovy>println 22**3.5</lang>
{{out}}
<pre>49943.547010599876</pre>
Power results are not defined for all possible pairs of operands.
Any power operation that does not have a result returns a 64-bit IEEE NaN (Not a Number) value.
<lang groovy>println ((-22)**3.5)</lang>
{{out}}
<pre>NaN</pre>
Also note that at the moment (07:00, 19 March 2011 (UTC)) Groovy (1.7.7) gives a mathematically incorrect result for "0**0".
The correct result should be "NaN", but the Groovy operation result is "1".
=={{header|Haskell}}==
Line 755 ⟶ 766:
[http://www.cs.arizona.edu/icon/library/src/procs/numbers.icn numbers provides floor and ceiling]
{{out}}
<pre>e=2.718281828459045
pi=3.141592653589793
Line 808 ⟶ 819:
=={{header|jq}}==
The mathematical functions available in jq are defined as 0-arity filters, so to evaluate the sqrt of 4, one writes <tt>4|sqrt</tt>
In jq, "." refers to the output coming from the left in the pipeline.
In the following, comments appear after the "#":<lang jq>
Line 923 ⟶ 935:
:- end_object.
</lang>
{{out}}
<pre>
| ?- constants_and_functions::show.
e = 2.718281828459045
Line 936 ⟶ 948:
2 ** -3.4 = 0.09473228540689989
yes
</
=={{header|Lua}}==
Line 1,128 ⟶ 1,140:
</lang>
{{out}}
<pre>
Java Math constants & functions:
Line 1,443 ⟶ 1,454:
(prinl (format (pow 3.0 4.0) *Scl)) # power</lang>
{{out}}
<pre>2.718281828459
3.141592653590
Line 1,958 ⟶ 1,969:
echo $(( pow(x,y) )) # power</lang>
{{
<pre>2.71828182845904524
3.14159265358979324
Line 1,999 ⟶ 2,010:
] \ can be used for both reals & ints</lang>
{{out}}
<pre>
2.7182818284590500
|