4 * Copyright (c) Tuomo Valkonen 1999-2002.
6 * You may distribute and modify this library under the terms of either
7 * the Clarified Artistic License or the GNU LGPL, version 2.1 or later.
14 #define FN_NUM_TO_SIGNED(T, UMAX, MAX, MIN) \
15 static int num_to_##T(T *ret, const NPNum *num, bool allow_uns_big) \
17 if(num->type!=NPNUM_INT) \
18 return E_TOKZ_NOTINT; \
22 if(allow_uns_big?num->ival>UMAX:num->ival>MAX) \
23 return E_TOKZ_RANGE; \
26 if(num->ival>-(ulong)MIN) \
27 return E_TOKZ_RANGE; \
32 #define FN_NUM_TO_UNSIGNED(T, UMAX, MIN) \
33 static int num_to_##T(T *ret, const NPNum *num, bool allow_neg) \
35 if(num->type!=NPNUM_INT) \
36 return E_TOKZ_NOTINT; \
41 return E_TOKZ_RANGE; \
44 if(!allow_neg || num->ival>(ulong)-MIN) \
45 return E_TOKZ_RANGE; \
50 #define FN_NUM_TO_FLOAT(T, POW) \
51 static int num_to_##T(T *ret, const NPNum *num) \
53 *ret=(num->negative?-num->fval:num->fval); \
57 #else /* NP_SIMPLE_IMPL */
59 #define FN_NUM_TO_SIGNED(T, UMAX, MAX, MIN) \
60 static int num_to_##T(T *ret, const NPNum *num, bool allow_uns_big) \
63 return E_TOKZ_NOTINT; \
64 if(num->nmantissa>0) \
65 return E_TOKZ_RANGE; \
68 *ret=num->mantissa[0]; \
69 if(allow_uns_big?num->mantissa[0]>UMAX:num->mantissa[0]>MAX) \
70 return E_TOKZ_RANGE; \
72 *ret=-num->mantissa[0]; \
73 if(num->mantissa[0]>-(ulong)MIN) \
74 return E_TOKZ_RANGE; \
79 #define FN_NUM_TO_UNSIGNED(T, UMAX, MIN) \
80 static int num_to_##T(T *ret, const NPNum *num, bool allow_neg) \
83 return E_TOKZ_NOTINT; \
84 if(num->nmantissa>0) \
85 return E_TOKZ_RANGE; \
88 *ret=num->mantissa[0]; \
89 if(num->mantissa[0]>UMAX) \
90 return E_TOKZ_RANGE; \
92 *ret=-num->mantissa[0]; \
93 if(!allow_neg || num->mantissa[0]>(ulong)-MIN) \
94 return E_TOKZ_RANGE; \
100 #define FN_NUM_TO_FLOAT(T, POW) \
101 static int num_to_##T(T *ret, const NPNum *num) \
106 for(i=num->nmantissa;i>=0;i--) \
107 d=d*(T)(ULONG_MAX+1.0)+num->mantissa[i]; \
109 d*=POW(num->base, num->exponent); \
115 #endif /* NP_SIMPLE_IMPL */
117 FN_NUM_TO_SIGNED(long, ULONG_MAX, LONG_MAX, LONG_MIN)
118 FN_NUM_TO_SIGNED(char, UCHAR_MAX, CHAR_MAX, CHAR_MIN)
119 FN_NUM_TO_FLOAT(double, pow)