Overview

Namespaces

  • BN
    • Collections
    • Compiler
      • Grammar
      • Parser
        • Operator
        • Token
      • Postfix
        • Operands
        • Operator
        • Token
      • Scanner
        • Converter
      • Token
  • Demo
  • None

Classes

  • AggregateFunctions
  • Number
  • NumberFactory
  • OperatorsFactory

Interfaces

  • INumber
  • Overview
  • Namespace
  • Class
  • Tree

Class Number

Big Number data type for PHP. Uses BC Math for arbitrary precision mathematics with numbers of any size and precision. Object is immutable and the inner representation of number is a string.

BN\Number implements BN\INumber
Namespace: BN
Located at lib/BN/Number.php
Methods summary
public
# __construct( string $number = '0' )

Sets a number and reset local scale (instance will use only global scale).

Sets a number and reset local scale (instance will use only global scale).

Parameters

$number
string
$number

Throws

InvalidArgumentException
if $number is not numeric string
public BN\INumber
# add( BN\INumber $number )

Gets a new number which is the sum of instance and argument.

Gets a new number which is the sum of instance and argument.

Parameters

$number
BN\INumber
$number

Returns

BN\INumber

Implementation of

BN\INumber::add()
public BN\INumber
# subtract( BN\INumber $number )

Gets a new number which is the difference of instance and argument.

Gets a new number which is the difference of instance and argument.

Parameters

$number
BN\INumber
$number

Returns

BN\INumber

Implementation of

BN\INumber::subtract()
public BN\INumber
# multiply( BN\INumber $number )

Gets a new number which is the product of instance and argument.

Gets a new number which is the product of instance and argument.

Parameters

$number
BN\INumber
$number

Returns

BN\INumber

Implementation of

BN\INumber::multiply()
public BN\INumber
# divide( BN\INumber $number )

Gets a new number which is the division of instance and argument.

Gets a new number which is the division of instance and argument.

Parameters

$number
BN\INumber
$number

Returns

BN\INumber

Throws

InvalidArgumentException
if $number equals 0

Example

5 / 2 = 2.5

Implementation of

BN\INumber::divide()
public BN\INumber
# quotient( BN\INumber $number )

Gets a new number which is the division of instance and argument without remainder.

Gets a new number which is the division of instance and argument without remainder.

Returns

BN\INumber

Throws

InvalidArgumentException
if $number equals 0

Example

10 \ 3 = 3
10 \ -3 = -3
-10 \ 3 = -3
-10 \ -3 = 3

Implementation of

BN\INumber::quotient()
public BN\INumber
# modulo( BN\INumber $number )

Gets a new number which is the remainder after division of instance by argument. It supports a decimal remainder (calculated m(x,n) = x - n * floor(x/n)). See the rules in examples:

Gets a new number which is the remainder after division of instance by argument. It supports a decimal remainder (calculated m(x,n) = x - n * floor(x/n)). See the rules in examples:

Parameters

$number
BN\INumber
$number

Returns

BN\INumber

Throws

InvalidArgumentException
if $number equals 0

Example

10 % 2.1 = 1.6
10 % -2.1 = 1.6
-10 % 2.1 = -1.6
-10 % 2.1 = -1.6

Implementation of

BN\INumber::modulo()
public BN\INumber
# power( BN\INumber $number )

Gets a new number which is instance raised to the power of argument. If power is decimal number and it's not square root (1/2) then numbers are typed to float (IEEE 754) and then pow function is used. Result can be influenced by inaccurate floating point precision.

Gets a new number which is instance raised to the power of argument. If power is decimal number and it's not square root (1/2) then numbers are typed to float (IEEE 754) and then pow function is used. Result can be influenced by inaccurate floating point precision.

Parameters

$number
BN\INumber
$number

Returns

BN\INumber

Implementation of

BN\INumber::power()
public BN\INumber
# sqrt( )

Gets a new number which is the square root of instance.

Gets a new number which is the square root of instance.

Returns

BN\INumber

Throws

InvalidArgumentException
if $number is smaller than 0

Implementation of

BN\INumber::sqrt()
public BN\INumber
# abs( )

Gets a new number which is the absolute value of instance.

Gets a new number which is the absolute value of instance.

Returns

BN\INumber

Implementation of

BN\INumber::abs()
public BN\INumber
# negate( )

Gets a new number which is the negated instance.

Gets a new number which is the negated instance.

Returns

BN\INumber

Implementation of

BN\INumber::negate()
public boolean
# isInteger( )

Returns true if instance is integer. Number is integer if don't have a decimal point or if numbers after decimal points are zeros.

Returns true if instance is integer. Number is integer if don't have a decimal point or if numbers after decimal points are zeros.

Returns

boolean

Implementation of

BN\INumber::isInteger()
public boolean
# isZero( )

Returns true if instance equal to zero.

Returns true if instance equal to zero.

Returns

boolean

Implementation of

BN\INumber::isZero()
public boolean
# isNegative( )

Returns true if instance is smaller than zero.

Returns true if instance is smaller than zero.

Returns

boolean

Implementation of

BN\INumber::isNegative()
public boolean
# isPositive( )

Returns true if instance is larger than zero.

Returns true if instance is larger than zero.

Returns

boolean

Implementation of

BN\INumber::isPositive()
public integer
# compare( BN\INumber $number )

Compares instance with argument and returns 0 if number are equal. If instance is larger than argument it returns 1. Otherwise returns -1.

Compares instance with argument and returns 0 if number are equal. If instance is larger than argument it returns 1. Otherwise returns -1.

Parameters

$number
BN\INumber
$number

Returns

integer

Implementation of

BN\INumber::compare()
public boolean
# isEqual( BN\INumber $number )

Returns true if instance and argument are equal numbers.

Returns true if instance and argument are equal numbers.

Parameters

$number
BN\INumber
$number

Returns

boolean

Implementation of

BN\INumber::isEqual()
public boolean
# isBiggerThan( BN\INumber $number )

Returns true if instance is larger than argument

Returns true if instance is larger than argument

Parameters

$number
BN\INumber
$number

Returns

boolean

Implementation of

BN\INumber::isBiggerThan()
public boolean
# isSmallerThan( BN\INumber $number )

Returns true if instance is less than argument.

Returns true if instance is less than argument.

Parameters

$number
BN\INumber
$number

Returns

boolean

Implementation of

BN\INumber::isSmallerThan()
public BN\INumber
# round( integer $precision )

Gets a new number which is number rounded to nearest number. Precision is number of digits before (negative) or after (positive number) the decimal point.

Gets a new number which is number rounded to nearest number. Precision is number of digits before (negative) or after (positive number) the decimal point.

Parameters

$precision
integer
$precision

Returns

BN\INumber

Throws

InvalidArgumentException
if $precision is not integer

Example

2.56 round 1 = 2.6
74 round -1 = 70

Implementation of

BN\INumber::round()
public BN\INumber
# roundUp( )

Gets a new number which is the next integer farthest from zero. It round fractions up.

Gets a new number which is the next integer farthest from zero. It round fractions up.

Returns

BN\INumber

Example

8.62 roundUp = 9
-3.2 roundUp = -4

Implementation of

BN\INumber::roundUp()
public BN\INumber
# roundDown( )

Gets a new number which is the the next integer closest to zero. It round fractions down.

Gets a new number which is the the next integer closest to zero. It round fractions down.

Returns

BN\INumber

Example

8.62 roundDown = 8
-3.2 roundDown = -3

Implementation of

BN\INumber::roundDown()
public BN\INumber
# roundToNumber( integer $precision )

Gets a new number which is the nearest multiple of precision. If precision is positive number (round after decimal point) then it at first rounds number to number of digits in precision.

Gets a new number which is the nearest multiple of precision. If precision is positive number (round after decimal point) then it at first rounds number to number of digits in precision.

Parameters

$precision
integer
$precision

Returns

BN\INumber

Throws

InvalidArgumentException
if $precision is not integer

Example

8.6256 roundTo 20 (= temporary 8.63 roundTo 20) = 8.60
165 roundTo -50 = 150

Implementation of

BN\INumber::roundToNumber()
public
# setLocalScale( integer $scale )

Sets a local scale used to set number of digits after decimal point in the result. It's defined only for the instance, all other instances will use global scale. Be careful about using local scale for methods modulo and roundToNumber, because they are creating temporary numbers without local scale.

Sets a local scale used to set number of digits after decimal point in the result. It's defined only for the instance, all other instances will use global scale. Be careful about using local scale for methods modulo and roundToNumber, because they are creating temporary numbers without local scale.

Parameters

$scale
integer
$scale

Throws

InvalidArgumentException
if $scale is not integer or it's smaller than 2

Implementation of

BN\INumber::setLocalScale()
public
# resetLocalScale( )

Operation won't use local scale, but global scale (if it's defined). E.g. in BC Math global scale is defined by function bcscale.

Operation won't use local scale, but global scale (if it's defined). E.g. in BC Math global scale is defined by function bcscale.

Implementation of

BN\INumber::resetLocalScale()
public string
# __toString( )

Gets a string represetation of number.

Gets a string represetation of number.

Returns

string

Implementation of

BN\INumber::__toString()
public BN\INumber
# __clone( )

Clones the current instance.

Clones the current instance.

Returns

BN\INumber

Implementation of

BN\INumber::__clone()
BN-PHP - Big Number data type for PHP API documentation generated by ApiGen 2.8.0