Installation

The Fermat library is available on Packagist, and can be installed with composer:

composer require samsara/fermat ^2.0

Dependencies

Fermat requires the following packages:

  • ircmaxell/random-lib: Provides cross-platform random number generation
  • riimu/kit-baseconversion: Provides the base conversion library used internally
  • samsara/common: Provides the exception model used in Fermat

It also requires the BCMath extension for PHP, however since 7.0 this extension has been included by default in distributions.

Improve Performance With Suggested Extensions

Fermat suggests that you also install the ext-ds extension and the ext-gmp extension. When present, these help reduce memory usage and computation time.

Basic Usage

A basic usage of the Fermat library is straightforward and simple to use quickly.

 1<?php
 2
 3use Samsara\Fermat\Values\ImmutableDecimal;
 4
 5// __construct($value, $scale = 10, $base = 10);
 6$five = new ImmutableDecimal(5, 20);
 7
 8echo $five->pow('1.2')->sin()->getValue();
 9// Prints: 0.57733662664006904181
10echo $five->getValue();
11// Prints: 5

Once you have your number objects created, you can continue using them with your desired scale.

Fluency

Both immutable and mutable instances can be used with a fluent interface.

With mutable objects, this is due to the class being designed with a fluent interface inherently. With immutable objects, this is due to a new instance of the immutable object being returned.

This means that each method call on an immutable object which returns an object represents a new instance being created and returned, a new zval being created by PHP, and a new set of memory being allocated.

See Also

The "Types & Values" section contains extensive detail about the exact ways that the value objects can be used.