aza/math

AzaMath - Anizoptera CMF 数学组件。任意精度算术(用于大整数;BCMath 封装)和位值数制之间的通用转换器(支持从2到62的基数,以及自定义字母表系统;纯PHP实现,可以使用GMP和核心PHP函数进行速度优化)。

v1.0.3 2013-05-28 13:04 UTC

This package is not auto-updated.

Last update: 2024-09-14 13:28:29 UTC


README

Anizoptera CMF 数学组件。

https://github.com/Anizoptera/Math

Build Status

目录

  1. 简介
  2. 要求
  3. 安装
  4. 示例
  5. 测试
  6. 鸣谢
  7. 许可协议
  8. 链接

简介

提供使用任意精度算术(使用BCMath)处理大数(整数、浮点数)的功能,以及位值数制之间的通用转换器(支持从2到62的基数,以及自定义系统;纯PHP实现,但可以使用GMP和核心PHP函数进行速度优化)。

功能

  • 处理大数(整数、浮点数)的任意精度功能(需要BCMath)。可以处理浮点数(包括E表示法)且不会丢失精度(尽可能)。它支持所有基本算术运算、指数运算、平方根、取模、位移、四舍五入、比较以及一些其他操作。
  • 非常简单的大整数算术(仅限于整数!具有原生PHP实现,并可以使用BCMath或GMP进行加速)。
  • 通用数(和超大数!)位值数制转换器(支持从2到62的基数,以及自定义字母表系统;纯PHP实现,但可以使用GMP核心PHP函数进行速度优化)。支持负数和大整数。
  • 方便、全面文档化且经过测试的API。

要求

安装

推荐通过Composer安装AzaMath。您可以在Packagist上查看软件包信息

{
    "require": {
        "aza/math": "~1.0"
    }
}

示例

您可以使用examples/example.php运行所有示例。

示例 #1 - 数制转换

$res = NumeralSystem::convert('WIKIPEDIA', 36, 10);
echo $res . PHP_EOL; // 91730738691298

$res = NumeralSystem::convert('9173073869129891730738691298', 10, 16);
echo $res . PHP_EOL; // 1da3c9f2dd3133d4ed04bce2

$res = NumeralSystem::convertTo('9173073869129891730738691298', 62);
echo $res . PHP_EOL; // BvepB3yk4UBFhGew

$res = NumeralSystem::convertFrom('BvepB3yk4UBFhGew', 62);
echo $res . PHP_EOL; // 9173073869129891730738691298

示例 #2 - 自定义数制

// Add new system with custom alphabet
// Each char must appear only once.
// It should use only one byte characters.
$alphabet = '!@#$%^&*()_+=-'; // base 14 equivalent
$system   = 'StrangeSystem';
NumeralSystem::setSystem($system, $alphabet);

$number = '9999';
$res = NumeralSystem::convertTo($number, $system);
echo $res . PHP_EOL; // $)!$

$res = NumeralSystem::convertFrom($res, $system);
echo $res . PHP_EOL; // 9999


// Full binary alphabet
$system = 'binary';
NumeralSystem::setSystem($system, $alphabet);
// Examples with it
$var = 'example';
$expected_hex = ltrim(sha1($var), '0'); // sha1 can return hex value padded with zeros
$expected_bin = sha1($var, true);       // raw sha1 hash (binary representation)
$result_hex   = NumeralSystem::convert($expected_bin, $system, 16);
$result_bin   = NumeralSystem::convert($expected_hex, 16, $system);
echo $expected_hex . PHP_EOL; // c3499c2729730a7f807efb8676a92dcb6f8a3f8f
echo $result_hex . PHP_EOL;   // c3499c2729730a7f807efb8676a92dcb6f8a3f8f
echo ($expected_bin === $result_bin) . PHP_EOL; // 1

示例 #3 - 任意精度算术

// Create new big number with the specified precision for operations - 20 (default is 100)
$number = new BigNumber('118059162071741130342591466421', 20);

// Divide number
$number->divide(12345678910);
echo $number . PHP_EOL; // 9562792207086578954.49764831288650451382

// Divide again and round with the specified precision and algorithm
// Three round algorithms a supported: HALF_UP, HALF_DOWN, CUT.
// You can use them as BigNumber::ROUND_* or PHP_ROUND_HALF_UP, PHP_ROUND_HALF_DOWN.
// Default is HALF_UP.
$number->divide(9876543210)->round(3, PHP_ROUND_HALF_DOWN);
echo $number . PHP_EOL; // 968232710.955

// Comparisions
$number = new BigNumber(10);
echo ($number->compareTo(20) < 0) . PHP_EOL; // 1
echo $number->isLessThan(20) . PHP_EOL; // 1

$number = new BigNumber(20);
echo ($number->compareTo(10) > 0) . PHP_EOL; // 1
echo $number->isGreaterThan(10) . PHP_EOL; // 1

$number = new BigNumber(20);
echo ($number->compareTo(20) === 0) . PHP_EOL; // 1
echo $number->isLessThanOrEqualTo(20) . PHP_EOL; // 1

示例 #4 - 输入过滤

// The arguments of all functions are also filtered.
$number = new BigNumber("9,223 372`036'854,775.808000");
echo $number . PHP_EOL; // 9223372036854775.808

示例 #5 - 执行一些操作然后将结果转换为62进制

$number = new BigNumber('9223372036854775807');
$number = $number->pow(2)->convertToBase(62);
echo $number . PHP_EOL; // 1wlVYJaWMuw53lV7Cg98qn

测试

测试在Tests文件夹中,达到100%代码覆盖率。要运行它们,您需要PHPUnit。示例

$ phpunit --configuration phpunit.xml.dist

或使用覆盖率报告

$ phpunit --configuration phpunit.xml.dist --coverage-html code_coverage/

鸣谢

AzaMath是Anizoptera CMF的一部分,由Amal Samally(amal.samally at gmail.com)和AzaGroup团队编写。
任意精度算术部分部分基于 Moontoast Math Library

许可协议

采用 MIT 许可协议发布。

链接