1236
1237
: rep_signal_missing_arg (1));
1241
number_foldv (int argc, repv *argv, repv (*op) (repv, repv))
1247
return rep_signal_missing_arg (1);
1248
if (!rep_NUMERICP (argv[0]))
1249
return rep_signal_arg_error (argv[0], 1);
1252
for (i = 1; i < argc; i++)
1254
if (!rep_NUMERICP (argv[i]))
1255
return rep_signal_arg_error (argv[i], i + 1);
1257
sum = op (sum, argv[i]);
1240
1264
rep_integer_foldl (repv args, repv (*op)(repv, repv))
1727
DEFUN("+", Fplus, Splus, (repv args), rep_SubrN) /*
1792
DEFUN("+", Fplus, Splus, (int argc, repv *argv), rep_SubrV) /*
1728
1793
::doc:rep.lang.math#+::
1731
1796
Adds all NUMBERS together. If no arguments are given returns 0.
1735
1800
return rep_MAKE_INT (0);
1737
return rep_number_foldl (args, rep_number_add);
1802
return number_foldv (argc, argv, rep_number_add);
1740
DEFUN("-", Fminus, Sminus, (repv args), rep_SubrN) /*
1805
DEFUN("-", Fminus, Sminus, (int argc, repv *argv), rep_SubrV) /*
1741
1806
::doc:rep.lang.math#-::
1742
1807
- NUMBER [NUMBERS...]
1749
1814
return rep_signal_missing_arg (1);
1750
else if (!rep_CONSP (rep_CDR (args)))
1751
return rep_number_neg (rep_CAR (args));
1816
return rep_number_neg (argv[0]);
1753
return rep_number_foldl (args, rep_number_sub);
1818
return number_foldv (argc, argv, rep_number_sub);
1756
DEFUN("*", Fproduct, Sproduct, (repv args), rep_SubrN) /*
1821
DEFUN("*", Fproduct, Sproduct, (int argc, repv *argv), rep_SubrV) /*
1757
1822
::doc:rep.lang.math#*::
1760
1825
Multiplies all NUMBERS together. If no numbers are given returns 1.
1764
1829
return rep_MAKE_INT (1);
1766
return rep_number_foldl (args, rep_number_mul);
1831
return number_foldv (argc, argv, rep_number_mul);
1769
DEFUN("/", Fdivide, Sdivide, (repv args), rep_SubrN) /*
1834
DEFUN("/", Fdivide, Sdivide, (int argc, repv *argv), rep_SubrV) /*
1770
1835
::doc:rep.lang.math#/::
1773
1838
Divides NUMBERS (in left-to-right order).
1777
1842
return rep_signal_missing_arg (1);
1778
else if (!rep_CONSP (rep_CDR (args)))
1779
return rep_number_div (rep_MAKE_INT (1), rep_CAR (args));
1780
return rep_number_foldl (args, rep_number_div);
1844
return rep_number_div (rep_MAKE_INT (1), argv[0]);
1846
return number_foldv (argc, argv, rep_number_div);
1783
1849
DEFUN("remainder", Fremainder, Sremainder, (repv n1, repv n2), rep_Subr2) /*
1920
1986
return rep_number_lognot (num);
1923
DEFUN("logior", Flogior, Slogior, (repv args), rep_SubrN) /*
1989
DEFUN("logior", Flogior, Slogior, (int argc, repv *argv), rep_SubrV) /*
1924
1990
::doc:rep.lang.math#logior::
1925
1991
logior NUMBERS...
1927
1993
Returns the bitwise logical `inclusive-or' of its arguments.
1931
1997
return rep_MAKE_INT (0);
1933
return rep_number_foldl (args, rep_number_logior);
1999
return number_foldv (argc, argv, rep_number_logior);
1936
DEFUN("logxor", Flogxor, Slogxor, (repv args), rep_SubrN) /*
2002
DEFUN("logxor", Flogxor, Slogxor, (int argc, repv *argv), rep_SubrV) /*
1937
2003
::doc:rep.lang.math#logxor::
1938
2004
logxor NUMBERS...
1940
2006
Returns the bitwise logical `exclusive-or' of its arguments.
1943
return rep_number_foldl (args, rep_number_logxor);
2009
return number_foldv (argc, argv, rep_number_logxor);
1946
DEFUN("logand", Flogand, Slogand, (repv args), rep_SubrN) /*
2012
DEFUN("logand", Flogand, Slogand, (int argc, repv *argv), rep_SubrV) /*
1947
2013
::doc:rep.lang.math#logand::
1948
2014
logand NUMBERS...
1950
2016
Returns the bitwise logical `and' of its arguments.
1953
return rep_number_foldl (args, rep_number_logand);
2019
return number_foldv (argc, argv, rep_number_logand);
1956
2022
DEFUN("eql", Feql, Seql, (repv arg1, repv arg2), rep_Subr2) /*
2499
2565
is always non-negative. Returns 0 with arguments.
2503
2569
return rep_MAKE_INT (0);
2504
else if (rep_CONSP (args) && rep_CDR (args) == Qnil)
2506
rep_DECLARE1 (rep_CAR (args), rep_INTEGERP);
2507
return rep_integer_gcd (rep_CAR (args), rep_CAR (args));
2572
rep_DECLARE1 (argv[0], rep_INTEGERP);
2573
return rep_integer_gcd (argv[0], argv[0]);
2510
return rep_integer_foldl (args, rep_integer_gcd);
2576
return integer_foldv (argc, argv, rep_integer_gcd);
2513
2579
DEFUN("numberp", Fnumberp, Snumberp, (repv arg), rep_Subr1) /*
2582
2648
return rep_make_float (rep_get_float (arg), rep_TRUE);
2652
rationalize (repv arg, double *numerator, double *denominator)
2657
/* X/Y always equals the input value. Tactic is to iteratively
2658
multiply both X and Y by 2 until X is an integer. We bound
2659
the number of iterations to the size of the mantissa
2660
by starting with the normalized value... */
2662
x = frexp (rep_get_float (arg), &expt);
2663
y = pow (2.0, -expt);
2665
while (x - floor (x) > DBL_EPSILON)
2671
if (numerator != NULL)
2673
if (denominator != NULL)
2585
2677
DEFUN("inexact->exact", Finexact_to_exact,
2586
2678
Sinexact_to_exact, (repv arg), rep_Subr1) /*
2587
2679
::doc:rep.lang.math#inexact->exact::
2594
2686
rep_DECLARE1(arg, rep_NUMERICP);
2595
2688
if (rep_INTP (arg) || !rep_NUMBER_FLOAT_P (arg))
2599
/* XXX is there a way to decide if it's rationalizable? */
2600
double d = floor (rep_get_float (arg));
2601
if (d >= rep_LISP_MIN_INT && d <= rep_LISP_MAX_INT)
2602
return rep_MAKE_INT ((long) d);
2605
rep_number_z *z = make_number (rep_NUMBER_BIGNUM);
2606
2691
#ifdef HAVE_GMP
2607
mpz_init_set_d (z->z, d);
2696
q = make_number (rep_NUMBER_RATIONAL);
2698
mpq_set_d (q->q, rep_get_float (arg));
2700
return maybe_demote (rep_VAL (q));
2609
if (d >= BIGNUM_MAX)
2611
else if (d <= BIGNUM_MIN)
2614
z->z = (rep_long_long) d;
2708
rationalize (arg, &x, &y);
2709
z = make_number (rep_NUMBER_BIGNUM);
2712
return maybe_demote (rep_VAL (z));
2621
DEFUN("numerator", Fnumerator, Snumerator, (repv x), rep_Subr1) /*
2717
DEFUN("numerator", Fnumerator, Snumerator, (repv arg), rep_Subr1) /*
2622
2718
::doc:rep.lang.math#numerator::
2625
2721
Return the numerator of rational number X.
2628
rep_DECLARE1(x, rep_NUMERICP);
2629
if (rep_INTP (x) || rep_NUMBER_BIGNUM_P (x))
2724
rep_bool inexact = rep_FALSE;
2727
rep_DECLARE1(arg, rep_NUMERICP);
2631
2729
#ifdef HAVE_GMP
2632
else if (rep_NUMBER_RATIONAL_P (x))
2730
if (rep_NUMBER_RATIONAL_P (arg))
2634
2732
rep_number_z *z = make_number (rep_NUMBER_BIGNUM);
2635
mpz_init_set (z->z, mpq_numref (rep_NUMBER(x,q)));
2733
mpz_init_set (z->z, mpq_numref (rep_NUMBER(arg,q)));
2636
2734
return maybe_demote (rep_VAL (z));
2640
return rep_signal_arg_error (x, 1);
2738
if (rep_NUMBER_INEXACT_P (arg))
2741
rationalize (arg, &x, NULL);
2743
return rep_make_float (x, inexact);
2643
DEFUN("denominator", Fdenominator, Sdenominator, (repv x), rep_Subr1) /*
2746
DEFUN("denominator", Fdenominator, Sdenominator, (repv arg), rep_Subr1) /*
2644
2747
::doc:rep.lang.math#denominator::
2647
2750
Return the denominator of rational number X.
2650
rep_DECLARE1(x, rep_NUMERICP);
2651
if (rep_INTP (x) || rep_NUMBER_BIGNUM_P (x))
2652
return rep_MAKE_INT (1);
2753
rep_bool inexact = rep_FALSE;
2756
rep_DECLARE1(arg, rep_NUMERICP);
2653
2758
#ifdef HAVE_GMP
2654
else if (rep_NUMBER_RATIONAL_P (x))
2759
if (rep_NUMBER_RATIONAL_P (arg))
2656
2761
rep_number_z *z = make_number (rep_NUMBER_BIGNUM);
2657
mpz_init_set (z->z, mpq_denref (rep_NUMBER(x,q)));
2762
mpz_init_set (z->z, mpq_denref (rep_NUMBER(arg,q)));
2658
2763
return maybe_demote (rep_VAL (z));
2662
return rep_signal_arg_error (x, 1);
2767
if (rep_NUMBER_INEXACT_P (arg))
2770
rationalize (arg, NULL, &y);
2772
return rep_make_float (y, inexact);
2665
DEFUN("max", Fmax, Smax, (repv args), rep_SubrN) /*
2775
DEFUN("max", Fmax, Smax, (int argc, repv *argv), rep_SubrV) /*
2666
2776
::doc:rep.lang.math#max::