Mercurial Hosting > luan
changeset 566:90b93790c544
Number and Math documentation and minor changes
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sun, 05 Jul 2015 18:26:04 -0600 |
parents | 22bfd8a2eaee |
children | 6c00b8a59240 |
files | core/src/luan/modules/Math.luan core/src/luan/modules/MathLuan.java core/src/luan/modules/Number.luan website/src/manual.html.luan |
diffstat | 4 files changed, 137 insertions(+), 201 deletions(-) [+] |
line wrap: on
line diff
diff -r 22bfd8a2eaee -r 90b93790c544 core/src/luan/modules/Math.luan --- a/core/src/luan/modules/Math.luan Sun Jul 05 00:47:00 2015 -0600 +++ b/core/src/luan/modules/Math.luan Sun Jul 05 18:26:04 2015 -0600 @@ -2,6 +2,8 @@ local MathLuan = require "java:luan.modules.MathLuan" local JavaMath = require "java:java.lang.Math" local Integer = require "java:java.lang.Integer" +local Double = require "java:java.lang.Double" + local M = {} @@ -16,22 +18,19 @@ M.deg = MathLuan.deg M.exp = MathLuan.exp M.floor = MathLuan.floor +M.fmod = MathLuan.fmod +M.huge = Double.POSITIVE_INFINITY M.log = MathLuan.log +M.max = MathLuan.max +M.max_integer = Integer.MAX_VALUE M.min = MathLuan.min M.min_integer = Integer.MIN_VALUE -M.max = MathLuan.max -M.max_integer = Integer.MAX_VALUE M.modf = MathLuan.modf M.pi = JavaMath.PI -M.pow = MathLuan.pow M.rad = MathLuan.rad M.random = MathLuan.random M.sin = MathLuan.sin -M.sinh = MathLuan.sinh M.sqrt = MathLuan.sqrt M.tan = MathLuan.tan -M.tanh = MathLuan.tanh - -M.long_to_string = MathLuan.long_to_string return M
diff -r 22bfd8a2eaee -r 90b93790c544 core/src/luan/modules/MathLuan.java --- a/core/src/luan/modules/MathLuan.java Sun Jul 05 00:47:00 2015 -0600 +++ b/core/src/luan/modules/MathLuan.java Sun Jul 05 18:26:04 2015 -0600 @@ -54,8 +54,12 @@ return Math.floor(x); } - public static double log(double x) { - return Math.log(x); + public static double fmod(double x,double y) { + return x % y; + } + + public static double log(double x,Double base) { + return base==null ? Math.log(x) : Math.log(x)/Math.log(base); } public static double min(double x,double... a) { @@ -79,10 +83,6 @@ return new double[]{i,x-i}; } - public static double pow(double x,double y) { - return Math.pow(x,y); - } - public static double rad(double x) { return Math.toRadians(x); } @@ -99,10 +99,6 @@ return Math.sin(x); } - public static double sinh(double x) { - return Math.sinh(x); - } - public static double sqrt(double x) { return Math.sqrt(x); } @@ -111,12 +107,8 @@ return Math.tan(x); } - public static double tanh(double x) { - return Math.tanh(x); - } - - public static String long_to_string(long i,Integer radix) { - return radix==null ? Long.toString(i) : Long.toString(i,radix); + public static String long_to_string(long i,int radix) { + return Long.toString(i,radix); } }
diff -r 22bfd8a2eaee -r 90b93790c544 core/src/luan/modules/Number.luan --- a/core/src/luan/modules/Number.luan Sun Jul 05 00:47:00 2015 -0600 +++ b/core/src/luan/modules/Number.luan Sun Jul 05 18:26:04 2015 -0600 @@ -1,11 +1,14 @@ java() local BasicLuan = require "java:luan.modules.BasicLuan" +local MathLuan = require "java:luan.modules.MathLuan" + local M = {} M.double = BasicLuan.assert_double M.integer = BasicLuan.assert_integer M.long = BasicLuan.assert_long -M.number_type = BasicLuan.number_type +M.long_to_string = MathLuan.long_to_string +M.type = BasicLuan.number_type return M
diff -r 22bfd8a2eaee -r 90b93790c544 website/src/manual.html.luan --- a/website/src/manual.html.luan Sun Jul 05 00:47:00 2015 -0600 +++ b/website/src/manual.html.luan Sun Jul 05 18:26:04 2015 -0600 @@ -92,6 +92,8 @@ <li><a href="#package_lib">Modules</a></li> <li><a href="#string_lib">String Manipulation</a></li> <li><a href="#table_lib">Table Manipulation</a></li> + <li><a href="#number_lib">Number Manipulation</a></li> + <li><a href="#math_lib">Mathematical Functions</a></li> </ul> </div> @@ -2765,36 +2767,69 @@ - - - -<h2>6.7 – <a name="6.7">Mathematical Functions</a></h2> +<h3 heading><a name="number_lib">Number Manipulation</a></h3> + +<p> +Include this library by: + +<pre> + local Number = require "luan:Number" +</pre> + + +<h4 heading><a name="Number.double"><code>Number.double (x)</code></a></h4> +<p> +Returns <code>x</code> as a double. + + +<h4 heading><a name="Number.integer"><code>Number.integer (x)</code></a></h4> +<p> +If the value <code>x</code> is convertible to an integer, +returns that integer. +Otherwise throws an error. + + +<h4 heading><a name="Number.long"><code>Number.long (x)</code></a></h4> +<p> +If the value <code>x</code> is convertible to an long, +returns that long. +Otherwise throws an error. + + +<h4 heading><a name="Number.long_to_string"><code>Number.long_to_string (i, radix)</code></a></h4> +<p> +Converts long value <code>i</code> to a string by calling <code><a href="http://docs.oracle.com/javase/7/docs/api/java/lang/Long.html#toString(long,%20int)">Long.toString</a></code>. + + +<h4 heading><a name="Number.type"><code>Number.type (x)</code></a></h4> +<p> +Returns a string for the numeric type of <code>x</code>. Possible return values include "<code>integer</code>", "<code>long</code>", "<code>double</code>", and "<code>float</code>". + + + + +<h3 heading><a name="math_lib">Mathematical Functions</a></h3> + +<p> +Include this library by: + +<pre> + local Math = require "luan:Math" +</pre> <p> This library provides basic mathematical functions. -It provides all its functions and constants inside the table <a name="pdf-math"><code>math</code></a>. -Functions with the annotation "<code>integer/float</code>" give -integer results for integer arguments -and float results for float (or mixed) arguments. -Rounding functions -(<a href="#pdf-math.ceil"><code>math.ceil</code></a>, <a href="#pdf-math.floor"><code>math.floor</code></a>, and <a href="#pdf-math.modf"><code>math.modf</code></a>) -return an integer when the result fits in the range of an integer, -or a float otherwise. - - -<p> -<hr><h3><a name="pdf-math.abs"><code>math.abs (x)</code></a></h3> - - -<p> -Returns the absolute value of <code>x</code>. (integer/float) - - - - -<p> -<hr><h3><a name="pdf-math.acos"><code>math.acos (x)</code></a></h3> - +It provides all its functions and constants inside the table <code>Math</code>. + + +<h4 heading><a name="Math.abs"><code>Math.abs (x)</code></a></h4> + +<p> +Returns the absolute value of <code>x</code>. + + + +<h4 heading><a name="Math.acos"><code>Math.acos (x)</code></a></h4> <p> Returns the arc cosine of <code>x</code> (in radians). @@ -2802,9 +2837,7 @@ -<p> -<hr><h3><a name="pdf-math.asin"><code>math.asin (x)</code></a></h3> - +<h4 heading><a name="Math.asin"><code>Math.asin (x)</code></a></h4> <p> Returns the arc sine of <code>x</code> (in radians). @@ -2812,29 +2845,18 @@ -<p> -<hr><h3><a name="pdf-math.atan"><code>math.atan (y [, x])</code></a></h3> - - -<p> - +<h4 heading><a name="Math.atan"><code>Math.atan (y, x)</code></a></h4> + +<p> Returns the arc tangent of <code>y/x</code> (in radians), but uses the signs of both parameters to find the quadrant of the result. (It also handles correctly the case of <code>x</code> being zero.) -<p> -The default value for <code>x</code> is 1, -so that the call <code>math.atan(y)</code> -returns the arc tangent of <code>y</code>. - - - - -<p> -<hr><h3><a name="pdf-math.ceil"><code>math.ceil (x)</code></a></h3> - + + +<h4 heading><a name="Math.ceil"><code>Math.ceil (x)</code></a></h4> <p> Returns the smallest integral value larger than or equal to <code>x</code>. @@ -2842,9 +2864,7 @@ -<p> -<hr><h3><a name="pdf-math.cos"><code>math.cos (x)</code></a></h3> - +<h4 heading><a name="Math.cos"><code>Math.cos (x)</code></a></h4> <p> Returns the cosine of <code>x</code> (assumed to be in radians). @@ -2852,9 +2872,7 @@ -<p> -<hr><h3><a name="pdf-math.deg"><code>math.deg (x)</code></a></h3> - +<h4 heading><a name="Math.deg"><code>Math.deg (x)</code></a></h4> <p> Converts the angle <code>x</code> from radians to degrees. @@ -2862,9 +2880,7 @@ -<p> -<hr><h3><a name="pdf-math.exp"><code>math.exp (x)</code></a></h3> - +<h4 heading><a name="Math.exp"><code>Math.exp (x)</code></a></h4> <p> Returns the value <em>e<sup>x</sup></em> @@ -2873,9 +2889,7 @@ -<p> -<hr><h3><a name="pdf-math.floor"><code>math.floor (x)</code></a></h3> - +<h4 heading><a name="Math.floor"><code>Math.floor (x)</code></a></h4> <p> Returns the largest integral value smaller than or equal to <code>x</code>. @@ -2883,31 +2897,24 @@ -<p> -<hr><h3><a name="pdf-math.fmod"><code>math.fmod (x, y)</code></a></h3> - +<h4 heading><a name="Math.fmod"><code>Math.fmod (x, y)</code></a></h4> <p> Returns the remainder of the division of <code>x</code> by <code>y</code> -that rounds the quotient towards zero. (integer/float) - - - - -<p> -<hr><h3><a name="pdf-math.huge"><code>math.huge</code></a></h3> - - -<p> -The float value <code>HUGE_VAL</code>, -a value larger than any other numerical value. - - - - -<p> -<hr><h3><a name="pdf-math.log"><code>math.log (x [, base])</code></a></h3> - +that rounds the quotient towards zero. + + + + +<h4 heading><a name="Math.huge"><code>Math.huge</code></a></h4> + +<p> +A value larger than any other numerical value. + + + + +<h4 heading><a name="Math.log"><code>Math.log (x [, base])</code></a></h4> <p> Returns the logarithm of <code>x</code> in the given base. @@ -2917,56 +2924,47 @@ -<p> -<hr><h3><a name="pdf-math.max"><code>math.max (x, ···)</code></a></h3> - +<h4 heading><a name="Math.max"><code>Math.max (x, ···)</code></a></h4> <p> Returns the argument with the maximum value, -according to the Lua operator <code><</code>. (integer/float) - - - - -<p> -<hr><h3><a name="pdf-math.maxinteger"><code>math.maxinteger</code></a></h3> +according to the Lua operator <code><</code>. + + + + +<h4 heading><a name="Math.max_integer"><code>Math.max_integer</code></a></h4> +<p> An integer with the maximum value for an integer. -<p> -<hr><h3><a name="pdf-math.min"><code>math.min (x, ···)</code></a></h3> - +<h4 heading><a name="Math.min"><code>Math.min (x, ···)</code></a></h4> <p> Returns the argument with the minimum value, -according to the Lua operator <code><</code>. (integer/float) - - - - -<p> -<hr><h3><a name="pdf-math.mininteger"><code>math.mininteger</code></a></h3> +according to the Lua operator <code><</code>. + + + + +<h4 heading><a name="Math.min_integer"><code>Math.min_integer</code></a></h4> +<p> An integer with the minimum value for an integer. -<p> -<hr><h3><a name="pdf-math.modf"><code>math.modf (x)</code></a></h3> - +<h4 heading><a name="Math.modf"><code>Math.modf (x)</code></a></h4> <p> Returns the integral part of <code>x</code> and the fractional part of <code>x</code>. -Its second result is always a float. - - - - -<p> -<hr><h3><a name="pdf-math.pi"><code>math.pi</code></a></h3> - + + + + +<h4 heading><a name="Math.pi"><code>Math.pi</code></a></h4> <p> The value of <em>π</em>. @@ -2974,9 +2972,7 @@ -<p> -<hr><h3><a name="pdf-math.rad"><code>math.rad (x)</code></a></h3> - +<h4 heading><a name="Math.rad"><code>Math.rad (x)</code></a></h4> <p> Converts the angle <code>x</code> from degrees to radians. @@ -2984,8 +2980,7 @@ -<p> -<hr><h3><a name="pdf-math.random"><code>math.random ([m [, n]])</code></a></h3> +<h4 heading><a name="Math.random"><code>Math.random ([m [, n])</code></a></h4> <p> @@ -2993,35 +2988,21 @@ returns a pseudo-random float with uniform distribution in the range <em>[0,1)</em>. When called with two integers <code>m</code> and <code>n</code>, -<code>math.random</code> returns a pseudo-random integer +<code>Math.random</code> returns a pseudo-random integer with uniform distribution in the range <em>[m, n]</em>. -(The value <em>m-n</em> cannot be negative and must fit in a Lua integer.) -The call <code>math.random(n)</code> is equivalent to <code>math.random(1,n)</code>. +(The value <em>m-n</em> cannot be negative and must fit in a Luan integer.) +The call <code>Math.random(n)</code> is equivalent to <code>Math.random(1,n)</code>. <p> This function is an interface to the underling -pseudo-random generator function provided by C. +pseudo-random generator function provided by Java. No guarantees can be given for its statistical properties. -<p> -<hr><h3><a name="pdf-math.randomseed"><code>math.randomseed (x)</code></a></h3> - - -<p> -Sets <code>x</code> as the "seed" -for the pseudo-random generator: -equal seeds produce equal sequences of numbers. - - - - -<p> -<hr><h3><a name="pdf-math.sin"><code>math.sin (x)</code></a></h3> - +<h4 heading><a name="Math.sin"><code>Math.sin (x)</code></a></h4> <p> Returns the sine of <code>x</code> (assumed to be in radians). @@ -3029,9 +3010,7 @@ -<p> -<hr><h3><a name="pdf-math.sqrt"><code>math.sqrt (x)</code></a></h3> - +<h4 heading><a name="Math.sqrt"><code>Math.sqrt (x)</code></a></h4> <p> Returns the square root of <code>x</code>. @@ -3040,9 +3019,7 @@ -<p> -<hr><h3><a name="pdf-math.tan"><code>math.tan (x)</code></a></h3> - +<h4 heading><a name="Math.tan"><code>Math.tan (x)</code></a></h4> <p> Returns the tangent of <code>x</code> (assumed to be in radians). @@ -3050,41 +3027,6 @@ -<p> -<hr><h3><a name="pdf-math.tointeger"><code>math.tointeger (x)</code></a></h3> - - -<p> -If the value <code>x</code> is convertible to an integer, -returns that integer. -Otherwise, returns <b>nil</b>. - - - - -<p> -<hr><h3><a name="pdf-math.type"><code>math.type (x)</code></a></h3> - - -<p> -Returns "<code>integer</code>" if <code>x</code> is an integer, -"<code>float</code>" if it is a float, -or <b>nil</b> if <code>x</code> is not a number. - - - - -<p> -<hr><h3><a name="pdf-math.ult"><code>math.ult (m, n)</code></a></h3> - - -<p> -Returns a boolean, -true if integer <code>m</code> is below integer <code>n</code> when -they are compared as unsigned integers. - - -