2 * $Id: ftype-double.c,v 1.11 2003/08/27 15:23:06 gram Exp $
4 * Ethereal - Network traffic analyzer
5 * By Gerald Combs <gerald@ethereal.com>
6 * Copyright 2001 Gerald Combs
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 #include <ftypes-int.h>
36 double_fvalue_new(fvalue_t *fv)
38 fv->value.floating = 0.0;
42 double_fvalue_set_floating(fvalue_t *fv, gdouble value)
44 fv->value.floating = value;
48 value_get_floating(fvalue_t *fv)
50 return fv->value.floating;
54 val_from_unparsed(fvalue_t *fv, char *s, gboolean allow_partial_value _U_, LogFunc logfunc)
58 fv->value.floating = strtod(s, &endptr);
60 if (endptr == s || *endptr != '\0') {
61 /* This isn't a valid number. */
62 logfunc("\"%s\" is not a valid number.", s);
65 if (errno == ERANGE) {
66 if (fv->value.floating == 0) {
67 logfunc("\"%s\" causes floating-point underflow.", s);
69 else if (fv->value.floating == HUGE_VAL) {
70 logfunc("\"%s\" causes floating-point overflow.", s);
73 logfunc("\"%s\" is not a valid floating-point number.",
83 float_val_repr_len(fvalue_t *fv _U_, ftrepr_t rtype _U_)
86 * 1 character for a sign.
87 * 26 characters for a Really Big Number.
88 * XXX - is that platform-dependent?
89 * XXX - smaller for float than for double?
90 * XXX - can we compute it from FLT_DIG and the like?
96 float_val_to_repr(fvalue_t *fv, ftrepr_t rtype _U_, char *buf)
98 sprintf(buf, "%." STRINGIFY(FLT_DIG) "g", fv->value.floating);
102 double_val_repr_len(fvalue_t *fv _U_, ftrepr_t rtype _U_)
105 * 1 character for a sign.
106 * 26 characters for a Really Big Number.
107 * XXX - is that platform-dependent?
108 * XXX - can we compute it from DBL_DIG and the like?
114 double_val_to_repr(fvalue_t *fv, ftrepr_t rtype _U_, char *buf)
116 sprintf(buf, "%." STRINGIFY(DBL_DIG) "g", fv->value.floating);
120 cmp_eq(fvalue_t *a, fvalue_t *b)
122 return a->value.floating == b->value.floating;
126 cmp_ne(fvalue_t *a, fvalue_t *b)
128 return a->value.floating != b->value.floating;
132 cmp_gt(fvalue_t *a, fvalue_t *b)
134 return a->value.floating > b->value.floating;
138 cmp_ge(fvalue_t *a, fvalue_t *b)
140 return a->value.floating >= b->value.floating;
144 cmp_lt(fvalue_t *a, fvalue_t *b)
146 return a->value.floating < b->value.floating;
150 cmp_le(fvalue_t *a, fvalue_t *b)
152 return a->value.floating <= b->value.floating;
156 ftype_register_double(void)
159 static ftype_t float_type = {
161 "floating point (single-precision)",
165 val_from_unparsed, /* val_from_unparsed */
166 NULL, /* val_from_string */
167 float_val_to_repr, /* val_to_string_repr */
168 float_val_repr_len, /* len_string_repr */
172 double_fvalue_set_floating,
184 NULL, /* cmp_contains */
190 static ftype_t double_type = {
192 "floating point (double-precision)",
196 val_from_unparsed, /* val_from_unparsed */
197 NULL, /* val_from_string */
198 double_val_to_repr, /* val_to_string_repr */
199 double_val_repr_len, /* len_string_repr */
203 double_fvalue_set_floating,
215 NULL, /* cmp_contains */
221 ftype_register(FT_FLOAT, &float_type);
222 ftype_register(FT_DOUBLE, &double_type);