1: <?php
2: /**
3: * Hard type support
4: * For when you absolutely want to know what you are getting
5: *
6: * @author Ashley Kitson <akitson@zf4.biz>
7: * @copyright Ashley Kitson, UK, 2014
8: * @licence GPL V3 or later : http://www.gnu.org/licenses/gpl.html
9: */
10:
11: namespace chippyash\Type\Number;
12:
13: use \chippyash\Type\AbstractType;
14: use chippyash\Type\Interfaces\NumericTypeInterface;
15: use chippyash\Type\Number\Complex\ComplexType;
16: use chippyash\Type\Number\Rational\RationalType;
17: use chippyash\Type\Number\Rational\RationalTypeFactory;
18:
19: /**
20: * Floating number Type
21: */
22: class FloatType extends AbstractType implements NumericTypeInterface
23: {
24:
25: /**
26: * Negates the number
27: *
28: * @return \chippyash\Type\Number\FloatType Fluent Interface
29: */
30: public function negate()
31: {
32: $this->value *= -1;
33:
34: return $this;
35: }
36:
37: /**
38: * Return the number as a Complex number i.e. n+0i
39: *
40: * @return \chippyash\Type\Number\Complex\ComplexType
41: */
42: public function asComplex()
43: {
44: return new ComplexType(
45: $this->asRational(),
46: new RationalType(new IntType(0), new IntType(1))
47: );
48: }
49:
50: /**
51: * Return number as Rational number.
52: * NB, numerator and denominator will be caste as IntTypes
53: *
54: * @return \chippyash\Type\Number\Rational\RationalType
55: */
56: public function asRational()
57: {
58: return RationalTypeFactory::fromFloat($this->value);
59: }
60:
61: /**
62: * Return number as an IntType number.
63: *
64: * @return \chippyash\Type\Number\IntType
65: */
66: public function asIntType()
67: {
68: return new IntType($this->value);
69: }
70:
71: /**
72: * Return number as a FloatType number.
73: *
74: * @return \chippyash\Type\Number\FloatType
75: */
76: public function asFloatType()
77: {
78: return clone $this;
79: }
80:
81: /**
82: * Return the absolute value of the number
83: *
84: * @return \chippyash\Type\Number\FloatType
85: */
86: public function abs()
87: {
88: return new self(abs($this->value));
89: }
90:
91: /**
92: * Return correctly typed value for this type
93: *
94: * @param mixed $value
95: *
96: * @return float
97: */
98: protected function typeOf($value)
99: {
100: return floatval($value);
101: }
102: }
103: