1 /* Copyright (C) 1995,1996,1997,1998,2000,2001,2003 Free Software Foundation, Inc.
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "libguile/_scm.h"
24 #include "libguile/ramap.h"
25 #include "libguile/stackchk.h"
26 #include "libguile/strorder.h"
27 #include "libguile/async.h"
28 #include "libguile/root.h"
29 #include "libguile/smob.h"
30 #include "libguile/unif.h"
31 #include "libguile/vectors.h"
33 #include "libguile/validate.h"
34 #include "libguile/eq.h"
42 SCM_DEFINE1 (scm_eq_p
, "eq?", scm_tc7_rpsubr
,
44 "Return @code{#t} iff @var{x} references the same object as @var{y}.\n"
45 "@code{eq?} is similar to @code{eqv?} except that in some cases it is\n"
46 "capable of discerning distinctions finer than those detectable by\n"
48 #define FUNC_NAME s_scm_eq_p
50 return scm_from_bool (scm_is_eq (x
, y
));
54 /* We compare doubles in a special way for 'eqv?' to be able to
55 distinguish plus and minus zero and to identify NaNs.
59 real_eqv (double x
, double y
)
61 return !memcmp (&x
, &y
, sizeof(double));
65 SCM_PRIMITIVE_GENERIC_1 (scm_eqv_p
, "eqv?", scm_tc7_rpsubr
,
67 "The @code{eqv?} procedure defines a useful equivalence relation on objects.\n"
68 "Briefly, it returns @code{#t} if @var{x} and @var{y} should normally be\n"
69 "regarded as the same object. This relation is left slightly open to\n"
70 "interpretation, but works for comparing immediate integers, characters,\n"
71 "and inexact numbers.")
72 #define FUNC_NAME s_scm_eqv_p
80 /* this ensures that types and scm_length are the same. */
82 if (SCM_CELL_TYPE (x
) != SCM_CELL_TYPE (y
))
84 /* fractions use 0x10000 as a flag (at the suggestion of Marius Vollmer),
85 but this checks the entire type word, so fractions may be accidentally
86 flagged here as unequal. Perhaps I should use the 4th double_cell word?
89 /* treat mixes of real and complex types specially */
93 return scm_from_bool (SCM_COMPLEXP (y
)
94 && real_eqv (SCM_REAL_VALUE (x
),
96 && SCM_COMPLEX_IMAG (y
) == 0.0);
98 return scm_from_bool (SCM_REALP (y
)
99 && real_eqv (SCM_COMPLEX_REAL (x
),
101 && SCM_COMPLEX_IMAG (x
) == 0.0);
104 if (SCM_FRACTIONP (x
) && SCM_FRACTIONP (y
))
105 return scm_i_fraction_equalp (x
, y
);
111 return scm_from_bool (scm_i_bigcmp (x
, y
) == 0);
112 } else if (SCM_REALP (x
)) {
113 return scm_from_bool (real_eqv (SCM_REAL_VALUE (x
), SCM_REAL_VALUE (y
)));
114 } else if (SCM_FRACTIONP (x
)) {
115 return scm_i_fraction_equalp (x
, y
);
116 } else { /* complex */
117 return scm_from_bool (real_eqv (SCM_COMPLEX_REAL (x
),
118 SCM_COMPLEX_REAL (y
))
119 && real_eqv (SCM_COMPLEX_IMAG (x
),
120 SCM_COMPLEX_IMAG (y
)));
123 if (SCM_UNPACK (g_scm_eqv_p
))
124 return scm_call_generic_2 (g_scm_eqv_p
, x
, y
);
131 SCM_PRIMITIVE_GENERIC_1 (scm_equal_p
, "equal?", scm_tc7_rpsubr
,
133 "Return @code{#t} iff @var{x} and @var{y} are recursively @code{eqv?} equivalent.\n"
134 "@code{equal?} recursively compares the contents of pairs,\n"
135 "vectors, and strings, applying @code{eqv?} on other objects such as\n"
136 "numbers and symbols. A rule of thumb is that objects are generally\n"
137 "@code{equal?} if they print the same. @code{equal?} may fail to\n"
138 "terminate if its arguments are circular data structures.")
139 #define FUNC_NAME s_scm_equal_p
144 if (scm_is_eq (x
, y
))
150 if (SCM_CONSP (x
) && SCM_CONSP (y
))
152 if (scm_is_false (scm_equal_p (SCM_CAR (x
), SCM_CAR (y
))))
158 if (SCM_TYP7 (x
) == scm_tc7_string
&& SCM_TYP7 (y
) == scm_tc7_string
)
159 return scm_string_equal_p (x
, y
);
160 /* This ensures that types and scm_length are the same. */
161 if (SCM_CELL_TYPE (x
) != SCM_CELL_TYPE (y
))
163 /* treat mixes of real and complex types specially */
164 if (SCM_INEXACTP (x
) && SCM_INEXACTP (y
))
167 return scm_from_bool (SCM_COMPLEXP (y
)
168 && SCM_REAL_VALUE (x
) == SCM_COMPLEX_REAL (y
)
169 && SCM_COMPLEX_IMAG (y
) == 0.0);
171 return scm_from_bool (SCM_REALP (y
)
172 && SCM_COMPLEX_REAL (x
) == SCM_REAL_VALUE (y
)
173 && SCM_COMPLEX_IMAG (x
) == 0.0);
176 /* should we handle fractions here also? */
177 else if ((SCM_FRACTIONP (x
)) && (SCM_INEXACTP (y
)))
180 return scm_from_bool (scm_i_fraction2double (x
) == SCM_REAL_VALUE (y
));
182 return scm_from_bool (SCM_COMPLEX_REAL (y
) == scm_i_fraction2double (x
)
183 && SCM_COMPLEX_IMAG (y
) == 0.0);
185 else if ((SCM_FRACTIONP (y
)) && (SCM_INEXACTP (x
)))
188 return scm_from_bool (scm_i_fraction2double (y
) == SCM_REAL_VALUE (x
));
190 return scm_from_bool (SCM_COMPLEX_REAL (x
) == scm_i_fraction2double (y
)
191 && SCM_COMPLEX_IMAG (x
) == 0.0);
196 switch (SCM_TYP7 (x
))
204 return scm_bigequal (x
, y
);
206 return scm_real_equalp (x
, y
);
207 case scm_tc16_complex
:
208 return scm_complex_equalp (x
, y
);
209 case scm_tc16_fraction
:
210 return scm_i_fraction_equalp (x
, y
);
214 return scm_vector_equal_p (x
, y
);
217 int i
= SCM_SMOBNUM (x
);
218 if (!(i
< scm_numsmob
))
220 if (scm_smobs
[i
].equalp
)
221 return (scm_smobs
[i
].equalp
) (x
, y
);
226 case scm_tc7_bvect
: case scm_tc7_uvect
: case scm_tc7_ivect
:
227 case scm_tc7_fvect
: case scm_tc7_cvect
: case scm_tc7_dvect
:
229 #if SCM_SIZEOF_LONG_LONG != 0
233 if (scm_tc16_array
&& scm_smobs
[SCM_TC2SMOBNUM (scm_tc16_array
)].equalp
)
234 return scm_array_equal_p (x
, y
);
237 if (SCM_UNPACK (g_scm_equal_p
))
238 return scm_call_generic_2 (g_scm_equal_p
, x
, y
);
252 #include "libguile/eq.x"