Computes exp(x) - 1 with improved precision for small values.
What Is expm1() in PHP?
Computes exp(x) - 1 with improved precision for small values.
Its syntax is:
expm1(float $num): float
Parameters:
$num— Accepted as float; required argument.
Return type: float. For small x, expm1(x) is more accurate than subtracting 1 from exp(x); extremely large x can overflow.
Availability: Core PHP.
Example 1: Basic Usage
<?php
echo round(expm1(1), 4);
Output:
1.7183
For small x, expm1(x) is more accurate than subtracting 1 from exp(x); extremely large x can overflow.
Example 2: A Different Input
<?php
echo expm1(0);
Output:
0
For small x, expm1(x) is more accurate than subtracting 1 from exp(x); extremely large x can overflow.
Practical Example: Applying It in a Program
<?php
$smallRate = 0.000001; echo number_format(expm1($smallRate), 8);
Output:
0.00000100
For small x, expm1(x) is more accurate than subtracting 1 from exp(x); extremely large x can overflow.
Important Behavior and Edge Cases
- Key behavior: For small x, expm1(x) is more accurate than subtracting 1 from exp(x); extremely large x can overflow.
- Environment: Core PHP.
Comparison With Related Functions
| Function | Purpose |
|---|---|
expm1() | Computes exp(x) - 1 with improved precision for small values. |
exp() | exp() returns e^x; expm1() returns e^x-1 with better accuracy for small x. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Ignoring this behavior: For small x, expm1(x) is more accurate than subtracting 1 from exp(x); extremely large x can overflow | For small x, expm1(x) is more accurate than subtracting 1 from exp(x); extremely large x can overflow. |
Key Takeaways
- Computes exp(x) - 1 with improved precision for small values.
- For small x, expm1(x) is more accurate than subtracting 1 from exp(x); extremely large x can overflow.
- Core PHP.
Summary: For small x, expm1(x) is more accurate than subtracting 1 from exp(x); extremely large x can overflow.