Skip to main content

lua_std/
math.rs

1//! The mathematical library.
2//!
3//! Provides basic mathematical functions and the interface to the
4//! integer/float subtypes of [`number`]. Functions annotated
5//! "integer/float" give integer results for integer arguments and float
6//! results otherwise.
7use crate::{boolean, number, option, string, Repeatable};
8
9/// A value larger than any other numerical value: the float infinity,
10/// `HUGE_VAL`.
11///
12/// # Examples
13///
14/// ```lua
15/// print(math.huge)      --> inf
16/// print(-math.huge)     --> -inf
17/// print(1 / 0)          --> inf
18/// ```
19///
20/// # See also
21///
22/// [`maxinteger`], [`mininteger`]
23pub const huge: number = number;
24
25/// An integer with the maximum value for an integer:
26/// 9223372036854775807 (2⁶³ − 1).
27///
28/// # Examples
29///
30/// ```lua
31/// print(math.maxinteger)      --> 9223372036854775807
32/// print(math.maxinteger + 1 == math.mininteger)  --> true (wraps around)
33/// ```
34///
35/// # See also
36///
37/// [`mininteger`], [`huge`]
38pub const maxinteger: number = number;
39
40/// An integer with the minimum value for an integer:
41/// −9223372036854775808 (−2⁶³).
42///
43/// # Examples
44///
45/// ```lua
46/// print(math.mininteger)      --> -9223372036854775808
47/// ```
48///
49/// # See also
50///
51/// [`maxinteger`], [`huge`]
52pub const mininteger: number = number;
53
54/// The value of π.
55///
56/// # Examples
57///
58/// ```lua
59/// print(math.pi)              --> 3.141592653589793
60/// print(math.deg(math.pi))    --> 180.0
61/// ```
62///
63/// # See also
64///
65/// [`deg`], [`rad`]
66pub const pi: number = number;
67
68/// Returns the absolute value of a number (integer/float).
69///
70/// # Parameters
71///
72/// - **`x`**: a number.
73///
74/// # Return value
75///
76/// The maximum value between `x` and −`x`. The subtype of the result
77/// matches the subtype of the argument.
78///
79/// # Examples
80///
81/// ```lua
82/// print(math.abs(-5))      --> 5
83/// print(math.abs(-5.25))   --> 5.25
84/// print(math.abs(7))       --> 7
85/// ```
86pub fn abs(x: number) -> number {
87    todo!()
88}
89
90/// Returns the arc cosine of a number, in radians.
91///
92/// # Parameters
93///
94/// - **`x`**: a number in the range \[−1, 1].
95///
96/// # Return value
97///
98/// The arc cosine of `x`, in radians, as a float in the range
99/// \[0, π]. Arguments outside \[−1, 1] produce NaN.
100///
101/// # Examples
102///
103/// ```lua
104/// print(math.acos(1))     --> 0.0
105/// print(math.acos(0))     --> 1.5707963267948966
106/// print(math.acos(-1))    --> 3.141592653589793
107/// ```
108///
109/// # See also
110///
111/// [`asin`], [`atan`], [`cos`]
112pub fn acos(x: number) -> number {
113    todo!()
114}
115
116/// Returns the arc sine of a number, in radians.
117///
118/// # Parameters
119///
120/// - **`x`**: a number in the range \[−1, 1].
121///
122/// # Return value
123///
124/// The arc sine of `x`, in radians, as a float in the range
125/// \[−π/2, π/2]. Arguments outside \[−1, 1] produce NaN.
126///
127/// # Examples
128///
129/// ```lua
130/// print(math.asin(0))     --> 0.0
131/// print(math.asin(1))     --> 1.5707963267948966
132/// ```
133///
134/// # See also
135///
136/// [`acos`], [`atan`], [`sin`]
137pub fn asin(x: number) -> number {
138    todo!()
139}
140
141/// Returns the arc tangent of `y/x`, in radians.
142///
143/// # Parameters
144///
145/// - **`y`**: a number.
146/// - **`x`**: a number (default 1).
147///
148/// # Return value
149///
150/// The arc tangent of `y/x`, in radians, as a float. The signs of
151/// both arguments are used to find the quadrant of the result, and
152/// the function handles the case of `x` being zero correctly.
153///
154/// # Examples
155///
156/// ```lua
157/// print(math.atan(1))        --> 0.7853981633974483  (π/4)
158/// print(math.atan(1, 0))     --> 1.5707963267948966  (π/2)
159/// print(math.atan(-1, -1))   --> -2.35619449019234545  (third quadrant)
160/// ```
161///
162/// # See also
163///
164/// [`acos`], [`asin`], [`tan`]
165pub fn atan(y: number, x: option<number>) -> number {
166    todo!()
167}
168
169/// Rounds a number up to the nearest integer.
170///
171/// # Parameters
172///
173/// - **`x`**: a number.
174///
175/// # Return value
176///
177/// The smallest integral value greater than or equal to `x`, as an
178/// integer. Raises an error if the result does not fit in an integer.
179///
180/// # Examples
181///
182/// ```lua
183/// print(math.ceil(4.2))    --> 5
184/// print(math.ceil(-4.2))   --> -4
185/// print(math.ceil(7))      --> 7
186/// ```
187///
188/// # See also
189///
190/// [`floor`], [`modf`]
191pub fn ceil(x: number) -> number {
192    todo!()
193}
194
195/// Returns the cosine of a number given in radians.
196///
197/// # Parameters
198///
199/// - **`x`**: an angle in radians.
200///
201/// # Return value
202///
203/// The cosine of `x`, as a float in the range \[−1, 1].
204///
205/// # Examples
206///
207/// ```lua
208/// print(math.cos(0))          --> 1.0
209/// print(math.cos(math.pi))    --> -1.0
210/// ```
211///
212/// # See also
213///
214/// [`sin`], [`tan`], [`acos`], [`rad`]
215pub fn cos(x: number) -> number {
216    todo!()
217}
218
219/// Converts an angle from radians to degrees.
220///
221/// # Parameters
222///
223/// - **`x`**: an angle in radians.
224///
225/// # Return value
226///
227/// The angle `x` expressed in degrees, as a float.
228///
229/// # Examples
230///
231/// ```lua
232/// print(math.deg(math.pi))       --> 180.0
233/// print(math.deg(math.pi / 2))   --> 90.0
234/// ```
235///
236/// # See also
237///
238/// [`rad`], [`pi`]
239pub fn deg(x: number) -> number {
240    todo!()
241}
242
243/// Returns *e* raised to a power.
244///
245/// # Parameters
246///
247/// - **`x`**: a number.
248///
249/// # Return value
250///
251/// The value *eˣ*, as a float, where *e* is the base of natural
252/// logarithms.
253///
254/// # Examples
255///
256/// ```lua
257/// print(math.exp(0))    --> 1.0
258/// print(math.exp(1))    --> 2.718281828459045
259/// ```
260///
261/// # See also
262///
263/// [`log`]
264pub fn exp(x: number) -> number {
265    todo!()
266}
267
268/// Rounds a number down to the nearest integer.
269///
270/// # Parameters
271///
272/// - **`x`**: a number.
273///
274/// # Return value
275///
276/// The largest integral value less than or equal to `x`, as an
277/// integer. Raises an error if the result does not fit in an integer.
278///
279/// # Examples
280///
281/// ```lua
282/// print(math.floor(4.7))    --> 4
283/// print(math.floor(-4.7))   --> -5
284/// print(math.floor(7))      --> 7
285/// ```
286///
287/// # See also
288///
289/// [`ceil`], [`modf`], [`tointeger`]
290pub fn floor(x: number) -> number {
291    todo!()
292}
293
294/// Splits a number into a normalized fraction and an exponent.
295/// **Re-added in Lua 5.5** (it had been removed in 5.3).
296///
297/// # Parameters
298///
299/// - **`x`**: a number.
300///
301/// # Return value
302///
303/// Two values `m` and `e` such that `x == m * 2^e`, where `e` is an
304/// integer and the absolute value of `m` is in the range [0.5, 1) —
305/// or zero when `x` is zero.
306///
307/// # Examples
308///
309/// ```lua
310/// print(math.frexp(8))      --> 0.5   4
311/// print(math.frexp(1))      --> 0.5   1
312/// print(math.frexp(0))      --> 0.0   0
313/// ```
314///
315/// # See also
316///
317/// [`ldexp`]
318pub fn frexp(x: number) -> (number, number) {
319    todo!()
320}
321
322/// Builds a float from a mantissa and an exponent.
323/// **Re-added in Lua 5.5** (it had been removed in 5.3).
324///
325/// # Parameters
326///
327/// - **`m`**: the mantissa.
328/// - **`e`**: an integer exponent.
329///
330/// # Return value
331///
332/// The float `m * 2^e`.
333///
334/// # Examples
335///
336/// ```lua
337/// print(math.ldexp(0.5, 4))    --> 8.0
338/// print(math.ldexp(1, 10))     --> 1024.0
339/// ```
340///
341/// # See also
342///
343/// [`frexp`]
344pub fn ldexp(m: number, e: number) -> number {
345    todo!()
346}
347
348/// Returns the remainder of a division, rounding the quotient towards
349/// zero (integer/float).
350///
351/// # Parameters
352///
353/// - **`x`**: the dividend.
354/// - **`y`**: the divisor.
355///
356/// # Return value
357///
358/// The remainder of `x / y` with the sign of `x`. Note that the `%`
359/// operator behaves differently: it rounds the quotient towards minus
360/// infinity, so the result takes the sign of the divisor.
361///
362/// # Examples
363///
364/// ```lua
365/// print(math.fmod(7, 3))     --> 1
366/// print(math.fmod(-7, 3))    --> -1
367/// print(-7 % 3)              --> 2   (the operator rounds differently)
368/// ```
369pub fn fmod(x: number, y: number) -> number {
370    todo!()
371}
372
373/// Returns the logarithm of a number in a given base.
374///
375/// # Parameters
376///
377/// - **`x`**: a positive number.
378/// - **`base`**: the logarithm base (default *e*).
379///
380/// # Return value
381///
382/// The logarithm of `x` in the given base, as a float. Without
383/// `base`, returns the natural logarithm of `x`.
384///
385/// # Examples
386///
387/// ```lua
388/// print(math.log(math.exp(1)))   --> 1.0
389/// print(math.log(8, 2))          --> 3.0
390/// print(math.log(100, 10))       --> 2.0
391/// ```
392///
393/// # See also
394///
395/// [`exp`]
396pub fn log(x: number, base: option<number>) -> number {
397    todo!()
398}
399
400/// Returns the largest of its arguments.
401///
402/// # Parameters
403///
404/// - **`x, ···`**: one or more numbers.
405///
406/// # Return value
407///
408/// The argument with the maximum value, according to the `<`
409/// operator.
410///
411/// # Examples
412///
413/// ```lua
414/// print(math.max(1, 5, 3))     --> 5
415/// print(math.max(-1, -5))      --> -1
416/// ```
417///
418/// # See also
419///
420/// [`min`]
421pub fn max(values: Repeatable<number>) -> number {
422    todo!()
423}
424
425/// Returns the smallest of its arguments.
426///
427/// # Parameters
428///
429/// - **`x, ···`**: one or more numbers.
430///
431/// # Return value
432///
433/// The argument with the minimum value, according to the `<`
434/// operator.
435///
436/// # Examples
437///
438/// ```lua
439/// print(math.min(1, 5, 3))     --> 1
440/// print(math.min(-1, -5))      --> -5
441/// ```
442///
443/// # See also
444///
445/// [`max`]
446pub fn min(values: Repeatable<number>) -> number {
447    todo!()
448}
449
450/// Splits a number into its integral and fractional parts.
451///
452/// # Parameters
453///
454/// - **`x`**: a number.
455///
456/// # Return value
457///
458/// Two values: the integral part of `x` and the fractional part of
459/// `x`. Its second result is always a float.
460///
461/// # Examples
462///
463/// ```lua
464/// print(math.modf(3.7))     --> 3.0   0.7000000000000002
465/// print(math.modf(-3.7))    --> -3.0  -0.7000000000000002
466/// ```
467///
468/// # See also
469///
470/// [`floor`], [`ceil`], [`fmod`]
471pub fn modf(x: number) -> (number, number) {
472    todo!()
473}
474
475/// Converts an angle from degrees to radians.
476///
477/// # Parameters
478///
479/// - **`x`**: an angle in degrees.
480///
481/// # Return value
482///
483/// The angle `x` expressed in radians, as a float.
484///
485/// # Examples
486///
487/// ```lua
488/// print(math.rad(180))    --> 3.141592653589793
489/// print(math.rad(90))     --> 1.5707963267948966
490/// ```
491///
492/// # See also
493///
494/// [`deg`], [`pi`]
495pub fn rad(x: number) -> number {
496    todo!()
497}
498
499/// Returns a pseudo-random number.
500///
501/// # Parameters
502///
503/// - **`m`**: an integer, the lower bound (or the upper bound of the
504///   range \[1, m], when `n` is absent).
505/// - **`n`**: an integer, the upper bound.
506///
507/// # Return value
508///
509/// With no arguments, a pseudo-random float with uniform distribution
510/// in the range \[0, 1). With one or two integer arguments, a
511/// pseudo-random integer with uniform distribution in the range
512/// \[1, m] or \[m, n], respectively.
513///
514/// # Examples
515///
516/// ```lua
517/// print(math.random())        --> 0.2896938390679077  (some float in [0, 1))
518/// print(math.random(6))       --> 4                 (a die roll: 1..6)
519/// print(math.random(10, 20))  --> 17                (an integer in 10..20)
520/// ```
521///
522/// # See also
523///
524/// [`randomseed`]
525pub fn random(m: option<number>, n: option<number>) -> number {
526    todo!()
527}
528
529/// Initialises the pseudo-random number generator.
530///
531/// # Parameters
532///
533/// - **`x`**: an integer used as seed.
534/// - **`y`**: an integer used as a second seed component.
535///
536/// # Return value
537///
538/// The two integer components of the seed that was set, so they can
539/// be reused later to repeat the sequence.
540///
541/// # Description
542///
543/// Equal seeds produce equal sequences of numbers. When called with
544/// no arguments, Lua generates a seed with a weak attempt for
545/// randomness. (Since Lua the generator is seeded automatically at
546/// startup, so calling this function is only needed to reproduce a
547/// sequence.)
548///
549/// # Examples
550///
551/// ```lua
552/// math.randomseed(42)
553/// print(math.random(100))   --> always the same value for seed 42
554/// ```
555///
556/// # See also
557///
558/// [`random`]
559pub fn randomseed(x: option<number>, y: option<number>) -> (number, number) {
560    todo!()
561}
562
563/// Returns the sine of a number given in radians.
564///
565/// # Parameters
566///
567/// - **`x`**: an angle in radians.
568///
569/// # Return value
570///
571/// The sine of `x`, as a float in the range \[−1, 1].
572///
573/// # Examples
574///
575/// ```lua
576/// print(math.sin(0))              --> 0.0
577/// print(math.sin(math.pi / 2))    --> 1.0
578/// ```
579///
580/// # See also
581///
582/// [`cos`], [`tan`], [`asin`], [`rad`]
583pub fn sin(x: number) -> number {
584    todo!()
585}
586
587/// Returns the square root of a number.
588///
589/// # Parameters
590///
591/// - **`x`**: a number greater than or equal to 0.
592///
593/// # Return value
594///
595/// The square root of `x`, as a nonnegative float. If `x` is less
596/// than 0, the result is NaN.
597///
598/// # Description
599///
600/// You can also use the expression `x^0.5` to compute this value.
601/// Like all Lua functions, `sqrt` lives in a table (the `math`
602/// library), so it is called as `math.sqrt(x)`.
603///
604/// # Examples
605///
606/// ```lua
607/// print(math.sqrt(0))          --> 0.0
608/// print(math.sqrt(1))          --> 1.0
609/// print(math.sqrt(2))          --> 1.4142135623730951
610/// print(math.sqrt(9))          --> 3.0
611/// print(math.sqrt(16))         --> 4.0
612/// print(math.sqrt(-1))         --> nan
613/// print(math.sqrt(math.huge))  --> inf
614/// ```
615///
616/// # See also
617///
618/// [`exp`], [`log`]
619pub fn sqrt(x: number) -> number {
620    todo!()
621}
622
623/// Returns the tangent of a number given in radians.
624///
625/// # Parameters
626///
627/// - **`x`**: an angle in radians.
628///
629/// # Return value
630///
631/// The tangent of `x`, as a float.
632///
633/// # Examples
634///
635/// ```lua
636/// print(math.tan(0))              --> 0.0
637/// print(math.tan(math.pi / 4))    --> 0.9999999999999999  (float rounding)
638/// ```
639///
640/// # See also
641///
642/// [`sin`], [`cos`], [`atan`]
643pub fn tan(x: number) -> number {
644    todo!()
645}
646
647/// Converts a value to an integer, if possible.
648///
649/// # Parameters
650///
651/// - **`x`**: any value.
652///
653/// # Return value
654///
655/// If `x` is convertible to an integer (an integer, a float with an
656/// integral value that fits, or a string that reads as one), returns
657/// that integer; otherwise, returns `nil`.
658///
659/// # Examples
660///
661/// ```lua
662/// print(math.tointeger(3.0))    --> 3
663/// print(math.tointeger(3.5))    --> nil
664/// print(math.tointeger("42"))   --> 42
665/// ```
666///
667/// # See also
668///
669/// [`floor`], `math.type`
670pub fn tointeger(x: number) -> option<number> {
671    todo!()
672}
673
674/// Reveals the subtype of a number.
675///
676/// # Parameters
677///
678/// - **`x`**: any value.
679///
680/// # Return value
681///
682/// The string `"integer"` if `x` is an integer, `"float"` if it is a
683/// float, or `nil` if `x` is not a number.
684///
685/// # Examples
686///
687/// ```lua
688/// print(math.type(3))      --> integer
689/// print(math.type(3.0))    --> float
690/// print(math.type("3"))    --> nil
691/// ```
692///
693/// # See also
694///
695/// [`tointeger`], [`type`](crate::type())
696pub fn r#type(x: number) -> option<string> {
697    todo!()
698}
699
700/// Compares two integers as unsigned values.
701///
702/// # Parameters
703///
704/// - **`m`**: an integer.
705/// - **`n`**: an integer.
706///
707/// # Return value
708///
709/// A boolean: true if and only if `m` is below `n` when they are
710/// compared as unsigned integers.
711///
712/// # Examples
713///
714/// ```lua
715/// print(math.ult(1, 2))     --> true
716/// print(math.ult(-1, 2))    --> false  (-1 is huge as unsigned)
717/// ```
718pub fn ult(m: number, n: number) -> boolean {
719    todo!()
720}
721
722/// Rounds a number to the nearest integer, halves away from zero.
723/// **Extended library — feature `extended`** (from `extended.lua`).
724///
725/// # Parameters
726///
727/// - **`ndigits`**: round to this many decimal places instead; the
728///   result is then a float.
729///
730/// # Examples
731///
732/// ```lua
733/// print(math.round(2.5))          --> 3
734/// print(math.round(-2.5))         --> -3
735/// print(math.round(2.4))          --> 2
736/// print(math.round(3.14159, 2))   --> 3.14
737/// ```
738///
739/// # See also
740///
741/// [`floor`], [`ceil`]
742#[cfg(feature = "extended")]
743pub fn round(x: number, ndigits: option<number>) -> number {
744    todo!()
745}
746
747/// Restricts a number to a range.
748/// **Extended library — feature `extended`** (from `extended.lua`).
749/// From Rust `f64::clamp`.
750///
751/// # Examples
752///
753/// ```lua
754/// print(math.clamp(5, 1, 3))     --> 3
755/// print(math.clamp(-5, 1, 3))    --> 1
756/// print(math.clamp(2, 1, 3))     --> 2
757/// ```
758///
759/// # See also
760///
761/// [`min`], [`max`]
762#[cfg(feature = "extended")]
763pub fn clamp(x: number, min: number, max: number) -> number {
764    todo!()
765}
766
767/// Returns the sign of a number: 1, −1 or 0. NaN is returned
768/// unchanged.
769/// **Extended library — feature `extended`** (from `extended.lua`).
770/// From Rust `f64::signum`.
771///
772/// # Examples
773///
774/// ```lua
775/// print(math.sign(9))       --> 1
776/// print(math.sign(-0.5))    --> -1
777/// print(math.sign(0))       --> 0
778/// ```
779///
780/// # See also
781///
782/// [`abs`]
783#[cfg(feature = "extended")]
784pub fn sign(x: number) -> number {
785    todo!()
786}