1.\" $OpenBSD: exp.3,v 1.35 2021/06/29 14:47:33 schwarze Exp $ 2.\" 3.\" Copyright (c) 1985, 1991 Regents of the University of California. 4.\" All rights reserved. 5.\" 6.\" Redistribution and use in source and binary forms, with or without 7.\" modification, are permitted provided that the following conditions 8.\" are met: 9.\" 1. Redistributions of source code must retain the above copyright 10.\" notice, this list of conditions and the following disclaimer. 11.\" 2. Redistributions in binary form must reproduce the above copyright 12.\" notice, this list of conditions and the following disclaimer in the 13.\" documentation and/or other materials provided with the distribution. 14.\" 3. Neither the name of the University nor the names of its contributors 15.\" may be used to endorse or promote products derived from this software 16.\" without specific prior written permission. 17.\" 18.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 19.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 22.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28.\" SUCH DAMAGE. 29.\" 30.\" from: @(#)exp.3 6.12 (Berkeley) 7/31/91 31.\" 32.Dd $Mdocdate: June 29 2021 $ 33.Dt EXP 3 34.Os 35.Sh NAME 36.Nm exp , 37.Nm expf , 38.Nm expl , 39.Nm exp2 , 40.Nm exp2f , 41.Nm exp2l , 42.Nm expm1 , 43.Nm expm1f , 44.Nm expm1l , 45.Nm log , 46.Nm logf , 47.Nm logl , 48.Nm log2 , 49.Nm log2f , 50.Nm log2l , 51.Nm log10 , 52.Nm log10f , 53.Nm log10l , 54.Nm log1p , 55.Nm log1pf , 56.Nm log1pl , 57.Nm pow , 58.Nm powf , 59.Nm powl 60.Nd exponential, logarithm, power functions 61.Sh SYNOPSIS 62.In math.h 63.Ft double 64.Fn exp "double x" 65.Ft float 66.Fn expf "float x" 67.Ft long double 68.Fn expl "long double x" 69.Ft double 70.Fn exp2 "double x" 71.Ft float 72.Fn exp2f "float x" 73.Ft long double 74.Fn exp2l "long double x" 75.Ft double 76.Fn expm1 "double x" 77.Ft float 78.Fn expm1f "float x" 79.Ft long double 80.Fn expm1l "long double x" 81.Ft double 82.Fn log "double x" 83.Ft float 84.Fn logf "float x" 85.Ft long double 86.Fn logl "long double x" 87.Ft double 88.Fn log2 "double x" 89.Ft float 90.Fn log2f "float x" 91.Ft long double 92.Fn log2l "long double x" 93.Ft double 94.Fn log10 "double x" 95.Ft float 96.Fn log10f "float x" 97.Ft long double 98.Fn log10l "long double x" 99.Ft double 100.Fn log1p "double x" 101.Ft float 102.Fn log1pf "float x" 103.Ft long double 104.Fn log1pl "long double x" 105.Ft double 106.Fn pow "double x" "double y" 107.Ft float 108.Fn powf "float x" "float y" 109.Ft long double 110.Fn powl "long double x" "long double y" 111.Sh DESCRIPTION 112The 113.Fn exp 114function computes the base 115.Ms e 116exponential value of the given argument 117.Fa x . 118The 119.Fn expf 120function is a single precision version of 121.Fn exp . 122The 123.Fn expl 124function is an extended precision version of 125.Fn exp . 126.Pp 127The 128.Fn exp2 129function computes the base 2 exponential of the given argument 130.Fa x . 131The 132.Fn exp2f 133function is a single precision version of 134.Fn exp2 . 135The 136.Fn exp2l 137function is an extended precision version of 138.Fn exp2 . 139.Pp 140The 141.Fn expm1 142function computes the value exp(x) \(mi 1 accurately even for tiny argument 143.Fa x . 144The 145.Fn expm1f 146function is a single precision version of 147.Fn expm1 . 148The 149.Fn expm1l 150function is an extended precision version of 151.Fn expm1 . 152.Pp 153The 154.Fn log 155function computes the value of the natural logarithm of argument 156.Fa x . 157The 158.Fn logf 159function is a single precision version of 160.Fn log . 161The 162.Fn logl 163function is an extended precision version of 164.Fn log . 165.Pp 166The 167.Fn log2 168function computes the value of the logarithm of argument 169.Fa x 170to base 2. 171The 172.Fn log2f 173function is a single precision version of 174.Fn log2 . 175The 176.Fn log2l 177function is an extended precision version of 178.Fn log2 . 179.Pp 180The 181.Fn log10 182function computes the value of the logarithm of argument 183.Fa x 184to base 10. 185The 186.Fn log10f 187function is a single precision version of 188.Fn log10 . 189The 190.Fn log10l 191function is an extended precision version of 192.Fn log10 . 193.Pp 194The 195.Fn log1p 196function computes 197the value of log(1 + x) accurately even for tiny argument 198.Fa x . 199The 200.Fn log1pf 201function is a single precision version of 202.Fn log1p . 203The 204.Fn log1pl 205function is an extended precision version of 206.Fn log1p . 207.Pp 208The 209.Fn pow 210function computes the value of 211.Fa x 212to the exponent 213.Fa y . 214The 215.Fn powf 216function is a single precision version of 217.Fn pow . 218The 219.Fn powl 220function is an extended precision version of 221.Fn pow . 222.Sh RETURN VALUES 223These functions will return the appropriate computation unless an error 224occurs or an argument is out of range. 225The functions 226.Fn exp , 227.Fn expm1 228and 229.Fn pow 230detect if the computed value will overflow 231and set the global variable 232.Va errno 233to 234.Er ERANGE . 235The function 236.Fn pow x y 237checks to see if 238.Fa x 239< 0 and 240.Fa y 241is not an integer, in the event this is true, 242the global variable 243.Va errno 244is set to 245.Er EDOM . 246.Sh ERRORS (due to Roundoff etc.) 247exp(x), log(x), expm1(x) and log1p(x) are accurate to within 248an 249.Em ulp , 250and log10(x) to within about 2 251.Em ulps ; 252an 253.Em ulp 254is one 255.Em Unit 256in the 257.Em Last 258.Em Place . 259The error in 260.Fn pow x y 261is below about 2 262.Em ulps 263when its 264magnitude is moderate, but increases as 265.Fn pow x y 266approaches 267the over/underflow thresholds until almost as many bits could be 268lost as are occupied by the floating\-point format's exponent 269field; that is 11 bits for IEEE 754 Double. 270No such drastic loss has been exposed by testing; the worst 271errors observed have been below 300 272.Em ulps 273for IEEE 754 Double. 274Moderate values of 275.Fn pow 276are accurate enough that 277.Fn pow integer integer 278is exact until it is bigger than 2**53 for IEEE 754. 279.Sh NOTES 280The functions exp(x) \(mi 1 and log(1 + x) are called 281expm1 and logp1 in BASIC on the Hewlett\-Packard HP-71B 282and APPLE Macintosh, EXP1 and LN1 in Pascal, exp1 and log1 in C 283on APPLE Macintoshes, where they have been provided to make 284sure financial calculations of ((1 + x)**n \(mi 1) / x, namely 285expm1(n * log1p(x)) / x, will be accurate when x is tiny. 286They also provide accurate inverse hyperbolic functions. 287.Pp 288The function 289.Fn pow x 0 290returns x**0 = 1 for all x including x = 0 and infinity. 291Previous implementations of 292.Fn pow 293may have defined x**0 to be undefined in some or all of these cases. 294Here are reasons for returning x**0 = 1 always: 295.Bl -enum -width indent 296.It 297Any program that already tests whether x is zero (or 298infinite or NaN) before computing x**0 cannot care 299whether 0**0 = 1 or not. 300Any program that depends upon 0**0 to be invalid is dubious anyway since that 301expression's meaning and, if invalid, its consequences 302vary from one computer system to another. 303.It 304Some Algebra texts (e.g., Sigler's) define x**0 = 1 for 305all x, including x = 0. 306This is compatible with the convention that accepts a[0] 307as the value of polynomial 308.Bd -literal -offset indent 309p(x) = a[0]*x**0 + a[1]*x**1 + a[2]*x**2 +...+ a[n]*x**n 310.Ed 311.Pp 312at x = 0 rather than reject a[0]*0**0 as invalid. 313.It 314Analysts will accept 0**0 = 1 despite that x**y can 315approach anything or nothing as x and y approach 0 316independently. 317The reason for setting 0**0 = 1 anyway is this: 318.Bd -filled -offset indent 319If x(z) and y(z) are 320.Em any 321functions analytic (expandable 322in power series) in z around z = 0, and if there 323x(0) = y(0) = 0, then x(z)**y(z) \(-> 1 as z \(-> 0. 324.Ed 325.It 326If 0**0 = 1, then infinity**0 = 1/0**0 = 1 too; and 327then NaN**0 = 1 too because x**0 = 1 for all finite 328and infinite x, i.e., independently of x. 329.El 330.Sh SEE ALSO 331.Xr fpclassify 3 , 332.Xr ilogb 3 333.Sh HISTORY 334The 335.Fn exp 336and 337.Fn log 338functions first appeared in 339.At v1 ; 340.Fn pow 341in 342.At v3 ; 343.Fn log10 344in 345.At v7 ; 346.Fn log1p 347and 348.Fn expm1 349in 350.Bx 4.3 . 351