1.\" $OpenBSD: hypot.3,v 1.15 2007/05/31 19:19:35 jmc Exp $ 2.\" Copyright (c) 1985, 1991 Regents of the University of California. 3.\" All rights reserved. 4.\" 5.\" Redistribution and use in source and binary forms, with or without 6.\" modification, are permitted provided that the following conditions 7.\" are met: 8.\" 1. Redistributions of source code must retain the above copyright 9.\" notice, this list of conditions and the following disclaimer. 10.\" 2. Redistributions in binary form must reproduce the above copyright 11.\" notice, this list of conditions and the following disclaimer in the 12.\" documentation and/or other materials provided with the distribution. 13.\" 3. Neither the name of the University nor the names of its contributors 14.\" may be used to endorse or promote products derived from this software 15.\" without specific prior written permission. 16.\" 17.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27.\" SUCH DAMAGE. 28.\" 29.\" from: @(#)hypot.3 6.7 (Berkeley) 5/6/91 30.\" 31.Dd $Mdocdate: May 31 2007 $ 32.Dt HYPOT 3 33.Os 34.Sh NAME 35.Nm hypot , 36.Nm hypotf , 37.Nm cabs , 38.Nm cabsf 39.Nd Euclidean distance and complex absolute value functions 40.Sh SYNOPSIS 41.Fd #include <math.h> 42.Ft double 43.Fn hypot "double x" "double y" 44.Ft float 45.Fn hypotf "float x" "float y" 46.Ft double 47.Fn cabs "struct complex { double x; double y; } z" 48.Ft float 49.Fn cabsf "struct complex { float x; float y; } z" 50.Sh DESCRIPTION 51The 52.Fn hypot 53and 54.Fn cabs 55functions 56compute the 57sqrt(x*x+y*y) 58in such a way that underflow will not happen, and overflow 59occurs only if the final result deserves it. 60The 61.Fn hypotf 62and 63.Fn cabsf 64functions are single precision versions of 65.Fn hypot 66and 67.Fn cabs , 68respectively. 69.Pp 70.Fn hypot "\*(If" "v" 71= 72.Fn hypot "v" "\*(If" 73= +\*(If for all 74.Ar v , 75including \*(Na. 76.Sh ERRORS (due to Roundoff, etc.) 77Below 0.97 78.Em ulps . 79Consequently 80.Fn hypot "5.0" "12.0" 81= 13.0 82exactly; 83in general, hypot and cabs return an integer whenever an 84integer might be expected. 85.Pp 86The same cannot be said for the shorter and faster version of hypot 87and cabs that is provided in the comments in cabs.c; its error can 88exceed 1.2 89.Em ulps . 90.Sh NOTES 91As might be expected, 92.Fn hypot "v" "\*(Na" 93and 94.Fn hypot "\*(Na" "v" 95are \*(Na for all 96.Em finite 97.Ar v ; 98with "reserved operand" in place of "\*(Na", the 99same is true on a 100.Tn VAX . 101But programmers on machines other than a 102.Tn VAX 103(it has no \*(If) 104might be surprised at first to discover that 105.Fn hypot "\(+-\*(If" "\*(Na" 106= +\*(If. 107This is intentional; it happens because 108.Fn hypot "\*(If" "v" 109= +\*(If 110for 111.Em all 112.Ar v , 113finite or infinite. 114Hence 115.Fn hypot "\*(If" "v" 116is independent of 117.Ar v . 118Unlike the reserved operand fault on a 119.Tn VAX , 120the 121.Tn IEEE 122\*(Na is designed to 123disappear when it turns out to be irrelevant, as it does in 124.Fn hypot "\*(If" "\*(Na" . 125.Sh SEE ALSO 126.Xr math 3 , 127.Xr sqrt 3 128.Sh HISTORY 129Both a 130.Fn hypot 131function and a 132.Fn cabs 133function 134appeared in 135.At v7 . 136.Sh BUGS 137The 138.Fn cabs 139and 140.Fn cabsf 141functions use structures that are not defined in any header and need to 142be defined by the user. 143As such they cannot be prototyped properly. 144