Passed
Push — master ( 223051...44a4ce )
by Fabio
06:19 queued 01:38
created

TURational::getValue()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 5
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 9
rs 10
1
<?php
2
/**
3
 * TURational class file
4
 *
5
 * @author Brad Anderson <[email protected]>
6
 * @link https://github.com/pradosoft/prado
7
 * @license https://github.com/pradosoft/prado/blob/master/LICENSE
8
 */
9
10
namespace Prado\Util\Math;
11
12
use Prado\Util\TBitHelper;
13
14
/**
15
 * TURational class.
16
 *
17
 * TURational implements a fraction in the form of one unsigned integer {@link getNumerator
18
 *  Numerator} divided by another unsigned integer {@link getDenominator Denominator}.
19
 *
20
 * TURational is a specialization of {@link TRational} and TRational has information
21
 * about how these classes work.
22
 *
23
 * INF is "4294967295/0" and NAN (Not A Number) has the denominator equal zero
24
 * (to avoid a divide by zero error).
25
 *
26
 * When setting a {@link setNumerator Numerator} and {@link setDenominator Denominator},
27
 * the PHP instance is checked if it is 32 bit or 64 bit.  64 Bit PHP can represent
28
 * integers in the range [2147483648, 4294967295] as an integer, but on a 32 bit
29
 * PHP instance, these high bit integers are converted to float to be more accurately
30
 * represented.
31
 *
32
 * The Rational data format is used by EXIF and, in particular, the GPS Image File
33
 * Directory.
34
 *
35
 * @author Brad Anderson <[email protected]>
36
 * @see TRational
37
 * @since 4.2.3
38
 */
39
class TURational extends TRational
40
{
41
	/**
42
	 * @return bool Is the class unsigned.  Returns true.
43
	 */
44
	public static function getIsUnsigned(): bool
45
	{
46
		return true;
47
	}
48
49
	/**
50
	 * This only accepts 0 and positive values. For 32 bit systems this accepts a float
51
	 * to represent numbers larger than PHP_INT_MAX.
52
	 * @param float|int|string $value The numerator.
53
	 */
54
	public function setNumerator($value): TURational
55
	{
56
		$value = min(max(0, $value), TBitHelper::PHP_INT32_UMAX);
57
		if (PHP_INT_SIZE > 4 || $value <= PHP_INT_MAX) {
58
			$this->_numerator = (int) $value;
59
		} else {
60
			$this->_numerator = (float) $value;
61
		}
62
		return $this;
63
	}
64
65
	/**
66
	 * This only accepts 0 and positive values. For 32 bit systems this accepts a float
67
	 * to represent numbers larger than PHP_INT_MAX.
68
	 * @param float|int|string $value The denominator.
69
	 */
70
	public function setDenominator($value): TURational
71
	{
72
		$value = min(max(0, $value), TBitHelper::PHP_INT32_UMAX);
73
		if (PHP_INT_SIZE > 4 || $value <= PHP_INT_MAX) {
74
			$this->_denominator = (int) $value;
75
		} else {
76
			$this->_denominator = (float) $value;
77
		}
78
		return $this;
79
	}
80
81
	/**
82
	 * This returns the float value of the Numerator divided by the denominator.
83
	 * Returns INF (Infinity) float value if the {@link getNumerator Numerator} is
84
	 * 0xFFFFFFFF (4294967295) and {@link getDenominator Denominator} is 0.   Returns
85
	 * NAN (Not A Number) float value if the {@link getDenominator Denominator} is zero.
86
	 * @return float The float value of the Numerator divided by denominator.
87
	 */
88
	public function getValue(): float
89
	{
90
		if ($this->_numerator === TBitHelper::PHP_INT32_UMAX && $this->_denominator === 0) {
91
			return INF;
92
		}
93
		if ($this->_denominator === 0) {
94
			return NAN;
95
		}
96
		return ((float) $this->_numerator) / ((float) $this->_denominator);
97
	}
98
}
99