Real constants and functions: Difference between revisions

Content added Content deleted
(→‎sqrt (optimized): added/changed comments and whitespace.)
m (Category:Simple, {{out}})
Line 1: 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]]
Show how to use the following math constants and functions in your language (if not available, note it):
*<math>e</math> (base of the natural logarithm)
*<math>e</math> (base of the natural logarithm)
*<math>\pi</math>
*<math>\pi</math>
Line 78: Line 81:
x ** y # power #
x ** y # power #
))</lang>
))</lang>
{{out}}
Output:
<pre> 2.71828; 3.14159; 1.64872; 0.43429; 1.00000; 15.15426; 2.71828; 2.00000; 3.00000; 23.14069; </pre>
<pre> 2.71828; 3.14159; 1.64872; 0.43429; 1.00000; 15.15426; 2.71828; 2.00000; 3.00000; 23.14069; </pre>


Line 117: Line 120:
# outputs 1.41421, 0.693147, 7.38906, 0.0947323</lang>
# outputs 1.41421, 0.693147, 7.38906, 0.0947323</lang>


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


<lang awk>BEGIN {
<lang awk>BEGIN {
Line 150: Line 158:


=={{header|Axe}}==
=={{header|Axe}}==
In general, Axe does not support many operations on real numbers. However, there are a few special cases that it does support.
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:
To take the square root of an integer X:
Line 691: Line 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:
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>
<lang groovy>println ((-22).abs())</lang>
{{out}}
Output:
<pre>22</pre>
<pre>22</pre>


Line 698: Line 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:
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>
<lang groovy>println 22**3.5</lang>
{{out}}
Output:
<pre>49943.547010599876</pre>
<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.
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>
<lang groovy>println ((-22)**3.5)</lang>
{{out}}
Output:
<pre>NaN</pre>
<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".
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}}==
=={{header|Haskell}}==
Line 755: Line 766:
[http://www.cs.arizona.edu/icon/library/src/procs/numbers.icn numbers provides floor and ceiling]
[http://www.cs.arizona.edu/icon/library/src/procs/numbers.icn numbers provides floor and ceiling]


{{out}}
Sample output:
<pre>e=2.718281828459045
<pre>e=2.718281828459045
pi=3.141592653589793
pi=3.141592653589793
Line 808: Line 819:


=={{header|jq}}==
=={{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.
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>
In the following, comments appear after the "#":<lang jq>
Line 923: Line 935:
:- end_object.
:- end_object.
</lang>
</lang>
{{out}}
Output:
<pre>
<lang text>
| ?- constants_and_functions::show.
| ?- constants_and_functions::show.
e = 2.718281828459045
e = 2.718281828459045
Line 936: Line 948:
2 ** -3.4 = 0.09473228540689989
2 ** -3.4 = 0.09473228540689989
yes
yes
</lang>
</pre>


=={{header|Lua}}==
=={{header|Lua}}==
Line 1,128: Line 1,140:
</lang>
</lang>


{{out}}
'''Output:'''

<pre>
<pre>
Java Math constants & functions:
Java Math constants & functions:
Line 1,443: Line 1,454:


(prinl (format (pow 3.0 4.0) *Scl)) # power</lang>
(prinl (format (pow 3.0 4.0) *Scl)) # power</lang>
{{out}}
Output:
<pre>2.718281828459
<pre>2.718281828459
3.141592653590
3.141592653590
Line 1,958: Line 1,969:
echo $(( pow(x,y) )) # power</lang>
echo $(( pow(x,y) )) # power</lang>


{{output}}
{{out}}
<pre>2.71828182845904524
<pre>2.71828182845904524
3.14159265358979324
3.14159265358979324
Line 1,999: Line 2,010:
] \ can be used for both reals & ints</lang>
] \ can be used for both reals & ints</lang>


{{out}}
Output:
<pre>
<pre>
2.7182818284590500
2.7182818284590500