hypot.c

来自「MINIX系统源码」· C语言 代码 · 共 44 行

C
44
字号
/*
 * (c) copyright 1988 by the Vrije Universiteit, Amsterdam, The Netherlands.
 * See the copyright notice in the ACK home directory, in the file "Copyright".
 *
 * Author: Ceriel J.H. Jacobs
 */

#include <math.h>

struct complex {
	double r,i;
};

_PROTOTYPE(double hypot, (double x, double y ));
_PROTOTYPE(double cabs, (struct complex p_compl ));

/* $Header: hypot.c,v 1.1 91/02/26 18:08:08 ceriel Exp $ */

double
hypot(x, y)
double x, y;
{
	/*	Computes sqrt(x*x+y*y), avoiding overflow */

	if (x < 0) x = -x;
	if (y < 0) y = -y;
	if (x > y) {
		double t = y;
		y = x;
		x = t;
	}
	/* sqrt(x*x+y*y) = sqrt(y*y*(x*x/(y*y)+1.0)) = y*sqrt(x*x/(y*y)+1.0) */
	if (y == 0.0) return 0.0;
	x /= y;
	return y*sqrt(x*x+1.0);
}

double
cabs(p_compl)
struct complex p_compl;
{
	return hypot(p_compl.r, p_compl.i);
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?