Pergi ke kandungan

Modul:Math/doc

Daripada Wikipedia, ensiklopedia bebas.
Semakan 4949014 pada 13:41, 26 November 2020 oleh Muhammad Roslee (bincang | sumb.) (order)

Ini ialah laman pendokumenan untuk Modul:Math

Modul ini menyediakan sejumlah fungsi matematik. Fungsi-fungsi ini boleh digunakan dari #invoke atau dari modul Lua yang lain.

Gunakan dari modul Lua yang lain

Untuk menggunakan modul dari halaman wiki biasa, tidak diperlukan persiapan khas. Sekiranya anda menggunakan modul dari modul Lua yang lain, pertama anda perlu memuatkannya, seperti ini:

local mm = require('Modul:Math')

(mm pembolehubah berdiri untuk Modul Math; anda boleh memilih sesuatu yang lebih deskriptif jika anda suka.)

Sebilangan besar fungsi dalam modul mempunyai versi untuk Lua dan versi untuk #invoke. Ia adalah mungkin untuk menggunakan fungsi-fungsi #invoke dari modul Lua lain, tetapi menggunakan fungsi Lua mempunyai kelebihan bahawa anda tidak perlu untuk mengakses Lua objek bingkai. Fungsi Lua didahului oleh _, sedangkan #invoke fungsinya tidak.

Random (Rawak)

{{#invoke:math|random}}
{{#invoke:math|random|max_value}}
{{#invoke:math|random|min_value|max_value}}
mm._random()
mm._random(max_value)
mm._random(min_value, max_value)

Menghasilkan nombor rawak.

  • Jika tiada argumen (alasan yang dikemukakan) yang ditentukan, jumlah yang dihasilkan adalah lebih besar daripada atau sama dengan 0 dan kurang daripada 1.
  • Jika satu argumen diberikan, nombor yang dihasilkan adalah bilangan bulat antara 1 dan argumen itu. Argumen mestilah integer (angka bulat) positif.
  • Jika dua argumen disediakan, jumlah yang dihasilkan adalah integer antara argumen yang pertama dan kedua. Kedua-dua argumen mestilah integer, tetapi boleh menjadi negatif.

Fungsi ini tidak akan berfungsi dengan betul untuk nombor yang kurang daripada −232 dan lebih besar daripada 232 − 1. Sekiranya anda perlu menggunakan nombor di luar julat ini, disarankan agar anda menggunakan Modul:Random.

susunan

{{#invoke:math|order|n}}
mm._order(n)

Menentukan peringkat magnitud daripada nombor.

precision

{{#invoke:math|precision|n}}
{{#invoke:math|precision|x=n}}
mm._precision(number_string)

Detemines the precision of a number. For example, for "4" it will return "0", for "4.567" it will return "3", and for "100" it will return "-2".

The function attempts to parse the string representation of the number, and detects whether the number uses E notation. For this reason, when called from Lua, very large numbers or very precise numbers should be directly input as strings to get accurate results. If they are input as numbers, the Lua interpreter will change them to E notation and this function will return the precision of the E notation rather than that of the original number. This is not a problem when the number is called from #invoke, as all input from #invoke is in string format.

max

{{#invoke:math|max|v1|v2|v3|...}}
mm._max(v1, v2, v3, ...)

Returns the maximum value from the values specified. Values that cannot be converted to numbers are ignored.

median

{{#invoke:math|median|v1|v2|v3|...}}
mm._median(v1, v2, v3, ...)

Returns the median value from the values specified. Values that cannot be converted to numbers are ignored.

min

{{#invoke:math|min|v1|v2|v3|...}}
mm._min(v1, v2, v3, ...)

Returns the minimum value from the values specified. Values that cannot be converted to numbers are ignored.

sum

{{#invoke:math|sum|v1|v2|v3|...}}
mm._sum(v1, v2, v3, ...)

Returns the sum of the values specified. Values that cannot be converted to numbers are ignored.

average

{{#invoke:math|average|v1|v2|v3|...}}
mm._average(v1, v2, v3, ...)

Returns the average of the values specified. (More precisely, the value returned is the arithmetic mean.) Values that cannot be converted to numbers are ignored.

round

{{#invoke:math|round|value|precision}}
{{#invoke:math|round|value=value|precision=precision}}
mm._round(value, precision)

Rounds a number to the specified precision.

Note: As of October 2019, there is a bug in the display of some rounded numbers. When trying to round a number that rounds to "n.0", like "1.02", to the nearest tenth of a digit (i.e. |r=1), this function should display "1.0", but it unexpectedly displays "1". Use the |precision_format= parameter instead.

log10

{{#invoke:math | log10 | x}}
mm._log10(x)

Returns log10(x), the logarithm of x using base 10.

mod

{{#invoke:math|mod|x|y}}
mm._mod(x, y)

Gets x modulo y, or the remainder after x has been divided by y. This is accurate for integers up to 253; for larger integers Lua's modulo operator may return an erroneous value. This function deals with this problem by returning 0 if the modulo given by Lua's modulo operator is less than 0 or greater than y.

gcd

{{#invoke:math|gcd|v1|v2|...}}
mm._gcd(v1, v2, ...)

Finds the greatest common divisor of the values specified. Values that cannot be converted to numbers are ignored.

precision_format

{{#invoke:math|precision_format|value_string|precision}}
mm._precision_format(value_string, precision)

Rounds a number to the specified precision and formats according to rules originally used for {{Rnd}}. Output is a string.

Parameter precision should be an integer. Negative values are permitted. Non-integers give unexpected result

  • 1,234.567
  • 2: 1,234.57
  • −2: 1,200
  • 2.5: 1,234.5658476404
  • −2.5: 1,264.9110640674

cleanNumber

local number, number_string = mm._cleanNumber(number_string)

A helper function that can be called from other Lua modules, but not from #invoke. This takes a string or a number value as input, and if the value can be converted to a number, cleanNumber returns the number and the number string. If the value cannot be converted to a number, cleanNumber returns nil, nil.

See also

Templat:Math templates