summaryrefslogtreecommitdiff
path: root/libguile/numbers.h
diff options
context:
space:
mode:
authorMarius Vollmer <mvo@zagadka.de>2003-11-18 19:59:53 +0000
committerMarius Vollmer <mvo@zagadka.de>2003-11-18 19:59:53 +0000
commitf92e85f7352174c9fe0ac0e67e6c38cfce923300 (patch)
treebc52293ad0d0d1770d35bd58a118744a80e86957 /libguile/numbers.h
parent9dd9857f77451a706052cae009d2fe7150373c5a (diff)
downloadguile-f92e85f7352174c9fe0ac0e67e6c38cfce923300.tar.gz
* print.c (scm_iprin1): Handle fractions.
* objects.h (scm_class_fraction): New. * objects.c (scm_class_fraction): New. (scm_class_of): Handle fractions. * hash.c (scm_hasher): Handle fractions. * numbers.c: New code for handling fraction all over the place. (scm_odd_p, scm_even_p): Handle inexact integers. (scm_rational_p): New function, same as scm_real_p. (scm_round_number, scm_truncate_number, scm_ceiling, scm_floor): New exact functions that replace the inexact 'dsubr' implementations. (scm_numerator, scm_denominator): New. * numbers.h (SCM_NUMP): Recognize fractions. (SCM_FRACTIONP, SCM_SLOPPY_FRACTIONP, SCM_FRACTION_NUMERATOR, SCM_FRACTION_DENOMINATOR, SCM_FRACTION_SET_NUMERATOR, SCM_FRACTION_SET_DENOMINATOR, SCM_FRACTION_REDUCED_BIT, SCM_FRACTION_REDUCED_SET, SCM_FRACTION_REDUCED_CLEAR, SCM_FRACTION_REDUCED): New. (scm_floor, scm_ceiling, scm_truncate_number, scm_round_number): New prototypes. (scm_make_ratio, scm_rationalize, scm_numerator, scm_denominator, scm_rational_p): New prototypes. (scm_i_dbl2num, scm_i_fraction2double, scm_i_fraction_equalp, scm_i_print_fraction): New prototypes. * goops.c (create_standard_classes): Create "<fraction>" class. * gc-mark.c (scm_gc_mark_dependencies): Handle fractions. * gc-card.c (scm_i_sweep_card): Include scm_tc16_fraction as a case in the switch, but do nothing for now. * eval.c (SCM_CEVAL, SCM_APPLY, call_dsubr_1): Convert fractions to doubles when calling 'dsubr' functions. * eq.c (scm_eqv_p, scm_equal_p): Handle fractions.
Diffstat (limited to 'libguile/numbers.h')
-rw-r--r--libguile/numbers.h34
1 files changed, 33 insertions, 1 deletions
diff --git a/libguile/numbers.h b/libguile/numbers.h
index 8bf211d6f..772a0ebe5 100644
--- a/libguile/numbers.h
+++ b/libguile/numbers.h
@@ -131,6 +131,7 @@
#define scm_tc16_big (scm_tc7_number + 1 * 256L)
#define scm_tc16_real (scm_tc7_number + 2 * 256L)
#define scm_tc16_complex (scm_tc7_number + 3 * 256L)
+#define scm_tc16_fraction (scm_tc7_number + 4 * 256L)
#define SCM_INEXACTP(x) \
(!SCM_IMP (x) && (0xfeff & SCM_CELL_TYPE (x)) == scm_tc16_real)
@@ -148,7 +149,21 @@
#define SCM_NUMBERP(x) (SCM_INUMP(x) || SCM_NUMP(x))
#define SCM_NUMP(x) (!SCM_IMP(x) \
- && (0xfcff & SCM_CELL_TYPE (x)) == scm_tc7_number)
+ && (((0xfcff & SCM_CELL_TYPE (x)) == scm_tc7_number) \
+ || ((0xfbff & SCM_CELL_TYPE (x)) == scm_tc7_number)))
+/* 0xfcff (#b1100) for 0 free, 1 big, 2 real, 3 complex, then 0xfbff (#b1011) for 4 fraction */
+
+#define SCM_FRACTIONP(x) (!SCM_IMP (x) && SCM_TYP16 (x) == scm_tc16_fraction)
+#define SCM_SLOPPY_FRACTIONP(x) (SCM_TYP16 (x) == scm_tc16_fraction)
+#define SCM_FRACTION_NUMERATOR(x) ((SCM) (SCM_CELL_WORD_1 (x)))
+#define SCM_FRACTION_DENOMINATOR(x) ((SCM) (SCM_CELL_WORD_2 (x)))
+#define SCM_FRACTION_SET_NUMERATOR(x, v) ((SCM) (SCM_SET_CELL_WORD_1 ((x), (v))))
+#define SCM_FRACTION_SET_DENOMINATOR(x, v) ((SCM) (SCM_SET_CELL_WORD_2 ((x), (v))))
+ /* I think the left half word is free in the type, so I'll use bit 17 */
+#define SCM_FRACTION_REDUCED_BIT 0x10000
+#define SCM_FRACTION_REDUCED_SET(x) (SCM_SET_CELL_TYPE((x), (SCM_CELL_TYPE (x) | SCM_FRACTION_REDUCED_BIT)))
+#define SCM_FRACTION_REDUCED_CLEAR(x) (SCM_SET_CELL_TYPE((x), (SCM_CELL_TYPE (x) & ~SCM_FRACTION_REDUCED_BIT)))
+#define SCM_FRACTION_REDUCED(x) (0x10000 & SCM_CELL_TYPE (x))
@@ -223,11 +238,15 @@ SCM_API SCM scm_difference (SCM x, SCM y);
SCM_API SCM scm_product (SCM x, SCM y);
SCM_API double scm_num2dbl (SCM a, const char * why);
SCM_API SCM scm_divide (SCM x, SCM y);
+SCM_API SCM scm_floor (SCM x);
+SCM_API SCM scm_ceiling (SCM x);
SCM_API double scm_asinh (double x);
SCM_API double scm_acosh (double x);
SCM_API double scm_atanh (double x);
SCM_API double scm_truncate (double x);
SCM_API double scm_round (double x);
+SCM_API SCM scm_truncate_number (SCM x);
+SCM_API SCM scm_round_number (SCM x);
SCM_API SCM scm_sys_expt (SCM z1, SCM z2);
SCM_API SCM scm_sys_atan2 (SCM z1, SCM z2);
SCM_API SCM scm_make_rectangular (SCM z1, SCM z2);
@@ -286,6 +305,7 @@ SCM_API SCM scm_i_mkbig (void);
SCM_API SCM scm_i_normbig (SCM x);
SCM_API int scm_i_bigcmp (SCM a, SCM b);
SCM_API SCM scm_i_dbl2big (double d);
+SCM_API SCM scm_i_dbl2num (double d);
SCM_API double scm_i_big2dbl (SCM b);
SCM_API SCM scm_i_short2big (short n);
SCM_API SCM scm_i_ushort2big (unsigned short n);
@@ -302,6 +322,18 @@ SCM_API SCM scm_i_ulong_long2big (unsigned long long n);
#endif
+/* ratio functions */
+SCM_API SCM scm_make_ratio (SCM num, SCM den);
+SCM_API SCM scm_rationalize (SCM x, SCM err);
+SCM_API SCM scm_numerator (SCM z);
+SCM_API SCM scm_denominator (SCM z);
+SCM_API SCM scm_rational_p (SCM z);
+
+/* fraction internal functions */
+SCM_API double scm_i_fraction2double (SCM z);
+SCM_API SCM scm_i_fraction_equalp (SCM x, SCM y);
+SCM_API int scm_i_print_fraction (SCM sexp, SCM port, scm_print_state *pstate);
+
#ifdef GUILE_DEBUG
SCM_API SCM scm_sys_check_number_conversions (void);