Skip to content

bypassflow/number-bcmath

Repository files navigation

bypassflow/number-bcmath

bypassflow/number-bcmath は、BCMathを使って整数と10進小数を正確に比較・正規化する数値処理パッケージです。 浮動小数点への変換を避け、数値文字列をそのまま扱うための小さな基盤を提供します。

Requirements

  • PHP 8.5以上
  • ext-bcmath

Installation

composer require bypassflow/number-bcmath

Quick Start

use bypassflow\NumberBcmath\Decimal\ExactDecimalComparator;
use bypassflow\NumberBcmath\Integer\ExactIntegerNormalizer;

$integer = new ExactIntegerNormalizer();
$decimal = new ExactDecimalComparator();

$normalized = $integer->normalize('+0010');
$sameValue = $decimal->compare('001.2300', '1.23') === 0;

このパッケージは、整数と10進小数を文字列として扱い、BCMathで正確に処理します。

  • 整数・10進小数の正規化と比較を扱います。
  • 倍数判定、符号判定、桁数判定、範囲丸めを扱います。
  • 金額、計測値、固定小数点表現など、丸め誤差を避けたい値に利用できます。

主要API

  • bypassflow\NumberBcmath\Integer\ExactIntegerNormalizer
    • 整数文字列を正規化します。
  • bypassflow\NumberBcmath\Integer\ExactIntegerComparator
    • 整数文字列を正確に比較します。
  • bypassflow\NumberBcmath\Integer\ExactIntegerMath
    • 整数文字列の倍数判定、符号判定、桁数判定、範囲丸めを扱います。
  • bypassflow\NumberBcmath\Decimal\ExactDecimalNormalizer
    • 10進小数文字列を正規化します。
  • bypassflow\NumberBcmath\Decimal\ExactDecimalComparator
    • 10進小数文字列を正確に比較します。
  • bypassflow\NumberBcmath\Decimal\ExactDecimalMath
    • 10進小数文字列の倍数判定、小数桁数判定、整数部桁数判定、範囲丸めを扱います。

ExactIntegerComparatorExactDecimalComparatorは次の操作を持ちます。

  • compare()
  • isGreaterThan()
  • isGreaterThanOrEqual()
  • isLessThan()
  • isLessThanOrEqual()
  • isBetweenInclusive()
  • isBetweenExclusive()

正規化クラスはnormalize()を持ち、比較クラスからも利用されます。

ExactIntegerMathは次の操作を持ちます。

  • isMultipleOf()
  • abs()
  • signum()
  • minOf()
  • maxOf()
  • clamp()
  • digitsOf()
  • hasDigits()

ExactDecimalMathは次の操作を持ちます。

  • isMultipleOf()
  • fractionDigitsOf()
  • hasFractionDigits()
  • abs()
  • signum()
  • minOf()
  • maxOf()
  • clamp()
  • integerDigitsOf()
  • totalDigitsOf()
  • hasIntegerDigits()
  • hasTotalDigits()

関数・補助関数

現時点ではありません。

失敗時の扱い

不正な数値文字列や不正な範囲指定は\InvalidArgumentExceptionになります。

use bypassflow\NumberBcmath\Integer\ExactIntegerNormalizer;

$normalizer = new ExactIntegerNormalizer();

try {
    $normalizer->normalize('12.3');
} catch (\InvalidArgumentException $exception) {
    $message = $exception->getMessage();
}

isMultipleOf()の単位値に0を渡した場合、桁数指定に1未満を渡した場合、clamp()で最小値が最大値を超える場合も\InvalidArgumentExceptionになります。

About

BCMathを使った正確な整数・10進小数処理基盤。

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages