Skip to content

Commit 947ddb3

Browse files
author
nejc
committed
feat: add integer types and traits
1 parent d79af5f commit 947ddb3

15 files changed

+954
-8
lines changed

examples/int128_operations.php

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?php
2+
3+
require_once __DIR__ . '/../vendor/autoload.php';
4+
5+
use Nejcc\PhpDatatypes\Scalar\Integers\Signed\Int128;
6+
7+
// Example 1: Basic Int128 Operations
8+
echo "Example 1: Basic Int128 Operations\n";
9+
try {
10+
$number = new Int128('170141183460469231731687303715884105720');
11+
echo "Created Int128: " . $number->getValue() . "\n";
12+
13+
// Addition
14+
$sum = $number->add(new Int128('7'));
15+
echo "Addition: " . $sum->getValue() . "\n";
16+
17+
// Subtraction
18+
$diff = $number->subtract(new Int128('100'));
19+
echo "Subtraction: " . $diff->getValue() . "\n";
20+
21+
// Multiplication
22+
$product = $number->multiply(new Int128('2'));
23+
echo "Multiplication: " . $product->getValue() . "\n";
24+
25+
// Division
26+
$quotient = $number->divide(new Int128('2'));
27+
echo "Division: " . $quotient->getValue() . "\n";
28+
} catch (Exception $e) {
29+
echo "Error in Example 1: " . $e->getMessage() . "\n";
30+
}
31+
32+
// Example 2: Range Validation
33+
echo "\nExample 2: Range Validation\n";
34+
try {
35+
// Valid range
36+
$valid = new Int128('170141183460469231731687303715884105727');
37+
echo "Valid maximum: " . $valid->getValue() . "\n";
38+
39+
// Invalid range (should throw OutOfRangeException)
40+
$invalid = new Int128('170141183460469231731687303715884105728');
41+
echo "This line should not be reached\n";
42+
} catch (OutOfRangeException $e) {
43+
echo "Caught OutOfRangeException: " . $e->getMessage() . "\n";
44+
}
45+
46+
// Example 3: Arithmetic Operations with Negative Numbers
47+
echo "\nExample 3: Arithmetic with Negative Numbers\n";
48+
try {
49+
$negative = new Int128('-170141183460469231731687303715884105720');
50+
echo "Negative number: " . $negative->getValue() . "\n";
51+
52+
// Addition with negative
53+
$sum = $negative->add(new Int128('100'));
54+
echo "Addition with negative: " . $sum->getValue() . "\n";
55+
56+
// Subtraction with negative
57+
$diff = $negative->subtract(new Int128('100'));
58+
echo "Subtraction with negative: " . $diff->getValue() . "\n";
59+
} catch (Exception $e) {
60+
echo "Error in Example 3: " . $e->getMessage() . "\n";
61+
}
62+
63+
// Example 4: Comparison Operations
64+
echo "\nExample 4: Comparison Operations\n";
65+
try {
66+
$a = new Int128('170141183460469231731687303715884105720');
67+
$b = new Int128('170141183460469231731687303715884105620');
68+
69+
echo "A: " . $a->getValue() . "\n";
70+
echo "B: " . $b->getValue() . "\n";
71+
72+
echo "A > B: " . ($a->greaterThan($b) ? 'true' : 'false') . "\n";
73+
echo "A < B: " . ($a->lessThan($b) ? 'true' : 'false') . "\n";
74+
echo "A == B: " . ($a->equals($b) ? 'true' : 'false') . "\n";
75+
} catch (Exception $e) {
76+
echo "Error in Example 4: " . $e->getMessage() . "\n";
77+
}
78+
79+
// Example 5: Overflow/Underflow Handling
80+
echo "\nExample 5: Overflow/Underflow Handling\n";
81+
try {
82+
$max = new Int128('170141183460469231731687303715884105727');
83+
echo "Maximum value: " . $max->getValue() . "\n";
84+
85+
// This should cause an overflow
86+
$overflow = $max->add(new Int128('1'));
87+
echo "This line should not be reached\n";
88+
} catch (OverflowException $e) {
89+
echo "Caught OverflowException: " . $e->getMessage() . "\n";
90+
}
91+
92+
try {
93+
$min = new Int128('-170141183460469231731687303715884105728');
94+
echo "Minimum value: " . $min->getValue() . "\n";
95+
96+
// This should cause an underflow
97+
$underflow = $min->subtract(new Int128('1'));
98+
echo "This line should not be reached\n";
99+
} catch (UnderflowException $e) {
100+
echo "Caught UnderflowException: " . $e->getMessage() . "\n";
101+
}

examples/int64_operations.php

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?php
2+
3+
require_once __DIR__ . '/../vendor/autoload.php';
4+
5+
use Nejcc\PhpDatatypes\Scalar\Integers\Signed\Int64;
6+
7+
// Example 1: Basic Int64 Operations
8+
echo "Example 1: Basic Int64 Operations\n";
9+
try {
10+
$number = new Int64('9223372036854775800');
11+
echo "Created Int64: " . $number->getValue() . "\n";
12+
13+
// Addition
14+
$sum = $number->add(new Int64('7'));
15+
echo "Addition: " . $sum->getValue() . "\n";
16+
17+
// Subtraction
18+
$diff = $number->subtract(new Int64('100'));
19+
echo "Subtraction: " . $diff->getValue() . "\n";
20+
21+
// Multiplication
22+
$product = $number->multiply(new Int64('2'));
23+
echo "Multiplication: " . $product->getValue() . "\n";
24+
25+
// Division
26+
$quotient = $number->divide(new Int64('2'));
27+
echo "Division: " . $quotient->getValue() . "\n";
28+
} catch (Exception $e) {
29+
echo "Error in Example 1: " . $e->getMessage() . "\n";
30+
}
31+
32+
// Example 2: Range Validation
33+
echo "\nExample 2: Range Validation\n";
34+
try {
35+
// Valid range
36+
$valid = new Int64('9223372036854775807');
37+
echo "Valid maximum: " . $valid->getValue() . "\n";
38+
39+
// Invalid range (should throw OutOfRangeException)
40+
$invalid = new Int64('9223372036854775808');
41+
echo "This line should not be reached\n";
42+
} catch (OutOfRangeException $e) {
43+
echo "Caught OutOfRangeException: " . $e->getMessage() . "\n";
44+
}
45+
46+
// Example 3: Arithmetic Operations with Negative Numbers
47+
echo "\nExample 3: Arithmetic with Negative Numbers\n";
48+
try {
49+
$negative = new Int64('-9223372036854775800');
50+
echo "Negative number: " . $negative->getValue() . "\n";
51+
52+
// Addition with negative
53+
$sum = $negative->add(new Int64('100'));
54+
echo "Addition with negative: " . $sum->getValue() . "\n";
55+
56+
// Subtraction with negative
57+
$diff = $negative->subtract(new Int64('100'));
58+
echo "Subtraction with negative: " . $diff->getValue() . "\n";
59+
} catch (Exception $e) {
60+
echo "Error in Example 3: " . $e->getMessage() . "\n";
61+
}
62+
63+
// Example 4: Comparison Operations
64+
echo "\nExample 4: Comparison Operations\n";
65+
try {
66+
$a = new Int64('9223372036854775800');
67+
$b = new Int64('9223372036854775700');
68+
69+
echo "A: " . $a->getValue() . "\n";
70+
echo "B: " . $b->getValue() . "\n";
71+
72+
echo "A > B: " . ($a->greaterThan($b) ? 'true' : 'false') . "\n";
73+
echo "A < B: " . ($a->lessThan($b) ? 'true' : 'false') . "\n";
74+
echo "A == B: " . ($a->equals($b) ? 'true' : 'false') . "\n";
75+
} catch (Exception $e) {
76+
echo "Error in Example 4: " . $e->getMessage() . "\n";
77+
}
78+
79+
// Example 5: Overflow/Underflow Handling
80+
echo "\nExample 5: Overflow/Underflow Handling\n";
81+
try {
82+
$max = new Int64('9223372036854775807');
83+
echo "Maximum value: " . $max->getValue() . "\n";
84+
85+
// This should cause an overflow
86+
$overflow = $max->add(new Int64('1'));
87+
echo "This line should not be reached\n";
88+
} catch (OverflowException $e) {
89+
echo "Caught OverflowException: " . $e->getMessage() . "\n";
90+
}
91+
92+
try {
93+
$min = new Int64('-9223372036854775808');
94+
echo "Minimum value: " . $min->getValue() . "\n";
95+
96+
// This should cause an underflow
97+
$underflow = $min->subtract(new Int64('1'));
98+
echo "This line should not be reached\n";
99+
} catch (UnderflowException $e) {
100+
echo "Caught UnderflowException: " . $e->getMessage() . "\n";
101+
}

examples/integer_operations.php

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
<?php
2+
3+
require_once __DIR__ . '/../vendor/autoload.php';
4+
5+
use Nejcc\PhpDatatypes\Scalar\Integers\Signed\Int8;
6+
use Nejcc\PhpDatatypes\Scalar\Integers\Signed\Int16;
7+
use Nejcc\PhpDatatypes\Scalar\Integers\Unsigned\UInt8;
8+
9+
/**
10+
* Example 1: Basic Int8 Operations
11+
*/
12+
echo "Example 1: Basic Int8 Operations\n";
13+
echo "==============================\n";
14+
15+
try {
16+
// Create Int8 instances
17+
$number1 = new Int8(42);
18+
$number2 = new Int8(10);
19+
20+
// Arithmetic operations
21+
$sum = $number1->add($number2);
22+
$difference = $number1->subtract($number2);
23+
$product = $number1->multiply($number2);
24+
$quotient = $number1->divide($number2);
25+
26+
echo "Number 1: " . $number1->getValue() . "\n";
27+
echo "Number 2: " . $number2->getValue() . "\n";
28+
echo "Sum: " . $sum->getValue() . "\n";
29+
echo "Difference: " . $difference->getValue() . "\n";
30+
echo "Product: " . $product->getValue() . "\n";
31+
echo "Quotient: " . $quotient->getValue() . "\n";
32+
} catch (\Exception $e) {
33+
echo "Error: " . $e->getMessage() . "\n";
34+
}
35+
36+
/**
37+
* Example 2: Range Validation
38+
*/
39+
echo "\nExample 2: Range Validation\n";
40+
echo "========================\n";
41+
42+
try {
43+
// This will throw OutOfRangeException
44+
$invalidNumber = new Int8(200);
45+
} catch (\OutOfRangeException $e) {
46+
echo "Range validation works: " . $e->getMessage() . "\n";
47+
}
48+
49+
/**
50+
* Example 3: Overflow Handling
51+
*/
52+
echo "\nExample 3: Overflow Handling\n";
53+
echo "=========================\n";
54+
55+
try {
56+
$maxInt8 = new Int8(Int8::MAX_VALUE);
57+
$overflow = $maxInt8->add(new Int8(1));
58+
} catch (\OverflowException $e) {
59+
echo "Overflow protection works: " . $e->getMessage() . "\n";
60+
}
61+
62+
/**
63+
* Example 4: Comparison Operations
64+
*/
65+
echo "\nExample 4: Comparison Operations\n";
66+
echo "=============================\n";
67+
68+
$num1 = new Int8(50);
69+
$num2 = new Int8(30);
70+
71+
echo "Is 50 greater than 30? " . ($num1->greaterThan($num2) ? "Yes" : "No") . "\n";
72+
echo "Is 50 less than 30? " . ($num1->lessThan($num2) ? "Yes" : "No") . "\n";
73+
echo "Is 50 equal to 30? " . ($num1->equals($num2) ? "Yes" : "No") . "\n";
74+
75+
/**
76+
* Example 5: Working with Different Integer Types
77+
*/
78+
echo "\nExample 5: Working with Different Integer Types\n";
79+
echo "===========================================\n";
80+
81+
try {
82+
$int8 = new Int8(100);
83+
$int16 = new Int16(1000);
84+
$uint8 = new UInt8(200);
85+
86+
echo "Int8 value: " . $int8->getValue() . "\n";
87+
echo "Int16 value: " . $int16->getValue() . "\n";
88+
echo "UInt8 value: " . $uint8->getValue() . "\n";
89+
} catch (\Exception $e) {
90+
echo "Error: " . $e->getMessage() . "\n";
91+
}
92+
93+
/**
94+
* Example 6: Division and Modulo Operations
95+
*/
96+
echo "\nExample 6: Division and Modulo Operations\n";
97+
echo "=====================================\n";
98+
99+
try {
100+
$dividend = new Int8(50);
101+
$divisor = new Int8(3);
102+
103+
// This will throw UnexpectedValueException because 50/3 is not an integer
104+
$result = $dividend->divide($divisor);
105+
} catch (\UnexpectedValueException $e) {
106+
echo "Division validation works: " . $e->getMessage() . "\n";
107+
}
108+
109+
try {
110+
$dividend = new Int8(50);
111+
$divisor = new Int8(5);
112+
113+
$quotient = $dividend->divide($divisor);
114+
$remainder = $dividend->mod($divisor);
115+
116+
echo "50 divided by 5 = " . $quotient->getValue() . "\n";
117+
echo "50 modulo 5 = " . $remainder->getValue() . "\n";
118+
} catch (\Exception $e) {
119+
echo "Error: " . $e->getMessage() . "\n";
120+
}

0 commit comments

Comments
 (0)