--- name: exp signature: 'exp EXPR' status: documented categories: ["Numeric functions"] --- ```{index} single: exp; Perl built-in ``` *[Numeric functions](../perlfunc-by-category)* # exp Raise *e* to a power. `exp` returns *e* (the base of natural logarithms, roughly `2.718281828459045`) raised to the power of `EXPR`. It is the inverse of [`log`](log): for any finite positive `$x`, `exp(log($x))` reproduces `$x` to within floating-point rounding. With no argument, `exp` operates on [`$_`](../perlvar). ## Synopsis ```perl exp EXPR exp ``` ## What you get back A double-precision floating-point number. The result is always non-negative: `exp(0) == 1`, `exp` of any positive value is greater than `1`, and `exp` of any negative value lies in the open interval `(0, 1)`. On every Linux target pperl supports, the underlying type is IEEE 754 binary64, so the range of finite results is roughly `5e-324` up to `1.7976931348623157e+308`. Arguments above about `709.78` overflow to `Inf`; arguments below about `-745` underflow to `0.0`. ## Examples Get *e* itself, and verify the round-trip against [`log`](log): ```perl my $e = exp(1); # 2.718281828459045 printf "%.15f\n", exp(log(42)); # 42.000000000000000 ``` No-argument form reads [`$_`](../perlvar): ```perl for (1, 2, 3) { printf "exp(%d) = %.6f\n", $_, exp; } # exp(1) = 2.718282 # exp(2) = 7.389056 # exp(3) = 20.085537 ``` Compose with other numeric built-ins. The hyperbolic sine, for example, has no dedicated operator but falls out of `exp`: ```perl sub sinh { (exp($_[0]) - exp(-$_[0])) / 2 } printf "%.6f\n", sinh(1); # 1.175201 ``` Overflow at the top of the IEEE 754 range: ```perl print exp(709), "\n"; # 8.21840746949e+307 print exp(710), "\n"; # Inf ``` Underflow at the bottom: ```perl print exp(-745), "\n"; # 5e-324 (smallest subnormal) print exp(-746), "\n"; # 0 ``` ## Edge cases - **Very large positive argument**: Any argument greater than roughly `709.78` produces `Inf`. `Inf` propagates through subsequent arithmetic (`Inf + 1 == Inf`, `Inf - Inf` is `NaN`) and stringifies as `"Inf"`. - **Very negative argument**: Arguments below roughly `-745` underflow to `0.0`. Values in between render as denormal floats with progressively reduced precision. - **`NaN` input**: `exp(NaN)` is `NaN`. `NaN` compares unequal to everything including itself — test with [`Scalar::Util::looks_like_number`](../../Scalar/Util) or `$x != $x` rather than `==`. - **`exp(0)`** is exactly `1`, not a rounded approximation. - **Non-numeric argument**: Perl coerces the argument through its standard string-to-number rules before calling the C `exp(3)` routine. `exp("")` and `exp("abc")` are therefore `exp(0) == 1`, with an `Argument "…" isn't numeric` warning under `use warnings`. A leading-numeric string like `"3.5xyz"` becomes `3.5`, again with a warning. - **[`undef`](undef) argument**: Coerced to `0`, yielding `1`, with an `uninitialized` warning under `use warnings`. - **List context**: `exp` is a unary numeric function. It ignores list context and always returns a single scalar. `my @r = exp(2)` puts one element into `@r`. ## Differences from upstream Fully compatible with upstream Perl 5.42. ## See also - [`log`](log) — natural logarithm, the inverse of `exp`; `exp(log($x)) == $x` for positive `$x` - [`sqrt`](sqrt) — square root; `sqrt($x) == exp(log($x)/2)` for positive `$x` - [`sin`](sin) — sine, the other transcendental unary built-in - [`cos`](cos) — cosine; with [`sin`](sin) and `exp` covers the standard transcendentals available without a module - [`abs`](abs) — absolute value, useful when feeding a signed quantity into [`log`](log) after `exp`-based manipulation - [`$_`](../perlvar) — default argument when `exp` is called without an explicit operand