Skip to content

Commit 2c53a83

Browse files
committed
update for floats
1 parent e1d3458 commit 2c53a83

16 files changed

+337
-88
lines changed

index.php

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,20 @@
99

1010
class test
1111
{
12-
public Int8 $min;
13-
public UInt8 $minU;
14-
public Int16 $max;
15-
public Int32 $max2;
16-
// public Int64 $max3;
17-
// public Int128 $max4;
18-
19-
public function __construct(int $int, int $max, int $max2, int $max3, int $max4)
12+
public \Nejcc\PhpDatatypes\Floats\Float8 $min;
13+
14+
public function __construct(int $int)
2015
{
21-
$this->minU = uint8(1);
22-
// $this->min = int8($int);
23-
// $this->max = int16($max);
24-
// $this->max2 = int32($max2);
25-
// $this->max3 = int64($max3);
26-
// $this->max4 = int128($max4);
16+
$this->min = float8($int);
2717
}
2818

29-
public function __invoke(): Int8
19+
public function __invoke(): \Nejcc\PhpDatatypes\Floats\Float8
3020
{
3121
return $this->min;
3222
}
3323
}
3424

35-
$c = new test(8, 20000, 23213,23213,123123);
25+
$c = new test(8);
3626
var_dump($c);
3727

3828

src/Abstract/AbstractFloat.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Nejcc\PhpDatatypes\Abstract;
6+
7+
use OutOfRangeException;
8+
9+
abstract class AbstractFloat
10+
{
11+
protected readonly float $value;
12+
13+
public const MIN_VALUE = null;
14+
public const MAX_VALUE = null;
15+
16+
public function __construct(float $value)
17+
{
18+
$this->setValue($value);
19+
}
20+
21+
protected function setValue(float $value): void
22+
{
23+
// Check if value is out of range
24+
if ($value < static::MIN_VALUE || $value > static::MAX_VALUE) {
25+
throw new OutOfRangeException(sprintf(
26+
'Value %f is out of range for this float type. Allowed range: [%f, %f]',
27+
$value,
28+
static::MIN_VALUE,
29+
static::MAX_VALUE
30+
));
31+
}
32+
33+
$this->value = $value;
34+
}
35+
36+
public function getValue(): float
37+
{
38+
return $this->value;
39+
}
40+
}

src/Floats/AbstractFloat.php

Lines changed: 0 additions & 41 deletions
This file was deleted.

src/Floats/Float128.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Nejcc\PhpDatatypes\Floats;
6+
7+
use Nejcc\PhpDatatypes\Abstract\AbstractFloat;
8+
9+
/**
10+
* Represents a 128-bit float.
11+
*
12+
* @package Nejcc\PhpDatatypes\Floats
13+
*/
14+
final class Float128 extends AbstractFloat
15+
{
16+
/**
17+
* The minimum allowable value for Float128.
18+
*
19+
* @var float
20+
*/
21+
public const MIN_VALUE = -1.18973149535723176508575932662800702e4932;
22+
23+
/**
24+
* The maximum allowable value for Float128.
25+
*
26+
* @var float
27+
*/
28+
public const MAX_VALUE = 1.18973149535723176508575932662800702e4932;
29+
}

src/Floats/Float16.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Nejcc\PhpDatatypes\Floats;
6+
7+
use Nejcc\PhpDatatypes\Abstract\AbstractFloat;
8+
9+
/**
10+
* Represents a 16-bit float.
11+
*
12+
* @package Nejcc\PhpDatatypes\Floats
13+
*/
14+
final class Float16 extends AbstractFloat
15+
{
16+
/**
17+
* The minimum allowable value for Float16.
18+
*
19+
* @var float
20+
*/
21+
public const MIN_VALUE = -65504.0;
22+
23+
/**
24+
* The maximum allowable value for Float16.
25+
*
26+
* @var float
27+
*/
28+
public const MAX_VALUE = 65504.0;
29+
}

src/Floats/Float32.php

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,26 @@
44

55
namespace Nejcc\PhpDatatypes\Floats;
66

7+
use Nejcc\PhpDatatypes\Abstract\AbstractFloat;
8+
9+
/**
10+
* Represents a 32-bit float.
11+
*
12+
* @package Nejcc\PhpDatatypes\Floats
13+
*/
714
final class Float32 extends AbstractFloat
815
{
9-
protected function getScale(): int
10-
{
11-
return 7; // Approximately 7 decimal digits of precision
12-
}
13-
14-
public function add(FloatInterface $other): static
15-
{
16-
$result = bcadd((string)$this->value, (string)$other->getValue(), $this->getScale());
17-
return new static($result);
18-
}
16+
/**
17+
* The minimum allowable value for Float32.
18+
*
19+
* @var float
20+
*/
21+
public const MIN_VALUE = -3.4028235e38;
1922

20-
// Implement other arithmetic methods similarly
23+
/**
24+
* The maximum allowable value for Float32.
25+
*
26+
* @var float
27+
*/
28+
public const MAX_VALUE = 3.4028235e38;
2129
}

src/Floats/Float64.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Nejcc\PhpDatatypes\Floats;
6+
7+
use Nejcc\PhpDatatypes\Abstract\AbstractFloat;
8+
9+
/**
10+
* Represents a 64-bit float.
11+
*
12+
* @package Nejcc\PhpDatatypes\Floats
13+
*/
14+
final class Float64 extends AbstractFloat
15+
{
16+
/**
17+
* The minimum allowable value for Float64.
18+
*
19+
* @var float
20+
*/
21+
public const MIN_VALUE = -1.7976931348623157e308;
22+
23+
/**
24+
* The maximum allowable value for Float64.
25+
*
26+
* @var float
27+
*/
28+
public const MAX_VALUE = 1.7976931348623157e308;
29+
}

src/Floats/Float8.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Nejcc\PhpDatatypes\Floats;
6+
7+
use Nejcc\PhpDatatypes\Abstract\AbstractFloat;
8+
9+
/**
10+
* Represents an 8-bit float.
11+
*
12+
* @package Nejcc\PhpDatatypes\Floats
13+
*/
14+
final class Float8 extends AbstractFloat
15+
{
16+
/**
17+
* The minimum allowable value for Float8.
18+
*
19+
* @var float
20+
*/
21+
public const MIN_VALUE = -240.0;
22+
23+
/**
24+
* The maximum allowable value for Float8.
25+
*
26+
* @var float
27+
*/
28+
public const MAX_VALUE = 240.0;
29+
}

src/Floats/FloatInterface.php

Lines changed: 0 additions & 20 deletions
This file was deleted.

src/Interfaces/FloatInterface.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Nejcc\PhpDatatypes\Interfaces;
4+
5+
interface FloatInterface
6+
{
7+
public function getValue(): float;
8+
9+
}

0 commit comments

Comments
 (0)