QOF: qofnumeric.h Source File

QOF 0.7.5

qofnumeric.h
Go to the documentation of this file.
1 /********************************************************************
2 * qofnumeric.h - A rational number library *
3 * This program is free software; you can redistribute it and/or *
4 * modify it under the terms of the GNU General Public License as *
5 * published by the Free Software Foundation; either version 2 of *
6 * the License, or (at your option) any later version. *
7 * *
8 * This program 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 *
11 * GNU General Public License for more details. *
12 * *
13 * You should have received a copy of the GNU General Public License*
14 * along with this program; if not, contact: *
15 * *
16 * Free Software Foundation Voice: +1-617-542-5942 *
17 * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
18 * Boston, MA 02110-1301, USA gnu@gnu.org *
19 * *
20 *******************************************************************/
21
49 #ifndef QOF_NUMERIC_H
50 #define QOF_NUMERIC_H
51
52 struct _QofNumeric
53 {
54 gint64 num;
55 gint64 denom;
56 };
57
61 typedef struct _QofNumeric QofNumeric;
62
116 #define QOF_NUMERIC_RND_MASK 0x0000000f
117 #define QOF_NUMERIC_DENOM_MASK 0x000000f0
118 #define QOF_NUMERIC_SIGFIGS_MASK 0x0000ff00
119
129 enum
130 {
132 QOF_HOW_RND_FLOOR = 0x01,
133
135 QOF_HOW_RND_CEIL = 0x02,
136
138 QOF_HOW_RND_TRUNC = 0x03,
139
141 QOF_HOW_RND_PROMOTE = 0x04,
142
146 QOF_HOW_RND_ROUND_HALF_DOWN = 0x05,
147
151 QOF_HOW_RND_ROUND_HALF_UP = 0x06,
152
158 QOF_HOW_RND_ROUND = 0x07,
159
163 QOF_HOW_RND_NEVER = 0x08
164 };
165
167 enum
168 {
174 QOF_HOW_DENOM_EXACT = 0x10,
175
181 QOF_HOW_DENOM_REDUCE = 0x20,
182
186 QOF_HOW_DENOM_LCD = 0x30,
187
192 QOF_HOW_DENOM_FIXED = 0x40,
193
197 QOF_HOW_DENOM_SIGFIG = 0x50
198 };
199
203 #define QOF_HOW_DENOM_SIGFIGS( n ) ( ((( n ) & 0xff) << 8) | QOF_HOW_DENOM_SIGFIG)
204 #define QOF_HOW_GET_SIGFIGS( a ) ( (( a ) & 0xff00 ) >> 8)
205
207 typedef enum
208 {
209 QOF_ERROR_OK = 0,
210 QOF_ERROR_ARG = -1,
211 QOF_ERROR_OVERFLOW = -2,
214 QOF_ERROR_DENOM_DIFF = -3,
215
218 QOF_ERROR_REMAINDER = -4
219 } QofNumericErrorCode;
220
221
231 #define QOF_DENOM_AUTO 0
232
234 #define QOF_DENOM_RECIPROCAL( a ) (- ( a ))
235
242 static inline QofNumeric
243 qof_numeric_create (gint64 num, gint64 denom)
244 {
245 QofNumeric out;
246 out.num = num;
247 out.denom = denom;
248 return out;
249 }
250
252 static inline QofNumeric
253 qof_numeric_zero (void)
254 {
255 return qof_numeric_create (0, 1);
256 }
257
263 QofNumeric
264 qof_numeric_from_double (gdouble in, gint64 denom, gint how);
265
269 gboolean
270 qof_numeric_from_string (const gchar * str, QofNumeric * n);
271
275 QofNumeric
276 qof_numeric_error (QofNumericErrorCode error_code);
283 static inline gint64
284 qof_numeric_num (QofNumeric a)
285 {
286 return a.num;
287 }
288
290 static inline gint64
291 qof_numeric_denom (QofNumeric a)
292 {
293 return a.denom;
294 }
295
297 gdouble
298 qof_numeric_to_double (QofNumeric in);
299
302 gchar *
303 qof_numeric_to_string (QofNumeric n);
304
307 gchar *
308 qof_numeric_dbg_to_string (QofNumeric n);
318 QofNumericErrorCode
319 qof_numeric_check (QofNumeric a);
320
322 gint
323 qof_numeric_compare (QofNumeric a, QofNumeric b);
324
326 gboolean
327 qof_numeric_zero_p (QofNumeric a);
328
330 gboolean
331 qof_numeric_negative_p (QofNumeric a);
332
334 gboolean
335 qof_numeric_positive_p (QofNumeric a);
336
340 gboolean
341 qof_numeric_eq (QofNumeric a, QofNumeric b);
342
347 gboolean
348 qof_numeric_equal (QofNumeric a, QofNumeric b);
349
362 gint
363 qof_numeric_same (QofNumeric a, QofNumeric b, gint64 denom, gint how);
370 QofNumeric
371 qof_numeric_add (QofNumeric a, QofNumeric b,
372 gint64 denom, gint how);
373
375 QofNumeric
376 qof_numeric_sub (QofNumeric a, QofNumeric b,
377 gint64 denom, gint how);
378
384 QofNumeric
385 qof_numeric_mul (QofNumeric a, QofNumeric b,
386 gint64 denom, gint how);
387
395 QofNumeric
396 qof_numeric_div (QofNumeric x, QofNumeric y,
397 gint64 denom, gint how);
399 QofNumeric
400 qof_numeric_neg (QofNumeric a);
401
403 QofNumeric
404 qof_numeric_abs (QofNumeric a);
405
410 static inline QofNumeric
411 qof_numeric_add_fixed (QofNumeric a, QofNumeric b)
412 {
413 return qof_numeric_add (a, b, QOF_DENOM_AUTO,
414 QOF_HOW_DENOM_FIXED | QOF_HOW_RND_NEVER);
415 }
416
421 static inline QofNumeric
422 qof_numeric_sub_fixed (QofNumeric a, QofNumeric b)
423 {
424 return qof_numeric_sub (a, b, QOF_DENOM_AUTO,
425 QOF_HOW_DENOM_FIXED | QOF_HOW_RND_NEVER);
426 }
427
435 QofNumeric
436 qof_numeric_add_with_error (QofNumeric a, QofNumeric b,
437 gint64 denom, gint how,
438 QofNumeric * error);
439
442 QofNumeric
443 qof_numeric_sub_with_error (QofNumeric a, QofNumeric b,
444 gint64 denom, gint how,
445 QofNumeric * error);
446
450 QofNumeric
451 qof_numeric_mul_with_error (QofNumeric a, QofNumeric b,
452 gint64 denom, gint how,
453 QofNumeric * error);
454
458 QofNumeric
459 qof_numeric_div_with_error (QofNumeric a, QofNumeric b,
460 gint64 denom, gint how,
461 QofNumeric * error);
471 QofNumeric
472 qof_numeric_convert (QofNumeric in, gint64 denom, gint how);
473
477 QofNumeric
478 qof_numeric_convert_with_error (QofNumeric in, gint64 denom,
479 gint how, QofNumeric * error);
480
483 QofNumeric qof_numeric_reduce (QofNumeric in);
489 #define QOF_RND_FLOOR QOF_HOW_RND_FLOOR
490 #define QOF_RND_CEIL QOF_HOW_RND_CEIL
491 #define QOF_RND_TRUNC QOF_HOW_RND_TRUNC
492 #define QOF_RND_PROMOTE QOF_HOW_RND_PROMOTE
493 #define QOF_RND_ROUND_HALF_DOWN QOF_HOW_RND_ROUND_HALF_DOWN
494 #define QOF_RND_ROUND_HALF_UP QOF_HOW_RND_ROUND_HALF_UP
495 #define QOF_RND_ROUND QOF_HOW_RND_ROUND
496 #define QOF_RND_NEVER QOF_HOW_RND_NEVER
497
498 #define QOF_DENOM_EXACT QOF_HOW_DENOM_EXACT
499 #define QOF_DENOM_REDUCE QOF_HOW_DENOM_REDUCE
500 #define QOF_DENOM_LCD QOF_HOW_DENOM_LCD
501 #define QOF_DENOM_FIXED QOF_HOW_DENOM_FIXED
502 #define QOF_DENOM_SIGFIG QOF_HOW_DENOM_SIGFIG
503
504 #define QOF_DENOM_SIGFIGS(X) QOF_HOW_DENOM_SIGFIGS(X)
505 #define QOF_NUMERIC_GET_SIGFIGS(X) QOF_HOW_GET_SIGFIGS(X)
506
508 #endif
QOF_HOW_DENOM_SIGFIG
Definition: qofnumeric.h:197
qof_numeric_check
QofNumericErrorCode qof_numeric_check(QofNumeric a)
Definition: qofnumeric.c:44
qof_numeric_mul_with_error
QofNumeric qof_numeric_mul_with_error(QofNumeric a, QofNumeric b, gint64 denom, gint how, QofNumeric *error)
Definition: qofnumeric.c:1052
QOF_ERROR_ARG
Definition: qofnumeric.h:210
qof_numeric_convert_with_error
QofNumeric qof_numeric_convert_with_error(QofNumeric in, gint64 denom, gint how, QofNumeric *error)
qof_numeric_same
gint qof_numeric_same(QofNumeric a, QofNumeric b, gint64 denom, gint how)
Definition: qofnumeric.c:285
qof_numeric_mul
QofNumeric qof_numeric_mul(QofNumeric a, QofNumeric b, gint64 denom, gint how)
Definition: qofnumeric.c:405
qof_numeric_compare
gint qof_numeric_compare(QofNumeric a, QofNumeric b)
Definition: qofnumeric.c:174
qof_numeric_sub_with_error
QofNumeric qof_numeric_sub_with_error(QofNumeric a, QofNumeric b, gint64 denom, gint how, QofNumeric *error)
Definition: qofnumeric.c:1034
qof_numeric_error
QofNumeric qof_numeric_error(QofNumericErrorCode error_code)
Definition: qofnumeric.c:1004
qof_numeric_to_double
gdouble qof_numeric_to_double(QofNumeric in)
Definition: qofnumeric.c:991
qof_numeric_equal
gboolean qof_numeric_equal(QofNumeric a, QofNumeric b)
Definition: qofnumeric.c:233
qof_numeric_num
static gint64 qof_numeric_num(QofNumeric a)
Definition: qofnumeric.h:284
qof_numeric_div_with_error
QofNumeric qof_numeric_div_with_error(QofNumeric a, QofNumeric b, gint64 denom, gint how, QofNumeric *error)
Definition: qofnumeric.c:1071
qof_numeric_dbg_to_string
gchar * qof_numeric_dbg_to_string(QofNumeric n)
Definition: qofnumeric.c:1103
QOF_HOW_RND_CEIL
Definition: qofnumeric.h:135
qof_numeric_add_with_error
QofNumeric qof_numeric_add_with_error(QofNumeric a, QofNumeric b, gint64 denom, gint how, QofNumeric *error)
Definition: qofnumeric.c:1014
qof_numeric_abs
QofNumeric qof_numeric_abs(QofNumeric a)
Definition: qofnumeric.c:650
qof_numeric_denom
static gint64 qof_numeric_denom(QofNumeric a)
Definition: qofnumeric.h:291
QOF_HOW_RND_TRUNC
Definition: qofnumeric.h:138
qof_numeric_from_string
gboolean qof_numeric_from_string(const gchar *str, QofNumeric *n)
Definition: qofnumeric.c:1121
qof_numeric_reduce
QofNumeric qof_numeric_reduce(QofNumeric in)
Definition: qofnumeric.c:888
qof_numeric_sub_fixed
static QofNumeric qof_numeric_sub_fixed(QofNumeric a, QofNumeric b)
Definition: qofnumeric.h:422
QOF_HOW_RND_FLOOR
Definition: qofnumeric.h:132
_QofNumeric
Definition: qofnumeric.h:52
qof_numeric_create
static QofNumeric qof_numeric_create(gint64 num, gint64 denom)
Definition: qofnumeric.h:243
QOF_HOW_RND_NEVER
Definition: qofnumeric.h:163
qof_numeric_zero_p
gboolean qof_numeric_zero_p(QofNumeric a)
Definition: qofnumeric.c:119
qof_numeric_eq
gboolean qof_numeric_eq(QofNumeric a, QofNumeric b)
Definition: qofnumeric.c:223
QOF_HOW_DENOM_EXACT
Definition: qofnumeric.h:174
QOF_ERROR_OK
Definition: qofnumeric.h:209
qof_numeric_neg
QofNumeric qof_numeric_neg(QofNumeric a)
Definition: qofnumeric.c:637
qof_numeric_sub
QofNumeric qof_numeric_sub(QofNumeric a, QofNumeric b, gint64 denom, gint how)
Definition: qofnumeric.c:388
QOF_ERROR_REMAINDER
Definition: qofnumeric.h:218
qof_numeric_to_string
gchar * qof_numeric_to_string(QofNumeric n)
Definition: qofnumeric.c:1089
QofNumericErrorCode
QofNumericErrorCode
Definition: qofnumeric.h:207
QOF_HOW_RND_PROMOTE
Definition: qofnumeric.h:141
qof_numeric_negative_p
gboolean qof_numeric_negative_p(QofNumeric a)
Definition: qofnumeric.c:137
qof_numeric_convert
QofNumeric qof_numeric_convert(QofNumeric in, gint64 denom, gint how)
Definition: qofnumeric.c:662
qof_numeric_add
QofNumeric qof_numeric_add(QofNumeric a, QofNumeric b, gint64 denom, gint how)
Definition: qofnumeric.c:300
QOF_ERROR_DENOM_DIFF
Definition: qofnumeric.h:214
qof_numeric_from_double
QofNumeric qof_numeric_from_double(gdouble in, gint64 denom, gint how)
Definition: qofnumeric.c:919
qof_numeric_positive_p
gboolean qof_numeric_positive_p(QofNumeric a)
Definition: qofnumeric.c:155
QOF_HOW_RND_ROUND
Definition: qofnumeric.h:158
QOF_HOW_DENOM_REDUCE
Definition: qofnumeric.h:181
QOF_HOW_RND_ROUND_HALF_DOWN
Definition: qofnumeric.h:146
qof_numeric_zero
static QofNumeric qof_numeric_zero(void)
Definition: qofnumeric.h:253
qof_numeric_div
QofNumeric qof_numeric_div(QofNumeric x, QofNumeric y, gint64 denom, gint how)
Definition: qofnumeric.c:516
QOF_HOW_DENOM_FIXED
Definition: qofnumeric.h:192
QOF_ERROR_OVERFLOW
Definition: qofnumeric.h:211
qof_numeric_add_fixed
static QofNumeric qof_numeric_add_fixed(QofNumeric a, QofNumeric b)
Definition: qofnumeric.h:411
QOF_HOW_DENOM_LCD
Definition: qofnumeric.h:186
QOF_DENOM_AUTO
#define QOF_DENOM_AUTO
Definition: qofnumeric.h:231
QOF_HOW_RND_ROUND_HALF_UP
Definition: qofnumeric.h:151