Skip to content

Installation

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

composer require "samsara/fermat:^2.1"

Or by including it in your composer.json file:

1{
2  "require": {
3    "samsara/fermat": "^2.1"
4  }
5}

Dependencies

Fermat requires the following packages:

  • samsara/common: Provides the exception model used in Fermat

It also requires the BCMath and GMP extensions.

Improve Performance With Suggested Extensions

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

In particular, the Decimal extension results in performance increases ranging from 5x to 100x depending on the operation.

Basic Usage

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

 1<?php
 2
 3use Samsara\Fermat\Core\Values\ImmutableDecimal;
 4
 5// __construct(
 6//     $value, 
 7//     $scale = 10, 
 8//     $base = NumberBase::Ten, 
 9//     $baseTenInput = true
10// );
11$five = new ImmutableDecimal(5, 20);
12
13echo $five->pow('1.2')->sin()->getValue();
14// Prints: 0.57733662664006904181
15echo $five->getValue();
16// 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 "Source Reference" navigation tab contains detailed information about all the objects in Fermat.