[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Guile-commits] 62/85: Simplify magnitude, angle
From: |
Andy Wingo |
Subject: |
[Guile-commits] 62/85: Simplify magnitude, angle |
Date: |
Thu, 13 Jan 2022 03:40:23 -0500 (EST) |
wingo pushed a commit to branch main
in repository guile.
commit 7029a9c491827a641814e3624d3a16cc69a01aa1
Author: Andy Wingo <wingo@pobox.com>
AuthorDate: Fri Jan 7 11:29:28 2022 +0100
Simplify magnitude, angle
* libguile/numbers.c (scm_i_inum2big): Remove.
(scm_magnitude): Delegate to abs.
(scm_angle): Simplify.
---
libguile/numbers.c | 79 +++++++++---------------------------------------------
1 file changed, 12 insertions(+), 67 deletions(-)
diff --git a/libguile/numbers.c b/libguile/numbers.c
index 9575df09c..b2d78a694 100644
--- a/libguile/numbers.c
+++ b/libguile/numbers.c
@@ -300,15 +300,6 @@ scm_i_mkbig ()
return z;
}
-static SCM
-scm_i_inum2big (scm_t_inum x)
-{
- /* Return a newly created bignum initialized to X. */
- SCM z = make_bignum ();
- mpz_init_set_si (SCM_I_BIG_MPZ (z), x);
- return z;
-}
-
SCM
scm_i_long2big (long x)
{
@@ -6315,35 +6306,10 @@ SCM_PRIMITIVE_GENERIC (scm_magnitude, "magnitude", 1,
0, 0,
"@code{abs} for real arguments, but also allows complex numbers.")
#define FUNC_NAME s_scm_magnitude
{
- if (SCM_I_INUMP (z))
- {
- scm_t_inum zz = SCM_I_INUM (z);
- if (zz >= 0)
- return z;
- else if (SCM_POSFIXABLE (-zz))
- return SCM_I_MAKINUM (-zz);
- else
- return scm_i_inum2big (-zz);
- }
- else if (SCM_BIGP (z))
- {
- if (scm_is_integer_negative_z (scm_bignum (z)))
- return scm_integer_negate_z (scm_bignum (z));
- else
- return z;
- }
- else if (SCM_REALP (z))
- return scm_i_from_double (fabs (SCM_REAL_VALUE (z)));
- else if (SCM_COMPLEXP (z))
+ if (SCM_COMPLEXP (z))
return scm_i_from_double (hypot (SCM_COMPLEX_REAL (z), SCM_COMPLEX_IMAG
(z)));
- else if (SCM_FRACTIONP (z))
- {
- if (scm_is_false (scm_negative_p (SCM_FRACTION_NUMERATOR (z))))
- return z;
- return scm_i_make_ratio_already_reduced
- (scm_difference (SCM_FRACTION_NUMERATOR (z), SCM_UNDEFINED),
- SCM_FRACTION_DENOMINATOR (z));
- }
+ else if (SCM_NUMBERP (z))
+ return scm_abs (z);
else
return scm_wta_dispatch_1 (g_scm_magnitude, z, SCM_ARG1,
s_scm_magnitude);
@@ -6360,36 +6326,15 @@ SCM_PRIMITIVE_GENERIC (scm_angle, "angle", 1, 0, 0,
flo0 to save allocating a new flonum with scm_i_from_double each time.
But if atan2 follows the floating point rounding mode, then the value
is not a constant. Maybe it'd be close enough though. */
- if (SCM_I_INUMP (z))
- {
- if (SCM_I_INUM (z) >= 0)
- return flo0;
- else
- return scm_i_from_double (atan2 (0.0, -1.0));
- }
- else if (SCM_BIGP (z))
- {
- if (scm_is_integer_negative_z (scm_bignum (z)))
- return scm_i_from_double (atan2 (0.0, -1.0));
- else
- return flo0;
- }
- else if (SCM_REALP (z))
- {
- double x = SCM_REAL_VALUE (z);
- if (copysign (1.0, x) > 0.0)
- return flo0;
- else
- return scm_i_from_double (atan2 (0.0, -1.0));
- }
- else if (SCM_COMPLEXP (z))
- return scm_i_from_double (atan2 (SCM_COMPLEX_IMAG (z), SCM_COMPLEX_REAL
(z)));
- else if (SCM_FRACTIONP (z))
- {
- if (scm_is_false (scm_negative_p (SCM_FRACTION_NUMERATOR (z))))
- return flo0;
- else return scm_i_from_double (atan2 (0.0, -1.0));
- }
+ if (SCM_COMPLEXP (z))
+ return scm_i_from_double (atan2 (SCM_COMPLEX_IMAG (z),
+ SCM_COMPLEX_REAL (z)));
+ else if (SCM_NUMBERP (z))
+ return (SCM_REALP (z)
+ ? copysign (1.0, SCM_REAL_VALUE (z)) < 0.0
+ : scm_is_true (scm_negative_p (z)))
+ ? scm_i_from_double (atan2 (0.0, -1.0))
+ : flo0;
else
return scm_wta_dispatch_1 (g_scm_angle, z, SCM_ARG1, s_scm_angle);
}
- [Guile-commits] 79/85: Optimize scm_integer_mul_ii, (continued)
- [Guile-commits] 79/85: Optimize scm_integer_mul_ii, Andy Wingo, 2022/01/13
- [Guile-commits] 80/85: Optimize integer-expt for fixnums, Andy Wingo, 2022/01/13
- [Guile-commits] 81/85: Optimize logand against a positive inum, Andy Wingo, 2022/01/13
- [Guile-commits] 82/85: Simplify scm_abs for the real case, Andy Wingo, 2022/01/13
- [Guile-commits] 09/85: Implement ceiling-divide with new integer lib, Andy Wingo, 2022/01/13
- [Guile-commits] 08/85: Implement ceiling-remainder with new integer lib, Andy Wingo, 2022/01/13
- [Guile-commits] 11/85: Implement truncate-remainder with new integer lib, Andy Wingo, 2022/01/13
- [Guile-commits] 49/85: Reimplement scm_from_int8 etc, Andy Wingo, 2022/01/13
- [Guile-commits] 55/85: Reimplement exact-integer-sqrt with integers.[ch], Andy Wingo, 2022/01/13
- [Guile-commits] 56/85: Refactor scm_sqrt in terms of integers.[ch], Andy Wingo, 2022/01/13
- [Guile-commits] 62/85: Simplify magnitude, angle,
Andy Wingo <=
- [Guile-commits] 65/85: Start porting srfi-60 off the bad bignum interfaces, Andy Wingo, 2022/01/13
- [Guile-commits] 72/85: Optimize scm_integer_mul_zi, Andy Wingo, 2022/01/13
- [Guile-commits] 75/85: Start to optimize scm_integer_sub_iz, Andy Wingo, 2022/01/13
- [Guile-commits] 77/85: Optimize bignum add to avoid temporary allocations, Andy Wingo, 2022/01/13