1*1c7c1e02Sschwarze.\" $OpenBSD: exp.3,v 1.35 2021/06/29 14:47:33 schwarze Exp $ 2f885a527Saaron.\" 3df930be7Sderaadt.\" Copyright (c) 1985, 1991 Regents of the University of California. 4df930be7Sderaadt.\" All rights reserved. 5df930be7Sderaadt.\" 6df930be7Sderaadt.\" Redistribution and use in source and binary forms, with or without 7df930be7Sderaadt.\" modification, are permitted provided that the following conditions 8df930be7Sderaadt.\" are met: 9df930be7Sderaadt.\" 1. Redistributions of source code must retain the above copyright 10df930be7Sderaadt.\" notice, this list of conditions and the following disclaimer. 11df930be7Sderaadt.\" 2. Redistributions in binary form must reproduce the above copyright 12df930be7Sderaadt.\" notice, this list of conditions and the following disclaimer in the 13df930be7Sderaadt.\" documentation and/or other materials provided with the distribution. 146580fee3Smillert.\" 3. Neither the name of the University nor the names of its contributors 15df930be7Sderaadt.\" may be used to endorse or promote products derived from this software 16df930be7Sderaadt.\" without specific prior written permission. 17df930be7Sderaadt.\" 18df930be7Sderaadt.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 19df930be7Sderaadt.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20df930be7Sderaadt.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21df930be7Sderaadt.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 22df930be7Sderaadt.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23df930be7Sderaadt.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24df930be7Sderaadt.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25df930be7Sderaadt.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26df930be7Sderaadt.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27df930be7Sderaadt.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28df930be7Sderaadt.\" SUCH DAMAGE. 29df930be7Sderaadt.\" 30df930be7Sderaadt.\" from: @(#)exp.3 6.12 (Berkeley) 7/31/91 31df930be7Sderaadt.\" 32*1c7c1e02Sschwarze.Dd $Mdocdate: June 29 2021 $ 33df930be7Sderaadt.Dt EXP 3 34f0525d41Stholo.Os 35df930be7Sderaadt.Sh NAME 36df930be7Sderaadt.Nm exp , 378a2f5500Sderaadt.Nm expf , 387ac6a943Smartynas.Nm expl , 39fb2d3daaSmartynas.Nm exp2 , 40fb2d3daaSmartynas.Nm exp2f , 41854ac34eSmartynas.Nm exp2l , 42df930be7Sderaadt.Nm expm1 , 438a2f5500Sderaadt.Nm expm1f , 447ac6a943Smartynas.Nm expm1l , 45df930be7Sderaadt.Nm log , 468a2f5500Sderaadt.Nm logf , 477ac6a943Smartynas.Nm logl , 48f838d36bSjmc.Nm log2 , 49f838d36bSjmc.Nm log2f , 507ac6a943Smartynas.Nm log2l , 51df930be7Sderaadt.Nm log10 , 528a2f5500Sderaadt.Nm log10f , 537ac6a943Smartynas.Nm log10l , 54df930be7Sderaadt.Nm log1p , 558a2f5500Sderaadt.Nm log1pf , 567ac6a943Smartynas.Nm log1pl , 578a2f5500Sderaadt.Nm pow , 587ac6a943Smartynas.Nm powf , 597ac6a943Smartynas.Nm powl 60df930be7Sderaadt.Nd exponential, logarithm, power functions 61df930be7Sderaadt.Sh SYNOPSIS 6286f9d4cdStedu.In math.h 63df930be7Sderaadt.Ft double 64df930be7Sderaadt.Fn exp "double x" 65df930be7Sderaadt.Ft float 66df930be7Sderaadt.Fn expf "float x" 677ac6a943Smartynas.Ft long double 687ac6a943Smartynas.Fn expl "long double x" 69df930be7Sderaadt.Ft double 70fb2d3daaSmartynas.Fn exp2 "double x" 71fb2d3daaSmartynas.Ft float 72fb2d3daaSmartynas.Fn exp2f "float x" 73854ac34eSmartynas.Ft long double 74854ac34eSmartynas.Fn exp2l "long double x" 75fb2d3daaSmartynas.Ft double 76df930be7Sderaadt.Fn expm1 "double x" 77df930be7Sderaadt.Ft float 78df930be7Sderaadt.Fn expm1f "float x" 797ac6a943Smartynas.Ft long double 807ac6a943Smartynas.Fn expm1l "long double x" 81df930be7Sderaadt.Ft double 82df930be7Sderaadt.Fn log "double x" 83df930be7Sderaadt.Ft float 84df930be7Sderaadt.Fn logf "float x" 857ac6a943Smartynas.Ft long double 867ac6a943Smartynas.Fn logl "long double x" 87df930be7Sderaadt.Ft double 88f838d36bSjmc.Fn log2 "double x" 89f838d36bSjmc.Ft float 90f838d36bSjmc.Fn log2f "float x" 917ac6a943Smartynas.Ft long double 927ac6a943Smartynas.Fn log2l "long double x" 93f838d36bSjmc.Ft double 94df930be7Sderaadt.Fn log10 "double x" 95df930be7Sderaadt.Ft float 96df930be7Sderaadt.Fn log10f "float x" 977ac6a943Smartynas.Ft long double 987ac6a943Smartynas.Fn log10l "long double x" 99df930be7Sderaadt.Ft double 100df930be7Sderaadt.Fn log1p "double x" 101df930be7Sderaadt.Ft float 102df930be7Sderaadt.Fn log1pf "float x" 1037ac6a943Smartynas.Ft long double 1047ac6a943Smartynas.Fn log1pl "long double x" 105df930be7Sderaadt.Ft double 106df930be7Sderaadt.Fn pow "double x" "double y" 107df930be7Sderaadt.Ft float 108f0525d41Stholo.Fn powf "float x" "float y" 1097ac6a943Smartynas.Ft long double 1107ac6a943Smartynas.Fn powl "long double x" "long double y" 111df930be7Sderaadt.Sh DESCRIPTION 112df930be7SderaadtThe 113df930be7Sderaadt.Fn exp 114fb2d3daaSmartynasfunction computes the base 115fb2d3daaSmartynas.Ms e 116fb2d3daaSmartynasexponential value of the given argument 117df930be7Sderaadt.Fa x . 118dfec5a15SjasonThe 119dfec5a15Sjason.Fn expf 120dfec5a15Sjasonfunction is a single precision version of 121dfec5a15Sjason.Fn exp . 1227ac6a943SmartynasThe 1237ac6a943Smartynas.Fn expl 1247ac6a943Smartynasfunction is an extended precision version of 1257ac6a943Smartynas.Fn exp . 126df930be7Sderaadt.Pp 127df930be7SderaadtThe 128fb2d3daaSmartynas.Fn exp2 129fb2d3daaSmartynasfunction computes the base 2 exponential of the given argument 130fb2d3daaSmartynas.Fa x . 131fb2d3daaSmartynasThe 132fb2d3daaSmartynas.Fn exp2f 133fb2d3daaSmartynasfunction is a single precision version of 134fb2d3daaSmartynas.Fn exp2 . 135854ac34eSmartynasThe 136854ac34eSmartynas.Fn exp2l 137854ac34eSmartynasfunction is an extended precision version of 138854ac34eSmartynas.Fn exp2 . 139fb2d3daaSmartynas.Pp 140fb2d3daaSmartynasThe 141df930be7Sderaadt.Fn expm1 142164e647eStbfunction computes the value exp(x) \(mi 1 accurately even for tiny argument 143df930be7Sderaadt.Fa x . 144dfec5a15SjasonThe 145dfec5a15Sjason.Fn expm1f 146dfec5a15Sjasonfunction is a single precision version of 147dfec5a15Sjason.Fn expm1 . 1487ac6a943SmartynasThe 1497ac6a943Smartynas.Fn expm1l 1507ac6a943Smartynasfunction is an extended precision version of 1517ac6a943Smartynas.Fn expm1 . 152df930be7Sderaadt.Pp 153df930be7SderaadtThe 154df930be7Sderaadt.Fn log 155df930be7Sderaadtfunction computes the value of the natural logarithm of argument 156df930be7Sderaadt.Fa x . 157dfec5a15SjasonThe 158dfec5a15Sjason.Fn logf 159dfec5a15Sjasonfunction is a single precision version of 160dfec5a15Sjason.Fn log . 1617ac6a943SmartynasThe 1627ac6a943Smartynas.Fn logl 1637ac6a943Smartynasfunction is an extended precision version of 1647ac6a943Smartynas.Fn log . 165df930be7Sderaadt.Pp 166df930be7SderaadtThe 167f838d36bSjmc.Fn log2 168f838d36bSjmcfunction computes the value of the logarithm of argument 169f838d36bSjmc.Fa x 170f838d36bSjmcto base 2. 171f838d36bSjmcThe 172f838d36bSjmc.Fn log2f 173f838d36bSjmcfunction is a single precision version of 174f838d36bSjmc.Fn log2 . 1757ac6a943SmartynasThe 1767ac6a943Smartynas.Fn log2l 1777ac6a943Smartynasfunction is an extended precision version of 1787ac6a943Smartynas.Fn log2 . 179f838d36bSjmc.Pp 180f838d36bSjmcThe 18160f2437eSjmc.Fn log10 18260f2437eSjmcfunction computes the value of the logarithm of argument 18360f2437eSjmc.Fa x 18460f2437eSjmcto base 10. 18560f2437eSjmcThe 18660f2437eSjmc.Fn log10f 18760f2437eSjmcfunction is a single precision version of 18860f2437eSjmc.Fn log10 . 1897ac6a943SmartynasThe 1907ac6a943Smartynas.Fn log10l 1917ac6a943Smartynasfunction is an extended precision version of 1927ac6a943Smartynas.Fn log10 . 19360f2437eSjmc.Pp 19460f2437eSjmcThe 195df930be7Sderaadt.Fn log1p 196df930be7Sderaadtfunction computes 197df930be7Sderaadtthe value of log(1 + x) accurately even for tiny argument 198df930be7Sderaadt.Fa x . 199dfec5a15SjasonThe 200dfec5a15Sjason.Fn log1pf 201dfec5a15Sjasonfunction is a single precision version of 202dfec5a15Sjason.Fn log1p . 2037ac6a943SmartynasThe 2047ac6a943Smartynas.Fn log1pl 2057ac6a943Smartynasfunction is an extended precision version of 2067ac6a943Smartynas.Fn log1p . 207df930be7Sderaadt.Pp 208df930be7SderaadtThe 209df930be7Sderaadt.Fn pow 2102fc7c3e3Sjasonfunction computes the value of 211*1c7c1e02Sschwarze.Fa x 212df930be7Sderaadtto the exponent 213*1c7c1e02Sschwarze.Fa y . 214dfec5a15SjasonThe 215dfec5a15Sjason.Fn powf 216dfec5a15Sjasonfunction is a single precision version of 217dfec5a15Sjason.Fn pow . 2187ac6a943SmartynasThe 2197ac6a943Smartynas.Fn powl 2207ac6a943Smartynasfunction is an extended precision version of 2217ac6a943Smartynas.Fn pow . 222df930be7Sderaadt.Sh RETURN VALUES 223df930be7SderaadtThese functions will return the appropriate computation unless an error 224df930be7Sderaadtoccurs or an argument is out of range. 225df930be7SderaadtThe functions 226df930be7Sderaadt.Fn exp , 227df930be7Sderaadt.Fn expm1 228df930be7Sderaadtand 229df930be7Sderaadt.Fn pow 230bf886a7eStbdetect if the computed value will overflow 231bf886a7eStband set the global variable 23254239ef9Sjmc.Va errno 23354239ef9Sjmcto 234bf886a7eStb.Er ERANGE . 235df930be7SderaadtThe function 236df930be7Sderaadt.Fn pow x y 237df930be7Sderaadtchecks to see if 238df930be7Sderaadt.Fa x 239df930be7Sderaadt< 0 and 240df930be7Sderaadt.Fa y 241df930be7Sderaadtis not an integer, in the event this is true, 242df930be7Sderaadtthe global variable 243df930be7Sderaadt.Va errno 244df930be7Sderaadtis set to 245bf886a7eStb.Er EDOM . 2469eb52f1bSjmc.Sh ERRORS (due to Roundoff etc.) 2479eb52f1bSjmcexp(x), log(x), expm1(x) and log1p(x) are accurate to within 2489eb52f1bSjmcan 2499eb52f1bSjmc.Em ulp , 2509eb52f1bSjmcand log10(x) to within about 2 2519eb52f1bSjmc.Em ulps ; 2529eb52f1bSjmcan 2539eb52f1bSjmc.Em ulp 2549eb52f1bSjmcis one 2559eb52f1bSjmc.Em Unit 2569eb52f1bSjmcin the 2579eb52f1bSjmc.Em Last 2589eb52f1bSjmc.Em Place . 2599eb52f1bSjmcThe error in 2609eb52f1bSjmc.Fn pow x y 2619eb52f1bSjmcis below about 2 2629eb52f1bSjmc.Em ulps 2639eb52f1bSjmcwhen its 2649eb52f1bSjmcmagnitude is moderate, but increases as 2659eb52f1bSjmc.Fn pow x y 2669eb52f1bSjmcapproaches 2679eb52f1bSjmcthe over/underflow thresholds until almost as many bits could be 2689eb52f1bSjmclost as are occupied by the floating\-point format's exponent 269bf886a7eStbfield; that is 11 bits for IEEE 754 Double. 2709eb52f1bSjmcNo such drastic loss has been exposed by testing; the worst 271bf886a7eStberrors observed have been below 300 2729eb52f1bSjmc.Em ulps 27312bc6069Sschwarzefor IEEE 754 Double. 2749eb52f1bSjmcModerate values of 2759eb52f1bSjmc.Fn pow 2769eb52f1bSjmcare accurate enough that 2779eb52f1bSjmc.Fn pow integer integer 278bf886a7eStbis exact until it is bigger than 2**53 for IEEE 754. 279df930be7Sderaadt.Sh NOTES 280164e647eStbThe functions exp(x) \(mi 1 and log(1 + x) are called 28112bc6069Sschwarzeexpm1 and logp1 in BASIC on the Hewlett\-Packard HP-71B 28212bc6069Sschwarzeand APPLE Macintosh, EXP1 and LN1 in Pascal, exp1 and log1 in C 28312bc6069Sschwarzeon APPLE Macintoshes, where they have been provided to make 284164e647eStbsure financial calculations of ((1 + x)**n \(mi 1) / x, namely 2859f1be2b6Stbexpm1(n * log1p(x)) / x, will be accurate when x is tiny. 286df930be7SderaadtThey also provide accurate inverse hyperbolic functions. 287df930be7Sderaadt.Pp 288df930be7SderaadtThe function 289df930be7Sderaadt.Fn pow x 0 2909f1be2b6Stbreturns x**0 = 1 for all x including x = 0 and infinity. 29112bc6069SschwarzePrevious implementations of 29212bc6069Sschwarze.Fn pow 29312bc6069Sschwarzemay have defined x**0 to be undefined in some or all of these cases. 29454239ef9SjmcHere are reasons for returning x**0 = 1 always: 295df930be7Sderaadt.Bl -enum -width indent 296df930be7Sderaadt.It 297df930be7SderaadtAny program that already tests whether x is zero (or 2989f1be2b6Stbinfinite or NaN) before computing x**0 cannot care 29954239ef9Sjmcwhether 0**0 = 1 or not. 30054239ef9SjmcAny program that depends upon 0**0 to be invalid is dubious anyway since that 301df930be7Sderaadtexpression's meaning and, if invalid, its consequences 302df930be7Sderaadtvary from one computer system to another. 303df930be7Sderaadt.It 30442671979SaaronSome Algebra texts (e.g., Sigler's) define x**0 = 1 for 305df930be7Sderaadtall x, including x = 0. 306df930be7SderaadtThis is compatible with the convention that accepts a[0] 307df930be7Sderaadtas the value of polynomial 308df930be7Sderaadt.Bd -literal -offset indent 3099f1be2b6Stbp(x) = a[0]*x**0 + a[1]*x**1 + a[2]*x**2 +...+ a[n]*x**n 310df930be7Sderaadt.Ed 311df930be7Sderaadt.Pp 3129f1be2b6Stbat x = 0 rather than reject a[0]*0**0 as invalid. 313df930be7Sderaadt.It 314df930be7SderaadtAnalysts will accept 0**0 = 1 despite that x**y can 315df930be7Sderaadtapproach anything or nothing as x and y approach 0 316df930be7Sderaadtindependently. 317df930be7SderaadtThe reason for setting 0**0 = 1 anyway is this: 318df930be7Sderaadt.Bd -filled -offset indent 319df930be7SderaadtIf x(z) and y(z) are 320df930be7Sderaadt.Em any 321df930be7Sderaadtfunctions analytic (expandable 322df930be7Sderaadtin power series) in z around z = 0, and if there 323df930be7Sderaadtx(0) = y(0) = 0, then x(z)**y(z) \(-> 1 as z \(-> 0. 324df930be7Sderaadt.Ed 325df930be7Sderaadt.It 3269f1be2b6StbIf 0**0 = 1, then infinity**0 = 1/0**0 = 1 too; and 3279f1be2b6Stbthen NaN**0 = 1 too because x**0 = 1 for all finite 328df930be7Sderaadtand infinite x, i.e., independently of x. 329df930be7Sderaadt.El 330df930be7Sderaadt.Sh SEE ALSO 3319f1be2b6Stb.Xr fpclassify 3 , 332f1ae3669Sjmc.Xr ilogb 3 333df930be7Sderaadt.Sh HISTORY 334df930be7SderaadtThe 33523fbb714Sschwarze.Fn exp 33623fbb714Sschwarzeand 33723fbb714Sschwarze.Fn log 33823fbb714Sschwarzefunctions first appeared in 33923fbb714Sschwarze.At v1 ; 34023fbb714Sschwarze.Fn pow 34123fbb714Sschwarzein 34223fbb714Sschwarze.At v3 ; 34323fbb714Sschwarze.Fn log10 34423fbb714Sschwarzein 34523fbb714Sschwarze.At v7 ; 346df930be7Sderaadt.Fn log1p 347df930be7Sderaadtand 348df930be7Sderaadt.Fn expm1 34923fbb714Sschwarzein 350df930be7Sderaadt.Bx 4.3 . 351