source: trunk/minix/lib/other/hypot.c@ 9

Last change on this file since 9 was 9, checked in by Mattia Monga, 13 years ago

Minix 3.1.2a

File size: 843 bytes
Line 
1/*
2 * (c) copyright 1988 by the Vrije Universiteit, Amsterdam, The Netherlands.
3 * See the copyright notice in the ACK home directory, in the file "Copyright".
4 *
5 * Author: Ceriel J.H. Jacobs
6 */
7
8#include <math.h>
9
10struct complex {
11 double r,i;
12};
13
14_PROTOTYPE(double hypot, (double x, double y ));
15_PROTOTYPE(double cabs, (struct complex p_compl ));
16
17/* $Header: /cvsup/minix/src/lib/other/hypot.c,v 1.1.1.1 2005/04/21 14:56:27 beng Exp $ */
18
19double
20hypot(x, y)
21double x, y;
22{
23 /* Computes sqrt(x*x+y*y), avoiding overflow */
24
25 if (x < 0) x = -x;
26 if (y < 0) y = -y;
27 if (x > y) {
28 double t = y;
29 y = x;
30 x = t;
31 }
32 /* sqrt(x*x+y*y) = sqrt(y*y*(x*x/(y*y)+1.0)) = y*sqrt(x*x/(y*y)+1.0) */
33 if (y == 0.0) return 0.0;
34 x /= y;
35 return y*sqrt(x*x+1.0);
36}
37
38double
39cabs(p_compl)
40struct complex p_compl;
41{
42 return hypot(p_compl.r, p_compl.i);
43}
Note: See TracBrowser for help on using the repository browser.