104 lines
3.2 KiB
Ucode
104 lines
3.2 KiB
Ucode
/**
|
|
* Author: dkanus
|
|
* Home repo: https://www.insultplayers.ru/git/AcediaFramework/AcediaCore
|
|
* License: GPL
|
|
* Copyright 2020-2023 Anton Tarasenko
|
|
*------------------------------------------------------------------------------
|
|
* This file is part of Acedia.
|
|
*
|
|
* Acedia is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation, version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* Acedia is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
*/
|
|
class MathApi extends AcediaObject;
|
|
|
|
//! API for basic math methods and [`BigInt`] creation.
|
|
|
|
/// For storing result of integer division.
|
|
///
|
|
/// If we divide `number` by `divisor`, then `number = divisor/// quotient + remainder`.
|
|
struct IntegerDivisionResult
|
|
{
|
|
var int quotient;
|
|
var int remainder;
|
|
};
|
|
|
|
/// Converts given [`int`] value into [`BigInt`] value..
|
|
public function BigInt ToBigInt(int value)
|
|
{
|
|
local BigInt result;
|
|
|
|
result = BigInt(_.memory.Allocate(class'BigInt'));
|
|
result.SetInt(value);
|
|
return result;
|
|
}
|
|
|
|
|
|
/// Creates new `BigInt` value, based on the decimal number representation.
|
|
///
|
|
/// If (and only if) `none` or invalid decimal representation (digits only, possibly with
|
|
/// leading sign) is given as an argument, method will return `none`.
|
|
public function BigInt MakeBigInt(BaseText value)
|
|
{
|
|
local BigInt result;
|
|
|
|
result = BigInt(_.memory.Allocate(class'BigInt'));
|
|
if (result.SetDecimal(value)) {
|
|
return result;
|
|
}
|
|
result.FreeSelf();
|
|
return none;
|
|
}
|
|
|
|
/// Creates new `BigInt` value, based on the decimal number representation.
|
|
///
|
|
/// If (and only if) invalid decimal representation (digits only, possibly with leading sign) is
|
|
/// given as an argument, method will return `none`.
|
|
public function BigInt MakeBigInt_S(string value)
|
|
{
|
|
local BigInt result;
|
|
|
|
result = BigInt(_.memory.Allocate(class'BigInt'));
|
|
if (result.SetDecimal_S(value)) {
|
|
return result;
|
|
}
|
|
result.FreeSelf();
|
|
return none;
|
|
}
|
|
|
|
/// Computes remainder of the integer division of [`number`] by [`divisor`].
|
|
///
|
|
/// This method is necessary as a replacement for `%` module operator, since it is an operation on
|
|
/// `float`s in UnrealScript and does not have appropriate value range to work with big integer
|
|
// values.
|
|
public function int Remainder(int number, int divisor)
|
|
{
|
|
local int quotient;
|
|
|
|
quotient = number / divisor;
|
|
return (number - quotient * divisor);
|
|
}
|
|
|
|
/// Computes quotient and remainder of the integer division of [`number`] by [`divisor`].
|
|
///
|
|
/// See `MathApi::Remainder()` method if you only need remainder.
|
|
public function IntegerDivisionResult IntegerDivision(int number, int divisor)
|
|
{
|
|
local IntegerDivisionResult result;
|
|
|
|
result.quotient = number / divisor;
|
|
result.remainder = (number - result.quotient * divisor);
|
|
return result;
|
|
}
|
|
|
|
defaultproperties
|
|
{
|
|
} |