QOF  0.8.7
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 
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 {
133 
136 
139 
142 
147 
152 
159 
164 };
165 
167 enum
168 {
175 
182 
187 
193 
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 {
215 
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
254 {
255  return qof_numeric_create (0, 1);
256 }
257 
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 
283 static inline gint64
285 {
286  return a.num;
287 }
288 
290 static inline gint64
292 {
293  return a.denom;
294 }
295 
297 gdouble
299 
302 gchar *
304 
307 gchar *
320 
322 gint
324 
326 gboolean
328 
330 gboolean
332 
334 gboolean
336 
340 gboolean
342 
347 gboolean
349 
362 gint
363 qof_numeric_same (QofNumeric a, QofNumeric b, gint64 denom, gint how);
370 QofNumeric
372  gint64 denom, gint how);
373 
375 QofNumeric
377  gint64 denom, gint how);
378 
384 QofNumeric
386  gint64 denom, gint how);
387 
395 QofNumeric
397  gint64 denom, gint how);
399 QofNumeric
401 
403 QofNumeric
405 
410 static inline QofNumeric
412 {
413  return qof_numeric_add (a, b, QOF_DENOM_AUTO,
415 }
416 
421 static inline QofNumeric
423 {
424  return qof_numeric_sub (a, b, QOF_DENOM_AUTO,
426 }
427 
435 QofNumeric
437  gint64 denom, gint how,
438  QofNumeric * error);
439 
442 QofNumeric
444  gint64 denom, gint how,
445  QofNumeric * error);
446 
450 QofNumeric
452  gint64 denom, gint how,
453  QofNumeric * error);
454 
458 QofNumeric
460  gint64 denom, gint how,
461  QofNumeric * error);
471 QofNumeric
472 qof_numeric_convert (QofNumeric in, gint64 denom, gint how);
473 
477 QofNumeric
479  gint how, QofNumeric * error);
480 
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
QofNumericErrorCode qof_numeric_check(QofNumeric a)
Definition: qofnumeric.c:39
QofNumeric qof_numeric_mul_with_error(QofNumeric a, QofNumeric b, gint64 denom, gint how, QofNumeric *error)
Definition: qofnumeric.c:1047
QofNumeric qof_numeric_convert_with_error(QofNumeric in, gint64 denom, gint how, QofNumeric *error)
gint qof_numeric_same(QofNumeric a, QofNumeric b, gint64 denom, gint how)
Definition: qofnumeric.c:280
QofNumeric qof_numeric_mul(QofNumeric a, QofNumeric b, gint64 denom, gint how)
Definition: qofnumeric.c:400
gint qof_numeric_compare(QofNumeric a, QofNumeric b)
Definition: qofnumeric.c:169
QofNumeric qof_numeric_sub_with_error(QofNumeric a, QofNumeric b, gint64 denom, gint how, QofNumeric *error)
Definition: qofnumeric.c:1029
QofNumeric qof_numeric_error(QofNumericErrorCode error_code)
Definition: qofnumeric.c:999
gdouble qof_numeric_to_double(QofNumeric in)
Definition: qofnumeric.c:986
gboolean qof_numeric_equal(QofNumeric a, QofNumeric b)
Definition: qofnumeric.c:228
static gint64 qof_numeric_num(QofNumeric a)
Definition: qofnumeric.h:284
QofNumeric qof_numeric_div_with_error(QofNumeric a, QofNumeric b, gint64 denom, gint how, QofNumeric *error)
Definition: qofnumeric.c:1066
gchar * qof_numeric_dbg_to_string(QofNumeric n)
Definition: qofnumeric.c:1098
QofNumeric qof_numeric_add_with_error(QofNumeric a, QofNumeric b, gint64 denom, gint how, QofNumeric *error)
Definition: qofnumeric.c:1009
QofNumeric qof_numeric_abs(QofNumeric a)
Definition: qofnumeric.c:645
static gint64 qof_numeric_denom(QofNumeric a)
Definition: qofnumeric.h:291
gboolean qof_numeric_from_string(const gchar *str, QofNumeric *n)
Definition: qofnumeric.c:1116
QofNumeric qof_numeric_reduce(QofNumeric in)
Definition: qofnumeric.c:883
static QofNumeric qof_numeric_sub_fixed(QofNumeric a, QofNumeric b)
Definition: qofnumeric.h:422
static QofNumeric qof_numeric_create(gint64 num, gint64 denom)
Definition: qofnumeric.h:243
gboolean qof_numeric_zero_p(QofNumeric a)
Definition: qofnumeric.c:114
gboolean qof_numeric_eq(QofNumeric a, QofNumeric b)
Definition: qofnumeric.c:218
QofNumeric qof_numeric_neg(QofNumeric a)
Definition: qofnumeric.c:632
QofNumeric qof_numeric_sub(QofNumeric a, QofNumeric b, gint64 denom, gint how)
Definition: qofnumeric.c:383
gchar * qof_numeric_to_string(QofNumeric n)
Definition: qofnumeric.c:1084
QofNumericErrorCode
Definition: qofnumeric.h:207
gboolean qof_numeric_negative_p(QofNumeric a)
Definition: qofnumeric.c:132
QofNumeric qof_numeric_convert(QofNumeric in, gint64 denom, gint how)
Definition: qofnumeric.c:657
QofNumeric qof_numeric_add(QofNumeric a, QofNumeric b, gint64 denom, gint how)
Definition: qofnumeric.c:295
QofNumeric qof_numeric_from_double(gdouble in, gint64 denom, gint how)
Definition: qofnumeric.c:914
gboolean qof_numeric_positive_p(QofNumeric a)
Definition: qofnumeric.c:150
static QofNumeric qof_numeric_zero(void)
Definition: qofnumeric.h:253
QofNumeric qof_numeric_div(QofNumeric x, QofNumeric y, gint64 denom, gint how)
Definition: qofnumeric.c:511
static QofNumeric qof_numeric_add_fixed(QofNumeric a, QofNumeric b)
Definition: qofnumeric.h:411
#define QOF_DENOM_AUTO
Definition: qofnumeric.h:231