Nette Documentation Preview

syntax
Working with Floats
*******************

.[perex]{data-version:3.2}
[api:Nette\Utils\Floats] is a static class containing useful functions for comparing floating-point numbers.


Installation:

```shell
composer require nette/utils
```

All examples assume the following class alias is defined:

```php
use Nette\Utils\Floats;
```


Motivation
==========

Wondering what a float comparison class is for? You can use operators `<`, `>`, `===`, you think.
This is not entirely true. What do you think will print this code?

```php
$a = 0.1 + 0.2;
$b = 0.3;
echo $a === $b ? 'same' : 'not same';
```

If you run the code, some of you might be surprised that the program outputs `not same`.

Mathematical operations with floating-point numbers can lead to precision errors due to the conversion between decimal and binary representations. For example, `0.1 + 0.2` results in something like `0.300000000000000044…`. Therefore, when comparing floats, we need to tolerate a small difference, an epsilon.

And that's exactly what the `Floats` class does. The following comparison will now work as expected:

```php
echo Floats::areEqual($a, $b) ? 'same' : 'not same'; // same
```

Trying to compare `NAN` throws a `\LogicException`.


Float Comparison
================


areEqual(float $a, float $b): bool .[method]
--------------------------------------------

Returns `true` if `$a` = `$b`.

```php
Floats::areEqual(10, 10.0); // true
```


isLessThan(float $a, float $b): bool .[method]
----------------------------------------------

Returns `true` if `$a` < `$b`.

```php
Floats::isLessThan(9.5, 10.2); // true
Floats::isLessThan(INF, 10.2); // false
```


isLessThanOrEqualTo(float $a, float $b): bool .[method]
-------------------------------------------------------

Returns `true` if `$a` <= `$b`.

```php
Floats::isLessThanOrEqualTo(9.5, 10.2);    // true
Floats::isLessThanOrEqualTo(10.25, 10.25); // true
```


isGreaterThan(float $a, float $b): bool .[method]
-------------------------------------------------

Returns `true` if `$a` > `$b`.

```php
Floats::isGreaterThan(9.5, -10.2); // true
Floats::isGreaterThan(9.5, 10.2);  // false
```


isGreaterThanOrEqualTo(float $a, float $b): bool .[method]
----------------------------------------------------------

Returns `true` if `$a` >= `$b`.

```php
Floats::isGreaterThanOrEqualTo(9.5, 10.2);  // false
Floats::isGreaterThanOrEqualTo(10.2, 10.2); // true
```


compare(float $a, float $b): int .[method]
------------------------------------------

Returns `-1` if `$a` < `$b`, `0` if they are equal, and `1` if `$a` > `$b`.

It can be used, for example, with the `usort()` function.

```php
$arr = [1, 5, 2, -3.5];
usort($arr, [Float::class, 'compare']);
// $arr is now [-3.5, 1, 2, 5]
```


Helper Functions
================


isZero(float $value): bool .[method]
------------------------------------

Returns `true` if the value is zero.

```php
Floats::isZero(0.0); // true
Floats::isZero(0);   // true
```


isInteger(float $value): bool .[method]
---------------------------------------

Returns `true` if the value is an integer.

```php
Floats::isInteger(0);    // true
Floats::isInteger(0.0);  // true
Floats::isInteger(-5.0); // true

Floats::isInteger(-5.1); // false
Floats::isInteger(INF);  // false
Floats::isInteger(NAN);  // false
```

Working with Floats

Nette\Utils\Floats is a static class containing useful functions for comparing floating-point numbers.

Installation:

composer require nette/utils

All examples assume the following class alias is defined:

use Nette\Utils\Floats;

Motivation

Wondering what a float comparison class is for? You can use operators <, >, ===, you think. This is not entirely true. What do you think will print this code?

$a = 0.1 + 0.2;
$b = 0.3;
echo $a === $b ? 'same' : 'not same';

If you run the code, some of you might be surprised that the program outputs not same.

Mathematical operations with floating-point numbers can lead to precision errors due to the conversion between decimal and binary representations. For example, 0.1 + 0.2 results in something like 0.300000000000000044…. Therefore, when comparing floats, we need to tolerate a small difference, an epsilon.

And that's exactly what the Floats class does. The following comparison will now work as expected:

echo Floats::areEqual($a, $b) ? 'same' : 'not same'; // same

Trying to compare NAN throws a \LogicException.

Float Comparison

areEqual(float $a, float $b)bool

Returns true if $a = $b.

Floats::areEqual(10, 10.0); // true

isLessThan(float $a, float $b)bool

Returns true if $a < $b.

Floats::isLessThan(9.5, 10.2); // true
Floats::isLessThan(INF, 10.2); // false

isLessThanOrEqualTo(float $a, float $b)bool

Returns true if $a <= $b.

Floats::isLessThanOrEqualTo(9.5, 10.2);    // true
Floats::isLessThanOrEqualTo(10.25, 10.25); // true

isGreaterThan(float $a, float $b)bool

Returns true if $a > $b.

Floats::isGreaterThan(9.5, -10.2); // true
Floats::isGreaterThan(9.5, 10.2);  // false

isGreaterThanOrEqualTo(float $a, float $b)bool

Returns true if $a >= $b.

Floats::isGreaterThanOrEqualTo(9.5, 10.2);  // false
Floats::isGreaterThanOrEqualTo(10.2, 10.2); // true

compare(float $a, float $b)int

Returns -1 if $a < $b, 0 if they are equal, and 1 if $a > $b.

It can be used, for example, with the usort() function.

$arr = [1, 5, 2, -3.5];
usort($arr, [Float::class, 'compare']);
// $arr is now [-3.5, 1, 2, 5]

Helper Functions

isZero(float $value): bool

Returns true if the value is zero.

Floats::isZero(0.0); // true
Floats::isZero(0);   // true

isInteger(float $value): bool

Returns true if the value is an integer.

Floats::isInteger(0);    // true
Floats::isInteger(0.0);  // true
Floats::isInteger(-5.0); // true

Floats::isInteger(-5.1); // false
Floats::isInteger(INF);  // false
Floats::isInteger(NAN);  // false