forked from php/php-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* PHP-8.3: random: Fix γ-section implementation for Randomizer::getFloat() (php#12402)
- Loading branch information
Showing
3 changed files
with
150 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
ext/random/tests/03_randomizer/methods/getFloat_extreme_range.phpt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
--TEST-- | ||
Random: Randomizer: getFloat(): Extreme ranges are handled correctly | ||
--FILE-- | ||
<?php | ||
|
||
use Random\Engine; | ||
use Random\Randomizer; | ||
|
||
final class TestEngine implements Engine | ||
{ | ||
private array $values = [ | ||
"\x64\xe8\x58\x79\x3e\xf6\x38\x00", | ||
"\x65\xe8\x58\x79\x3e\xf6\x38\x00", | ||
"\x00\x00\x00\x00\x00\x00\x00\x00", | ||
]; | ||
|
||
public function generate(): string | ||
{ | ||
return \array_shift($this->values); | ||
} | ||
} | ||
|
||
$r = new Randomizer(new TestEngine()); | ||
|
||
$min = -1.6e308; | ||
$max = 1.6e308; | ||
printf("%.17g\n", $min); | ||
printf("%.17g\n\n", $max); | ||
|
||
printf("%.17g\n", $r->getFloat($min, $max)); | ||
printf("%.17g\n", $r->getFloat($min, $max)); | ||
printf("%.17g\n", $r->getFloat($min, $max)); | ||
|
||
?> | ||
--EXPECT-- | ||
-1.6e+308 | ||
1.6e+308 | ||
|
||
-1.5999999999999998e+308 | ||
-1.6e+308 | ||
1.5999999999999998e+308 |
52 changes: 52 additions & 0 deletions
52
ext/random/tests/03_randomizer/methods/getFloat_opposite_signs.phpt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
--TEST-- | ||
Random: Randomizer: getFloat(): Opposite signs are handled correctly | ||
--FILE-- | ||
<?php | ||
|
||
use Random\Engine; | ||
use Random\Randomizer; | ||
|
||
final class TestEngine implements Engine | ||
{ | ||
private array $values = [ | ||
"\x63\xe8\x58\x79\x3e\xf6\x38\x00", | ||
"\x64\xe8\x58\x79\x3e\xf6\x38\x00", | ||
"\x65\xe8\x58\x79\x3e\xf6\x38\x00", | ||
"\x66\xe8\x58\x79\x3e\xf6\x38\x00", | ||
"\x67\xe8\x58\x79\x3e\xf6\x38\x00", | ||
]; | ||
|
||
public function generate(): string | ||
{ | ||
return \array_shift($this->values); | ||
} | ||
} | ||
|
||
$r = new Randomizer(new TestEngine()); | ||
|
||
$min = -1; | ||
$max = 1; | ||
printf("%.17g\n", $min); | ||
printf("%.17g\n\n", $max); | ||
|
||
$prev = null; | ||
for ($i = 0; $i < 5; $i++) { | ||
$float = $r->getFloat($min, $max); | ||
printf("%.17f", $float); | ||
if ($prev !== null) { | ||
printf(" (%+.17g)", ($float - $prev)); | ||
} | ||
printf("\n"); | ||
|
||
$prev = $float; | ||
} | ||
?> | ||
--EXPECT-- | ||
-1 | ||
1 | ||
|
||
-0.78005908680576086 | ||
-0.78005908680576097 (-1.1102230246251565e-16) | ||
-0.78005908680576108 (-1.1102230246251565e-16) | ||
-0.78005908680576119 (-1.1102230246251565e-16) | ||
-0.78005908680576130 (-1.1102230246251565e-16) |