minus.d 5.49 KB
Newer Older
1 2
/* -*- Mode: C; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/* vim: set filetype=c tabstop=2 shiftwidth=2 expandtab: */
3

4
/*
5 6 7 8 9 10 11 12 13
 * ecminus.d - implementation of CL:-
 *
 * Copyright (c) 1984 Taiichi Yuasa and Masami Hagiya
 * Copyright (c) 1990 Giuseppe Attardi
 * Copyright (c) 2001 Juan Jose Garcia Ripoll
 *
 * See file 'LICENSE' for the copyright details.
 *
 */
14 15 16 17 18 19


#include <ecl/ecl.h>
#include <ecl/impl/math_dispatch2.h>

@(defun - (num &rest nums)
20 21 22 23 24 25 26 27 28 29
  cl_object diff;
  @
  /* INV: argument type check in number_{negate,minus}() */
  if (narg == 1) {
    @(return ecl_negate(num));
  }
  for (diff = num; --narg; )
    diff = ecl_minus(diff, ecl_va_arg(nums));
  @(return diff);
  @)
30 31 32 33

cl_object
ecl_minus(cl_object x, cl_object y)
{
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
  MATH_DISPATCH2_BEGIN(x,y)
    {
      CASE_FIXNUM_FIXNUM {
        return ecl_make_integer(ecl_fixnum(x) - ecl_fixnum(y));
      }
      CASE_FIXNUM_BIGNUM {
        return _ecl_fix_minus_big(ecl_fixnum(x), y);
      }
      CASE_FIXNUM_RATIO;
      CASE_BIGNUM_RATIO {
        cl_object z = ecl_times(x, y->ratio.den);
        z = ecl_minus(z, y->ratio.num);
        return ecl_make_ratio(z, y->ratio.den);
      }
      CASE_FIXNUM_SINGLE_FLOAT {
        return ecl_make_single_float(ecl_fixnum(x) - ecl_single_float(y));
      }
      CASE_FIXNUM_DOUBLE_FLOAT {
        return ecl_make_double_float(ecl_fixnum(x) - ecl_double_float(y));
      }
      CASE_BIGNUM_FIXNUM {
        return _ecl_big_plus_fix(x, -ecl_fixnum(y));
      }
      CASE_BIGNUM_BIGNUM {
        return _ecl_big_minus_big(x, y);
      }
      CASE_BIGNUM_SINGLE_FLOAT;
      CASE_RATIO_SINGLE_FLOAT {
        return ecl_make_single_float(ecl_to_float(x) - ecl_single_float(y));
      }
      CASE_BIGNUM_DOUBLE_FLOAT;
      CASE_RATIO_DOUBLE_FLOAT {
        return ecl_make_double_float(ecl_to_double(x) - ecl_double_float(y));
      }
      CASE_RATIO_FIXNUM;
69
      /* fallthrough */
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
      CASE_RATIO_BIGNUM {
        cl_object z = ecl_times(x->ratio.den, y);
        z = ecl_minus(x->ratio.num, z);
        return ecl_make_ratio(z, x->ratio.den);
      }
      CASE_RATIO_RATIO {
        cl_object z1 = ecl_times(x->ratio.num,y->ratio.den);
        cl_object z = ecl_times(x->ratio.den,y->ratio.num);
        z = ecl_minus(z1, z);
        z1 = ecl_times(x->ratio.den,y->ratio.den);
        return ecl_make_ratio(z, z1);
      }
      CASE_SINGLE_FLOAT_FIXNUM {
        return ecl_make_single_float(ecl_single_float(x) - ecl_fixnum(y));
      }
      CASE_SINGLE_FLOAT_BIGNUM;
      CASE_SINGLE_FLOAT_RATIO {
        return ecl_make_single_float(ecl_single_float(x) - ecl_to_float(y));
      }
      CASE_SINGLE_FLOAT_SINGLE_FLOAT {
        return ecl_make_single_float(ecl_single_float(x) - ecl_single_float(y));
      }
      CASE_SINGLE_FLOAT_DOUBLE_FLOAT {
        return ecl_make_double_float(ecl_single_float(x) - ecl_double_float(y));
      }
      CASE_DOUBLE_FLOAT_FIXNUM {
        return ecl_make_double_float(ecl_double_float(x) - ecl_fixnum(y));
      }
      CASE_DOUBLE_FLOAT_BIGNUM;
      CASE_DOUBLE_FLOAT_RATIO {
        return ecl_make_double_float(ecl_double_float(x) - ecl_to_double(y));
      }
      CASE_DOUBLE_FLOAT_SINGLE_FLOAT {
        return ecl_make_double_float(ecl_double_float(x) - ecl_single_float(y));
      }
      CASE_DOUBLE_FLOAT_DOUBLE_FLOAT {
        return ecl_make_double_float(ecl_double_float(x) - ecl_double_float(y));
      }
108
#ifdef ECL_LONG_FLOAT
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
      CASE_FIXNUM_LONG_FLOAT {
        return ecl_make_long_float(ecl_fixnum(x) - ecl_long_float(y));
      }
      CASE_BIGNUM_LONG_FLOAT {
        return ecl_make_long_float(ecl_to_long_double(x) - ecl_long_float(y));
      }
      CASE_RATIO_LONG_FLOAT {
        return ecl_make_long_float(ecl_to_long_double(x) - ecl_long_float(y));
      }
      CASE_SINGLE_FLOAT_LONG_FLOAT {
        return ecl_make_long_float(ecl_single_float(x) - ecl_long_float(y));
      }
      CASE_DOUBLE_FLOAT_LONG_FLOAT {
        return ecl_make_long_float(ecl_double_float(x) - ecl_long_float(y));
      }
      CASE_LONG_FLOAT_FIXNUM {
        return ecl_make_long_float(ecl_long_float(x) - ecl_fixnum(y));
      }
      CASE_LONG_FLOAT_BIGNUM;
      CASE_LONG_FLOAT_RATIO {
        return ecl_make_long_float(ecl_long_float(x) - ecl_to_long_double(y));
      }
      CASE_LONG_FLOAT_SINGLE_FLOAT {
        return ecl_make_long_float(ecl_long_float(x) - ecl_single_float(y));
      }
      CASE_LONG_FLOAT_DOUBLE_FLOAT {
        return ecl_make_long_float(ecl_long_float(x) - ecl_double_float(y));
      }
      CASE_LONG_FLOAT_LONG_FLOAT {
        return ecl_make_long_float(ecl_long_float(x) - ecl_long_float(y));
      }
      CASE_LONG_FLOAT_COMPLEX {
        goto COMPLEX_Y;
      }
      CASE_COMPLEX_LONG_FLOAT;  {
        goto COMPLEX_X;
      }
146
#endif
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
      CASE_COMPLEX_FIXNUM;
      CASE_COMPLEX_BIGNUM;
      CASE_COMPLEX_RATIO;
      CASE_COMPLEX_SINGLE_FLOAT;
      CASE_COMPLEX_DOUBLE_FLOAT {
      COMPLEX_X:
        return ecl_make_complex(ecl_minus(x->complex.real, y),
                                x->complex.imag);
      }
      CASE_BIGNUM_COMPLEX;
      CASE_RATIO_COMPLEX;
      CASE_SINGLE_FLOAT_COMPLEX;
      CASE_DOUBLE_FLOAT_COMPLEX;
      CASE_FIXNUM_COMPLEX {
      COMPLEX_Y:
        return ecl_make_complex(ecl_minus(x, y->complex.real),
                                ecl_negate(y->complex.imag));
      }
      CASE_COMPLEX_COMPLEX {
        cl_object z = ecl_minus(x->complex.real, y->complex.real);
        cl_object z1 = ecl_minus(x->complex.imag, y->complex.imag);
        return ecl_make_complex(z, z1);
      }
      CASE_UNKNOWN(@[-],x,y,@[number]);
    }
  MATH_DISPATCH2_END;
173
}