Download the PHP package webgriffe/rational without Composer

On this page you can find all versions of the php package webgriffe/rational. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package rational

Rational - A simple rational number implementation

Features

This library implements a numeric data type that represents a rational number, that is a number that can be represented as the division of two integer numbers. This includes periodic numbers (such as 0.333333..., which can be easily represented as 1/3) and those numbers that cannot be represented exactly by a floating point type (such as 0.1, which is a periodic number in binary representation, but which can be easily represented by the fraction 1/10).

In order to reduce the possibility of large values triggering overflow issues, an integer "whole" part is added to the fraction. This means that values are stored as mixed numbers of the form a + b/c, where a, b and c are all integers.

This library is fundamentally similar to https://github.com/markrogoyski/math-php/blob/master/src/Number/Rational.php, with the main exception being that this implementation uses the GMP extension internally to ensure that no overflow issues can arise with the intermediate computation results.

At the end of each operation, after all simplification and normalization steps, if the final results still do not fit into PHP's standard integer type, then an overflow exception is generated.

Setup

Add the library to your composer.json file in your project:

Use composer to install the library:

Composer will install the library inside your vendor folder. If you don't already use Composer in your project, you may need to explicitly include its autoload file in order to allow PHP to find the library class(es):

Minimum Requirements

Usage

Since floating point number are inherently inaccurate, it was deliberately decided NOT to provide a way to initialize a rational number from a float. Likewise, no method is provided to convert a rational to a float for the same reason.

Creating rational numbers is possible starting from integers or from the various parts of the rational number.

Usage in an application

Extension

It is possible to extend the Rational class to seamlessly use it in application-specific contexts.

For example, if one wants to use the Rational class to represent prices or amounts of money, it is possible to define a Money class that extends Rational:

All methods are type-hinted in such a way that all results will be identified as having the type of the first operand of each operation. So, for example, if a Money object is added to another Money, the result will be of type Money as well.

Containment

Of course, it is also possible to encapsulate instances of Rational inside other classes:

This has the benefit that one can control precisely what operations are allowed and what are not. For example, for a class that represents amounts of money, the reciprocal operation may not make much sense. Likewise, multiplying or dividing an amount of money by another amount of money may not be sensible.

The drawback is that one has to manually redefine all the desired arithmetic operations to work on the contained Rational value.

Internal working

The library stores all components of the rational number as PHP integers. This is to make it easier to store these values to databases and other media where storing arbitrary-length integers may be problematic. Intermediate values are handled through the PHP GMP library in order to avoid overflow issues until the final results are computed. If, however, the final result of each operation exceeds the range of PHP integers, the library reports an overflow error.

Immediately after creation and after every operation, each value is normalized. The purpose of this is to reduce the magnitude of the values stored internally and to make it easier to compare rational numbers and to extract other useful information.

In the context of this library which stores values as a + b/c, a normalized value is one where c > 0, where a * b >= 0 (i.e. they do not disagree in sign, though one or both can be zero), where GCD(|b|, c) == 1 (i.e. the fraction b/c is simplified) and |a| < b (i.e. it is a proper fraction).

Overflow

At the end of every operation the library converts the intermediate GMP values back to integers. If these values are too large or too small to fit into an integer, a OverflowException is thrown. It is the user's responsibility to catch the exception and act accordingly.

License

Webgriffe/Rational is licensed under the MIT License.


All versions of rational with dependencies

PHP Build Version
Package Version
Requires php Version ~8.1
ext-gmp Version *
ext-intl Version *
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package webgriffe/rational contains the following files

Loading the files please wait ....