php-dextrin

DXNRational
in package
implements DXNValueInterface

FinalYes

DXN `rational` (`22/7`) -- an exact ratio, never auto-reduced (`4/2` and `2/1` are structurally distinct values, same as the Elixir and JS ports). `numerator`/`denominator` are numeric strings, same "always bcmath, regardless of magnitude" rule as {@see DXNDecimal}.

Table of Contents

Interfaces

DXNValueInterface
Implemented by every `DXN*` wrapper class -- the PHP analog of the `dxnType` string tag used for kind dispatch in the JS port, and of `Dextrin.Value`'s closed type union in the Elixir original. Native PHP values (`null`, `bool`, `int`, `float`, `string`, and `array` for `map`/`array`) never implement this; {@see Equality::kindOf()} handles those directly.

Constants

DXN_TYPE  : mixed = 'rational'

Properties

$denominator  : numeric-string
$numerator  : numeric-string

Methods

__construct()  : mixed
compareTo()  : int
A total order against any other DXN value, not just another instance of this class -- backs {@see DXNSortedSet}. Cross-kind ordering is implementation-defined per DXN.md; within a kind, uses the kind's own natural order where one exists, or a deterministic structural fallback where it doesn't.
dxnType()  : string
The DXN type name this value represents, e.g. "symbol", "uuid".
equals()  : bool
Structural equality, recursing into nested DXN values as needed.
fromFloat()  : self
Exact, not approximate: every finite PHP `float` is a binary fraction (mantissa * 2^exponent), so decomposing it yields an exact rational with a power-of-2 denominator.
toFloat()  : float
decompose()  : array{0: numeric-string, 1: int}
normalize()  : numeric-string

Constants

Properties

$denominator read-only

public numeric-string $denominator

$numerator read-only

public numeric-string $numerator

Methods

__construct()

public __construct(string $numerator, string $denominator) : mixed
Parameters
$numerator : string
$denominator : string

compareTo()

A total order against any other DXN value, not just another instance of this class -- backs {@see DXNSortedSet}. Cross-kind ordering is implementation-defined per DXN.md; within a kind, uses the kind's own natural order where one exists, or a deterministic structural fallback where it doesn't.

public compareTo(mixed $other) : int
Parameters
$other : mixed
Return values
int

dxnType()

The DXN type name this value represents, e.g. "symbol", "uuid".

public dxnType() : string
Return values
string

equals()

Structural equality, recursing into nested DXN values as needed.

public equals(mixed $other) : bool
Parameters
$other : mixed
Return values
bool

fromFloat()

Exact, not approximate: every finite PHP `float` is a binary fraction (mantissa * 2^exponent), so decomposing it yields an exact rational with a power-of-2 denominator.

public static fromFloat(float $n) : self
Parameters
$n : float
Return values
self

toFloat()

public toFloat() : float
Return values
float

decompose()

private static decompose(float $n) : array{0: numeric-string, 1: int}
Parameters
$n : float
Return values
array{0: numeric-string, 1: int}

[signed integer mantissa (as a numeric string), base-2 exponent]

normalize()

private static normalize(string $s) : numeric-string
Parameters
$s : string
Return values
numeric-string
On this page

Search results