-
Notifications
You must be signed in to change notification settings - Fork 370
Closed
Description
LeetCode Username
iozh
Problem number, title, and link
- Pow(x, n) https://leetcode.com/problems/powx-n/
Category of the bug
- Problem description
- Solved examples
- Problem constraints
- Problem hints
- Incorrect or missing "Related Topics"
- Incorrect test Case (Output of test case is incorrect as per the problem statement)
- Missing test Case (Incorrect/Inefficient Code getting accepted because of missing test cases)
- Editorial
- Programming language support
Description of the bug
For PHP, one test case output is incorrect, even when using correct solution.
Language used for code
PHP
Code used for Submit/Run operation
class Solution
{
public function myPow(float $x, int $n): float
{
$result = 1.0;
while ($n) {
if ($n % 2) {
if ($n > 0) {
$result *= $x;
$n--;
} else {
$result /= $x;
$n++;
}
} else {
$x *= $x;
$n /= 2;
}
}
return $result;
}
}
Expected behavior
Test case with arguments x = 1.0000000000001, n = -2147483648. When evaluated, output is 1.00000, while the expected result is 0.99979. I tested multiple cases and that is what I found:
- Assigning the same value (1.0000000000001) to x inside function gives correct output (0.99979).
- The same code with the same arguments evaluated in local environment gives correct output (0.99979).
- Different solutions from "Solutions" tab, claimed to be correct, also do not pass this test (same mentioned by another user in "Discussion" tab).
- PHP pow and bcpow give the same wrong output (1.00000).
- var_dump($x) shows float(1).
I suppose there's some problem when x is passed to function in test case - it loses precision and comes as 1, not 1.0000000000001. This is what point 1 in above list about - when assigned directly inside function (just do $x = 1.0000000000001), the test is passed (but in this case the test with x = 1, n = -2147483648 fails as it also gets replaced).
Screenshots
Additional context
The same problem has been reported by multiple PHP programmers (on Github, in Discussion), but still not resolved.
Metadata
Metadata
Assignees
Labels
No labels