From c5e0bcde043799140604f0d71aa685bad08b3805 Mon Sep 17 00:00:00 2001 From: rlar Date: Fri, 5 Dec 2014 20:08:02 +0100 Subject: [PATCH] cleanup using `hypot()' --- src/ciderlib/twod/twoaval.c | 4 ++-- src/ciderlib/twod/twodopng.c | 2 +- src/frontend/com_measure2.c | 7 +++---- src/frontend/fourier.c | 2 +- src/frontend/plotting/clip.c | 8 ++++---- src/frontend/plotting/grid.c | 12 ++++++------ src/frontend/plotting/plotit.c | 2 +- src/frontend/plotting/x11.c | 2 +- src/frontend/wdisp/windisp.c | 2 +- src/include/ngspice/complex.h | 8 ++++---- src/include/ngspice/ngspice.h | 1 + src/maths/sparse/spdefs.h | 2 +- src/spicelib/analysis/dcpss.c | 2 +- src/xspice/icm/analog/s_xfer/cfunc.mod | 4 ++-- 14 files changed, 29 insertions(+), 29 deletions(-) diff --git a/src/ciderlib/twod/twoaval.c b/src/ciderlib/twod/twoaval.c index 11eb6c96c..57e543f9e 100644 --- a/src/ciderlib/twod/twoaval.c +++ b/src/ciderlib/twod/twoaval.c @@ -186,7 +186,7 @@ TWOavalanche(TWOelem *pElem, TWOnode *pNode) */ /* now calculate the avalanche generation rate */ - current = sqrt( jnx * jnx + jny * jny ); + current = hypot(jnx, jny); if ( current != 0.0 ) { eField = (enx * jnx + eny * jny) / current; if ( (eField > 0) && ( info->bii[ELEC] / eField <= 80.0) ) { @@ -194,7 +194,7 @@ TWOavalanche(TWOelem *pElem, TWOnode *pNode) * exp( - info->bii[ELEC] / eField ); } } - current = sqrt( jpx * jpx + jpy * jpy ); + current = hypot(jpx, jpy); if ( current != 0.0 ) { eField = (epx * jpx + epy * jpy) / current; if ( eField > eiip2 ) { diff --git a/src/ciderlib/twod/twodopng.c b/src/ciderlib/twod/twodopng.c index c89cb5b5b..9b3ceefde 100644 --- a/src/ciderlib/twod/twodopng.c +++ b/src/ciderlib/twod/twodopng.c @@ -65,7 +65,7 @@ TWOdopingValue(DOPprofile *pProfile, DOPtable *pTable, double x, argL = argY / pProfile->LAT_RATIO; } if ( pProfile->rotate ) { - argP = sqrt(argP*argP + argL*argL); + argP = hypot(argP, argL); argL = 0.0; } diff --git a/src/frontend/com_measure2.c b/src/frontend/com_measure2.c index d8c01ea6c..da59341fd 100644 --- a/src/frontend/com_measure2.c +++ b/src/frontend/com_measure2.c @@ -125,13 +125,13 @@ get_value( int idx /*in: index of vector value to be read out */ ) { - double ar, bi, tt; + double ar, bi; ar = values->v_compdata[idx].cx_real; bi = values->v_compdata[idx].cx_imag; if ((meas->m_vectype == 'm') || (meas->m_vectype == 'M')) { - return sqrt(ar*ar + bi*bi); /* magnitude */ + return hypot(ar, bi); /* magnitude */ } else if ((meas->m_vectype == 'r') || (meas->m_vectype == 'R')) { return ar; /* real value */ } else if ((meas->m_vectype == 'i') || (meas->m_vectype == 'I')) { @@ -139,8 +139,7 @@ get_value( } else if ((meas->m_vectype == 'p') || (meas->m_vectype == 'P')) { return radtodeg(atan2(bi, ar)); /* phase (in degrees) */ } else if ((meas->m_vectype == 'd') || (meas->m_vectype == 'D')) { - tt = sqrt(ar*ar + bi*bi); /* dB of magnitude */ - return 20.0 * log10(tt); + return 20.0 * log10(hypot(ar, bi)); /* dB of magnitude */ } else { return ar; /* default: real value */ } diff --git a/src/frontend/fourier.c b/src/frontend/fourier.c index d07a7a07c..285c4198c 100644 --- a/src/frontend/fourier.c +++ b/src/frontend/fourier.c @@ -337,7 +337,7 @@ CKTfour(int ndata, /* number of entries in the Time and tmp = Mag[i] * 2.0 / ndata; Phase[i] *= 2.0 / ndata; Freq[i] = i * FundFreq; - Mag[i] = sqrt(tmp*tmp + Phase[i]*Phase[i]); + Mag[i] = hypot(tmp, Phase[i]); Phase[i] = atan2(Phase[i], tmp) * 180.0/M_PI; nMag[i] = Mag[i] / Mag[1]; nPhase[i] = Phase[i] - Phase[1]; diff --git a/src/frontend/plotting/clip.c b/src/frontend/plotting/clip.c index 82310e3cc..4528a82b1 100644 --- a/src/frontend/plotting/clip.c +++ b/src/frontend/plotting/clip.c @@ -152,9 +152,9 @@ clip_to_circle(int *x1, int *y1, int *x2, int *y2, int cx, int cy, int rad) } /* Figure out the distances between the points */ - a = sqrt((double) ((*x1 - cx) * (*x1 - cx) + (*y1 - cy) * (*y1 - cy))); - b = sqrt((double) ((*x2 - cx) * (*x2 - cx) + (*y2 - cy) * (*y2 - cy))); - c = sqrt((double) ((*x1 - *x2) * (*x1 - *x2) + (*y1 - *y2) * (*y1 - *y2))); + a = hypot(*x1 - cx, *y1 - cy); + b = hypot(*x2 - cx, *y2 - cy); + c = hypot(*x1 - *x2, *y1 - *y2); /* We have three cases now -- either the midpoint of the line is * closest to the origon, or point 1 or point 2 is. Actually the @@ -164,7 +164,7 @@ clip_to_circle(int *x1, int *y1, int *x2, int *y2, int cx, int cy, int rad) */ tx = (*x1 + *x2) / 2; ty = (*y1 + *y2) / 2; - dt = sqrt((double) ((tx - cx) * (tx - cx) + (ty - cy) * (ty - cy))); + dt = hypot(tx - cx, ty - cy); if ((dt < a) && (dt < b)) { /* This is wierd -- round-off errors I guess. */ tt = (a * a + c * c - b * b) / (2 * a * c); diff --git a/src/frontend/plotting/grid.c b/src/frontend/plotting/grid.c index 41a5ddc6d..c7e5c0710 100644 --- a/src/frontend/plotting/grid.c +++ b/src/frontend/plotting/grid.c @@ -778,7 +778,7 @@ polargrid(GRAPH *graph) /* Figure out the minimum and maximum radii we're dealing with. */ mx = (graph->data.xmin + graph->data.xmax) / 2; my = (graph->data.ymin + graph->data.ymax) / 2; - d = sqrt(mx * mx + my * my); + d = hypot(mx, my); maxrad = d + (graph->data.xmax - graph->data.xmin) / 2; minrad = d - (graph->data.xmax - graph->data.xmin) / 2; @@ -862,7 +862,7 @@ drawpolargrid(GRAPH *graph) /* The distance from the center of the plotting area to the center of * the logical area. */ - dist = (int)sqrt((double) (relcx * relcx + relcy * relcy)); + dist = (int)hypot(relcx, relcy); SetLinestyle(0); DevDrawArc(graph->grid.xaxis.circular.center, @@ -987,7 +987,7 @@ adddeglabel(GRAPH *graph, int deg, int x, int y, int cx, int cy, int lx, int ly) int d, w, h; double angle; - if (sqrt((double) (x - cx) * (x - cx) + (y - cy) * (y - cy)) < MINDIST) + if (hypot(x - cx, y - cy) < MINDIST) return; (void) sprintf(buf, "%d", deg); w = graph->fontwidth * (int) (strlen(buf) + 1); @@ -1124,7 +1124,7 @@ drawsmithgrid(GRAPH *graph) /* Figure out the minimum and maximum radii we're dealing with. */ mx = (graph->datawindow.xmin + graph->datawindow.xmax) / 2; my = (graph->datawindow.ymin + graph->datawindow.ymax) / 2; - d = sqrt(mx * mx + my * my); + d = hypot(mx, my); maxrad = d + (graph->datawindow.xmax - graph->datawindow.xmin) / 2; mag = (int)floor(mylog10(maxrad)); @@ -1419,7 +1419,7 @@ cliparc(double cx, double cy, double rad, double start, double end, int iclipx, cliprad = (double) icliprad; x = cx - clipx; y = cy - clipy; - dist = sqrt((double) (x * x + y * y)); + dist = hypot(x, y); if (!rad || !cliprad) return (-1); @@ -1469,7 +1469,7 @@ cliparc(double cx, double cy, double rad, double start, double end, int iclipx, tx = cos(start) * rad + x; ty = sin(start) * rad + y; - d = sqrt((double) tx * tx + ty * ty); + d = hypot(tx, ty); in = (d > cliprad) ? FALSE : TRUE; /* Now begin with start. If the point is in, draw to either end, a1, diff --git a/src/frontend/plotting/plotit.c b/src/frontend/plotting/plotit.c index e32bfb6be..a6ead9536 100644 --- a/src/frontend/plotting/plotit.c +++ b/src/frontend/plotting/plotit.c @@ -876,7 +876,7 @@ plotit(wordlist *wl, char *hcopy, char *devname) * is outside the drawing area so I'll stay as the maximum size of the hypotenuse of * the complex value */ - rad = sqrt(mx * mx + my * my); + rad = hypot(mx, my); xlims[0] = - rad; xlims[1] = rad; ylims[0] = - rad; diff --git a/src/frontend/plotting/x11.c b/src/frontend/plotting/x11.c index 4a02600cf..3497a7b0f 100644 --- a/src/frontend/plotting/x11.c +++ b/src/frontend/plotting/x11.c @@ -726,7 +726,7 @@ slopelocation(GRAPH *graph, int x0, int y0) { angle = RAD_TO_DEG * atan2(fy0, fx0); fprintf(stdout, "r0 = %g, a0 = %g\n", - sqrt(fx0*fx0 + fy0*fy0), + hypot(fx0, fy0), (angle>0)?angle:360.0+angle); } diff --git a/src/frontend/wdisp/windisp.c b/src/frontend/wdisp/windisp.c index c4abeed79..a5541c17a 100644 --- a/src/frontend/wdisp/windisp.c +++ b/src/frontend/wdisp/windisp.c @@ -434,7 +434,7 @@ LRESULT CALLBACK PlotWindowProc( HWND hwnd, { angle = RAD_TO_DEG * atan2( fy0, fx0 ); fprintf(stdout, "r0 = %g, a0 = %g\n", - sqrt( fx0*fx0 + fy0*fy0 ), + hypot(fx0, fy0), (angle>0)?angle:360.0+angle); } } else { diff --git a/src/include/ngspice/complex.h b/src/include/ngspice/complex.h index 750ecccd9..307663d34 100644 --- a/src/include/ngspice/complex.h +++ b/src/include/ngspice/complex.h @@ -71,7 +71,7 @@ typedef struct { /* Some defines used mainly in cmath.c. */ #define FTEcabs(d) (((d) < 0.0) ? - (d) : (d)) #define cph(c) (atan2(imagpart(c), (realpart(c)))) -#define cmag(c) (sqrt(imagpart(c) * imagpart(c) + realpart(c) * realpart(c))) +#define cmag(c) (hypot(realpart(c), imagpart(c))) #define radtodeg(c) (cx_degrees ? ((c) / 3.14159265358979323846 * 180) : (c)) #define degtorad(c) (cx_degrees ? ((c) * 3.14159265358979323846 / 180) : (c)) #define rcheck(cond, name) if (!(cond)) { \ @@ -157,7 +157,7 @@ typedef struct { (A).imag = 0.0; \ } \ } else { \ - _mag = sqrt((A).real * (A).real + (A).imag * (A).imag); \ + _mag = hypot((A).real, (A).imag); \ _a = (_mag - (A).real) / 2.0; \ if (_a <= 0.0) { \ (A).real = sqrt(_mag); \ @@ -225,7 +225,7 @@ typedef struct { * The magnitude of the complex number */ -#define C_ABS(A) (sqrt((A).real * (A.real) + (A.imag * A.imag))) +#define C_ABS(A) (hypot((A).real, (A).imag)) /* * Standard arithmetic between complex numbers @@ -334,7 +334,7 @@ typedef struct { #define CMPLX_INF_NORM(a) (MAX (ABS((a).real),ABS((a).imag))) /* Macro function that returns the magnitude (L-2 norm) of a complex number. */ -#define CMPLX_2_NORM(a) (sqrt((a).real*(a).real + (a).imag*(a).imag)) +#define CMPLX_2_NORM(a) (hypot((a).real, (a).imag)) /* Macro function that performs complex addition. */ #define CMPLX_ADD(to,from_a,from_b) \ diff --git a/src/include/ngspice/ngspice.h b/src/include/ngspice/ngspice.h index ad7d37e8d..d2c602eaf 100644 --- a/src/include/ngspice/ngspice.h +++ b/src/include/ngspice/ngspice.h @@ -170,6 +170,7 @@ extern double x_asinh(double); extern double x_acosh(double); #define atanh x_atanh extern double x_atanh(double); +#define hypot _hypot #endif #define strdup _strdup #define unlink _unlink diff --git a/src/maths/sparse/spdefs.h b/src/maths/sparse/spdefs.h index fd5237ca7..f88255a3f 100644 --- a/src/maths/sparse/spdefs.h +++ b/src/maths/sparse/spdefs.h @@ -145,7 +145,7 @@ typedef struct #define CMPLX_INF_NORM(a) (MAX (ABS((a).Real),ABS((a).Imag))) /* Macro function that returns the magnitude (L-2 norm) of a complex number. */ -#define CMPLX_2_NORM(a) (sqrt((a).Real*(a).Real + (a).Imag*(a).Imag)) +#define CMPLX_2_NORM(a) (hypot((a).Real, (a).Imag)) /* Macro function that performs complex addition. */ #define CMPLX_ADD(to,from_a,from_b) \ diff --git a/src/spicelib/analysis/dcpss.c b/src/spicelib/analysis/dcpss.c index 190522a6c..bc3b9d8b6 100644 --- a/src/spicelib/analysis/dcpss.c +++ b/src/spicelib/analysis/dcpss.c @@ -1605,7 +1605,7 @@ DFT tmp = Mag [i] * 2.0 / (double)ndata; Phase [i] *= 2.0 / (double)ndata; Freq [i] = i * FundFreq; - Mag [i] = sqrt (tmp * tmp + Phase [i] * Phase [i]); + Mag [i] = hypot (tmp, Phase [i]); Phase [i] = atan2 (Phase [i], tmp) * 180.0 / M_PI; nMag [i] = Mag [i] / Mag [1]; nPhase [i] = Phase [i] - Phase [1]; diff --git a/src/xspice/icm/analog/s_xfer/cfunc.mod b/src/xspice/icm/analog/s_xfer/cfunc.mod index 13b2954ec..dd95dd07d 100644 --- a/src/xspice/icm/analog/s_xfer/cfunc.mod +++ b/src/xspice/icm/analog/s_xfer/cfunc.mod @@ -135,10 +135,10 @@ double mag_x, phase_x, mag_y, phase_y; Mif_Complex_t out; -mag_x = sqrt( (x.real * x.real) + (x.imag * x.imag) ); +mag_x = hypot(x.real, x.imag); phase_x = atan2(x.imag, x.real); -mag_y = sqrt( (y.real * y.real) + (y.imag * y.imag) ); +mag_y = hypot(y.real, y.imag); phase_y = atan2(y.imag, y.real); mag_x = mag_x/mag_y;