GnuCash  4.8a-176-g88ecf8dd1
gnc-numeric.h
Go to the documentation of this file.
1 /********************************************************************
2  * gnc-numeric.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 
50 #ifndef GNC_NUMERIC_H
51 #define GNC_NUMERIC_H
52 #ifdef __cplusplus
53 extern "C"
54 {
55 #endif
56 
57 #include <glib-object.h>
58 
60 {
61  gint64 num;
62  gint64 denom;
63 };
64 
68 typedef struct _gnc_numeric gnc_numeric;
69 
127 #define GNC_NUMERIC_RND_MASK 0x0000000f
128 #define GNC_NUMERIC_DENOM_MASK 0x000000f0
129 #define GNC_NUMERIC_SIGFIGS_MASK 0x0000ff00
130 
144 enum
145 {
148 
151 
154 
157 
162 
167 
174 
179 };
180 
182 enum
183 {
190 
197 
202 
208 
213 };
214 
218 #define GNC_HOW_DENOM_SIGFIGS( n ) ( ((( n ) & 0xff) << 8) | GNC_HOW_DENOM_SIGFIG)
219 #define GNC_HOW_GET_SIGFIGS( a ) ( (( a ) & 0xff00 ) >> 8)
220 
222 typedef enum
223 {
230 
235 
236 
246 #define GNC_DENOM_AUTO 0
247 
255 static inline
256 gnc_numeric gnc_numeric_create(gint64 num, gint64 denom)
257 {
258  gnc_numeric out;
259  out.num = num;
260  out.denom = denom;
261  return out;
262 }
263 
265 static inline
266 gnc_numeric gnc_numeric_zero(void)
267 {
268  return gnc_numeric_create(0, 1);
269 }
270 
294 gnc_numeric double_to_gnc_numeric(double n, gint64 denom,
295  gint how);
296 
300 gboolean string_to_gnc_numeric(const gchar* str, gnc_numeric *n);
301 
305 gnc_numeric gnc_numeric_error(GNCNumericErrorCode error_code);
306 
316 static inline
317 gint64 gnc_numeric_num(gnc_numeric a)
318 {
319  return a.num;
320 }
322 static inline
323 gint64 gnc_numeric_denom(gnc_numeric a)
324 {
325  return a.denom;
326 }
327 
329 gdouble gnc_numeric_to_double(gnc_numeric n);
330 
333 gchar *gnc_numeric_to_string(gnc_numeric n);
334 
337 gchar * gnc_num_dbg_to_string(gnc_numeric n);
348 
350 gint gnc_numeric_compare(gnc_numeric a, gnc_numeric b);
351 
353 gboolean gnc_numeric_zero_p(gnc_numeric a);
354 
356 gboolean gnc_numeric_negative_p(gnc_numeric a);
357 
359 gboolean gnc_numeric_positive_p(gnc_numeric a);
360 
364 gboolean gnc_numeric_eq(gnc_numeric a, gnc_numeric b);
365 
370 gboolean gnc_numeric_equal(gnc_numeric a, gnc_numeric b);
371 
384 gint gnc_numeric_same(gnc_numeric a, gnc_numeric b,
385  gint64 denom, gint how);
392 gnc_numeric gnc_numeric_add(gnc_numeric a, gnc_numeric b,
393  gint64 denom, gint how);
394 
396 gnc_numeric gnc_numeric_sub(gnc_numeric a, gnc_numeric b,
397  gint64 denom, gint how);
398 
404 gnc_numeric gnc_numeric_mul(gnc_numeric a, gnc_numeric b,
405  gint64 denom, gint how);
406 
414 gnc_numeric gnc_numeric_div(gnc_numeric x, gnc_numeric y,
415  gint64 denom, gint how);
419 gnc_numeric gnc_numeric_neg(gnc_numeric a);
420 
424 gnc_numeric gnc_numeric_abs(gnc_numeric a);
425 
430 static inline
431 gnc_numeric gnc_numeric_add_fixed(gnc_numeric a, gnc_numeric b)
432 {
433  return gnc_numeric_add(a, b, GNC_DENOM_AUTO,
435 }
436 
441 static inline
442 gnc_numeric gnc_numeric_sub_fixed(gnc_numeric a, gnc_numeric b)
443 {
444  return gnc_numeric_sub(a, b, GNC_DENOM_AUTO,
446 }
457 gnc_numeric gnc_numeric_convert(gnc_numeric n, gint64 denom,
458  gint how);
459 
462 gnc_numeric gnc_numeric_reduce(gnc_numeric n);
463 
476 gboolean gnc_numeric_to_decimal(gnc_numeric * a,
477  guint8 * max_decimal_places);
478 
484 gnc_numeric gnc_numeric_invert (gnc_numeric num);
490 GType gnc_numeric_get_type( void );
491 #define GNC_TYPE_NUMERIC (gnc_numeric_get_type ())
492 
493 #ifdef __cplusplus
494 }
495 #endif
496 
497 #endif
GNC_HOW_RND_NEVER was specified, but the result could not be converted to the desired denominator wit...
Definition: gnc-numeric.h:233
Truncate fractions (round toward zero)
Definition: gnc-numeric.h:153
Reduce the result value by common factor elimination, using the smallest possible value for the denom...
Definition: gnc-numeric.h:196
gboolean gnc_numeric_equal(gnc_numeric a, gnc_numeric b)
Equivalence predicate: Returns TRUE (1) if a and b represent the same number.
gchar * gnc_num_dbg_to_string(gnc_numeric n)
Convert to string.
Round toward -infinity.
Definition: gnc-numeric.h:147
gnc_numeric double_to_gnc_numeric(double n, gint64 denom, gint how)
Convert a floating-point number to a gnc_numeric.
Round to the nearest integer, rounding toward zero when there are two equidistant nearest integers...
Definition: gnc-numeric.h:161
gnc_numeric gnc_numeric_neg(gnc_numeric a)
Returns a newly created gnc_numeric that is the negative of the given gnc_numeric value...
An rational-number type.
Definition: gnc-numeric.h:59
GNCNumericErrorCode
Error codes.
Definition: gnc-numeric.h:222
gnc_numeric gnc_numeric_add(gnc_numeric a, gnc_numeric b, gint64 denom, gint how)
Return a+b.
gboolean gnc_numeric_to_decimal(gnc_numeric *a, guint8 *max_decimal_places)
Attempt to convert the denominator to an exact power of ten without rounding.
gboolean gnc_numeric_zero_p(gnc_numeric a)
Returns 1 if the given gnc_numeric is 0 (zero), else returns 0.
Use any denominator which gives an exactly correct ratio of numerator to denominator.
Definition: gnc-numeric.h:189
Intermediate result overflow.
Definition: gnc-numeric.h:226
gint gnc_numeric_compare(gnc_numeric a, gnc_numeric b)
Returns 1 if a>b, -1 if b>a, 0 if a == b.
gchar * gnc_numeric_to_string(gnc_numeric n)
Convert to string.
Round to the nearest integer, rounding away from zero when there are two equidistant nearest integers...
Definition: gnc-numeric.h:166
gboolean string_to_gnc_numeric(const gchar *str, gnc_numeric *n)
Read a gnc_numeric from str, skipping any leading whitespace.
gboolean gnc_numeric_negative_p(gnc_numeric a)
Returns 1 if a < 0, otherwise returns 0.
gnc_numeric gnc_numeric_reduce(gnc_numeric n)
Return input after reducing it by Greater Common Factor (GCF) elimination.
gdouble gnc_numeric_to_double(gnc_numeric n)
Convert numeric to floating-point value.
gnc_numeric gnc_numeric_invert(gnc_numeric num)
Invert a gnc_numeric.
gnc_numeric gnc_numeric_convert(gnc_numeric n, gint64 denom, gint how)
Change the denominator of a gnc_numeric value to the specified denominator under standard arguments &#39;...
Promote fractions (round away from zero)
Definition: gnc-numeric.h:156
gnc_numeric gnc_numeric_mul(gnc_numeric a, gnc_numeric b, gint64 denom, gint how)
Multiply a times b, returning the product.
const char * gnc_numeric_errorCode_to_string(GNCNumericErrorCode error_code)
Returns a string representation of the given GNCNumericErrorCode.
gnc_numeric gnc_numeric_error(GNCNumericErrorCode error_code)
Create a gnc_numeric object that signals the error condition noted by error_code, rather than a numbe...
Argument is not a valid number.
Definition: gnc-numeric.h:225
Use unbiased ("banker&#39;s") rounding.
Definition: gnc-numeric.h:173
gnc_numeric gnc_numeric_abs(gnc_numeric a)
Returns a newly created gnc_numeric that is the absolute value of the given gnc_numeric value...
Find the least common multiple of the arguments&#39; denominators and use that as the denominator of the ...
Definition: gnc-numeric.h:201
gnc_numeric gnc_numeric_div(gnc_numeric x, gnc_numeric y, gint64 denom, gint how)
Division.
gboolean gnc_numeric_eq(gnc_numeric a, gnc_numeric b)
Equivalence predicate: Returns TRUE (1) if a and b are exactly the same (have the same numerator and ...
gboolean gnc_numeric_positive_p(gnc_numeric a)
Returns 1 if a > 0, otherwise returns 0.
All arguments are required to have the same denominator, that denominator is to be used in the output...
Definition: gnc-numeric.h:207
Never round at all, and signal an error if there is a fractional result in a computation.
Definition: gnc-numeric.h:178
gnc_numeric gnc_numeric_sub(gnc_numeric a, gnc_numeric b, gint64 denom, gint how)
Return a-b.
GNC_HOW_DENOM_FIXED was specified, but argument denominators differed.
Definition: gnc-numeric.h:229
GNCNumericErrorCode gnc_numeric_check(gnc_numeric a)
Check for error signal in value.
Round to the number of significant figures given in the rounding instructions by the GNC_HOW_DENOM_SI...
Definition: gnc-numeric.h:212
Round toward +infinity.
Definition: gnc-numeric.h:150
gint gnc_numeric_same(gnc_numeric a, gnc_numeric b, gint64 denom, gint how)
Equivalence predicate: Convert both a and b to denom using the specified DENOM and method HOW...
No error.
Definition: gnc-numeric.h:224
#define GNC_DENOM_AUTO
Values that can be passed as the &#39;denom&#39; argument.
Definition: gnc-numeric.h:246