Библиотека, содержащая разные математические функции.
код
library MathLib
globals
constant real Ln2 = 0.693147180
constant real Ln10 = 2.302585092
endglobals
function SignInt takes integer i returns integer
/*
Sign function for integers.
Returns 1 if i is positive, -1 if negative and 0 if zero (ISignBJ returns 1 for zero).
*/
if i < 0 then
return -1
elseif i > 0 then
return 1
endif
return 0
endfunction
function SignReal takes real r returns integer
/*
Sign function for reals.
Returns 1 if r is positive, -1 if negative and 0 if zero (RSignBJ returns 1 for zero).
*/
if r < 0. then
return -1
elseif r > 0. then
return 1
endif
return 0
endfunction
function AbsInt takes integer i returns integer
/*
Returns the absolute value of an integer. Same as IAbsBJ.
*/
if i < 0 then
return - i
endif
return i
endfunction
function AbsReal takes real r returns real
/*
Returns the absolute value of a real. Same as RAbsBJ.
*/
if r < 0. then
return - r
endif
return r
endfunction
function MaxInt takes integer i1, integer i2 returns integer
/*
Returns the maximum of two integers. Same as IMaxBJ.
*/
if i1 > i2 then
return i1
endif
return i2
endfunction
function MaxReal takes real r1, real r2 returns real
/*
Returns the maximum of two reals. Same as RMaxBJ.
*/
if r1 > r2 then
return r1
endif
return r2
endfunction
function MinInt takes integer i1, integer i2 returns integer
/*
Returns the minimum of two integers. Same as IMinBJ.
*/
if i1 < i2 then
return i1
endif
return i2
endfunction
function MinReal takes real r1, real r2 returns real
/*
Returns the minimum of two reals. Same as RMinBJ.
*/
if r1 < r2 then
return r1
endif
return r2
endfunction
function IsEven takes integer i returns boolean
/*
Returns true if an integer is even and false otherwise.
*/
return i / 2 * 2 == i
endfunction
function IsOdd takes integer i returns boolean
/*
Returns true if an integer is odd and false otherwise.
*/
return i / 2 * 2 != i
endfunction
function IsDivisibleByN takes integer i, integer n returns boolean
/*
Returns true if an integer is divisible by n and false otherwise.
*/
return i / n * n == i
endfunction
function IsNotDivisibleByN takes integer i, integer n returns boolean
/*
Returns true if an integer is NOT divisible by n and false otherwise.
*/
return i / n * n != i
endfunction
function RemainderN takes integer i, integer n returns integer
/*
Returns the remainder ater dividing an integer by n.
*/
return i - i / n * n
endfunction
function ModuloN takes integer i, integer n returns integer
/*
Evaluates i mod n.
Examples:
ModuloN(5, 3) = 2
ModuloN(5, -3) = -1
ModuloN(-5, 3) = 1
ModuloN(-5, -3) = -2
ModuloN(2, 3) = 2
ModuloN(2, -3) = -1
ModuloN(-2, 3) = 1
ModuloN(-2, -3) = -2
ModuloN(0, 3) = 0
ModuloN(0, -3) = 0
*/
local integer r = RemainderN(i, n)
if (r > 0 and n < 0) or (r < 0 and n > 0) then
return r + n
endif
return r
endfunction
function Floor takes real r returns integer
/*
Returns the floor of r: the greatest integer less than or equal to r.
*/
local integer i = R2I(r)
/*
Equality operator '==' rounds real numbers,
use '!=' and negation for precise comparison.
*/
if r > 0. or not (i != r) then
return i
endif
return i - 1
endfunction
function Ceil takes real r returns integer
/*
Returns the ceil of r: the least integer greater than or equal to r.
*/
local integer i = R2I(r)
/*
Equality operator '==' rounds real numbers,
use '!=' and negation for precise comparison.
*/
if r < 0. or not (i != r) then
return i
endif
return i + 1
endfunction
function R2Idown takes real r returns integer
/*
Rounds a real down.
Can round up if the real is thousandths apart from being a whole number.
*/
local integer i = Floor(r)
if r - i == 1. then
return i + 1
endif
return i
endfunction
function R2Iup takes real r returns integer
/*
Rounds a real up.
Can round down if the real is thousandths apart from being a whole number.
*/
local integer i = Floor(r)
if r == i then
return i
endif
return i + 1
endfunction
function R2Ieven takes real r returns integer
/*
Rounds a real using banker's rounding.
*/
local integer i = Floor(r)
local real mid = i + 0.5
if (r == mid and IsEven(i)) or r < mid then
return i
endif
return i + 1
endfunction
function B2I takes boolean b returns integer
/*
Returns 1 if boolean is true and 0 otherwise.
*/
if b then
return 1
endif
return 0
endfunction
function B2Ineg takes boolean b returns integer
/*
Returns 1 if boolean is true and -1 otherwise.
*/
if b then
return 1
endif
return -1
endfunction
function I2B takes integer i returns boolean
/*
Returns false if i is zero and true otherwise.
*/
return i != 0
endfunction
function PowerN takes integer i, integer n returns integer
/*
Raises an integer to nth power.
Returns 0 if n is negative and the absolute value of the integer is greater than 1.
*/
local integer c = i
if i == -1 then
if IsEven(n) then
return 1
endif
return -1
elseif i == 1 or n == 0 then
return 1
elseif i == 0 or n < 0 then
return 0
endif
loop
exitwhen n == 1
set c = c * i
set n = n - 1
endloop
return c
endfunction
function Ln takes real r returns real
/*
Returns the natural logarithm of a real.
Hangs if the real is not positive.
*/
local real power_ln2 = 0.
// Hang if r is not positive
if r <= 0. then
call I2R(1 / 0)
endif
/*
Move r to interval [1, 2)
ln(r) = ln(2 ^ power * v)
= ln(2 ^ power) + ln(v)
= power * ln2 + ln(v)
*/
loop
exitwhen r >= 1.
set r = r * 2.
set power_ln2 = power_ln2 - Ln2
endloop
loop
exitwhen r < 2.
set r = r * 0.5
set power_ln2 = power_ln2 + Ln2
endloop
// Source: https://www.hiveworkshop.com/threads/snippet-natural-logarithm.108059
return power_ln2 + (r - 1.) * (1. + 9. / (2. + r) + 9. / (1. + r * 2.) + 1. / r) / 8.
endfunction
function Log2 takes real r returns real
/*
Returns the logarithm base 2 of a real.
Hangs if the real is not positive.
*/
return Ln(r) / Ln2
endfunction
function Log10 takes real r returns real
/*
Returns the logarithm base 10 of a real.
Hangs if the real is not positive.
*/
return Ln(r) / Ln10
endfunction
function Log takes real r, real base returns real
/*
Returns the logarithm of a real given the base.
Hangs if the real or the base are not positive.
*/
return Ln(r) / Ln(base)
endfunction
endlibrary
Установка
- Скачать .j файл и переместить в Директория-JNGP/jass.
- В коде карты прописать импорт библиотеки.
//! import "MathLibrary.j"
Обновления
Обновление 06.12.2024
- Точность констант Ln2 и Ln10 увеличена до 9 знаков после запятой.
- К каждой функции добавлена документация.
- Функция ModuloN теперь возвращает модуль по n, а не остаток от деления на n.
- Добавлена функция RemainderN, которая возвращает остаток от деления на n.
- Функция ModuloNPos удалена. Используйте идентичную функцию ModuloInteger из blizzard.j.
- Функция PowerN больше не возвращает 0 для чисел 1 и -1, если степень отрицательна.
- Немного увеличена точность результата функции Ln.
Обновление 16.07.2021
Исправлена функция ModuloNPos.
Ред. PT153
Ред. Clamp
Экономишь итерации при схождении ряда и теряешь в точности, а экономии там до 3х проверок и до 3х сложений максимум, смех один, а не экономия.
Ред. PT153