GnuCash  5.6-150-g038405b370+
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 
48 #ifndef GNC_NUMERIC_H
49 #define GNC_NUMERIC_H
50 
51 #include <glib-object.h>
52 
53 #ifdef __cplusplus
54 extern "C"
55 {
56 #endif
57 
59 {
60  gint64 num;
61  gint64 denom;
62 };
63 
67 typedef struct _gnc_numeric gnc_numeric;
68 
126 #define GNC_NUMERIC_RND_MASK 0x0000000f
127 #define GNC_NUMERIC_DENOM_MASK 0x000000f0
128 #define GNC_NUMERIC_SIGFIGS_MASK 0x0000ff00
129 
143 enum
144 {
147 
150 
153 
156 
161 
166 
173 
178 };
179 
181 enum
182 {
189 
196 
201 
207 
212 };
213 
217 #define GNC_HOW_DENOM_SIGFIGS( n ) ( ((( n ) & 0xff) << 8) | GNC_HOW_DENOM_SIGFIG)
218 #define GNC_HOW_GET_SIGFIGS( a ) ( (( a ) & 0xff00 ) >> 8)
219 
221 typedef enum
222 {
229 
234 
235 
245 #define GNC_DENOM_AUTO 0
246 
254 static inline
255 gnc_numeric gnc_numeric_create(gint64 num, gint64 denom)
256 {
257  gnc_numeric out;
258  out.num = num;
259  out.denom = denom;
260  return out;
261 }
262 
264 static inline
265 gnc_numeric gnc_numeric_zero(void)
266 {
267  return gnc_numeric_create(0, 1);
268 }
269 
293 gnc_numeric double_to_gnc_numeric(double n, gint64 denom,
294  gint how);
295 
299 gnc_numeric gnc_numeric_from_string (const gchar* str);
303 gnc_numeric gnc_numeric_error(GNCNumericErrorCode error_code);
304 
314 static inline
315 gint64 gnc_numeric_num(gnc_numeric a)
316 {
317  return a.num;
318 }
320 static inline
321 gint64 gnc_numeric_denom(gnc_numeric a)
322 {
323  return a.denom;
324 }
325 
327 gdouble gnc_numeric_to_double(gnc_numeric n);
328 
331 gchar *gnc_numeric_to_string(gnc_numeric n);
332 
335 gchar * gnc_num_dbg_to_string(gnc_numeric n);
346 
348 gint gnc_numeric_compare(gnc_numeric a, gnc_numeric b);
349 
351 gboolean gnc_numeric_zero_p(gnc_numeric a);
352 
354 gboolean gnc_numeric_negative_p(gnc_numeric a);
355 
357 gboolean gnc_numeric_positive_p(gnc_numeric a);
358 
362 gboolean gnc_numeric_eq(gnc_numeric a, gnc_numeric b);
363 
368 gboolean gnc_numeric_equal(gnc_numeric a, gnc_numeric b);
369 
382 gint gnc_numeric_same(gnc_numeric a, gnc_numeric b,
383  gint64 denom, gint how);
390 gnc_numeric gnc_numeric_add(gnc_numeric a, gnc_numeric b,
391  gint64 denom, gint how);
392 
394 gnc_numeric gnc_numeric_sub(gnc_numeric a, gnc_numeric b,
395  gint64 denom, gint how);
396 
402 gnc_numeric gnc_numeric_mul(gnc_numeric a, gnc_numeric b,
403  gint64 denom, gint how);
404 
412 gnc_numeric gnc_numeric_div(gnc_numeric x, gnc_numeric y,
413  gint64 denom, gint how);
417 gnc_numeric gnc_numeric_neg(gnc_numeric a);
418 
422 gnc_numeric gnc_numeric_abs(gnc_numeric a);
423 
428 static inline
429 gnc_numeric gnc_numeric_add_fixed(gnc_numeric a, gnc_numeric b)
430 {
431  return gnc_numeric_add(a, b, GNC_DENOM_AUTO,
433 }
434 
439 static inline
440 gnc_numeric gnc_numeric_sub_fixed(gnc_numeric a, gnc_numeric b)
441 {
442  return gnc_numeric_sub(a, b, GNC_DENOM_AUTO,
444 }
455 gnc_numeric gnc_numeric_convert(gnc_numeric n, gint64 denom,
456  gint how);
457 
460 gnc_numeric gnc_numeric_reduce(gnc_numeric n);
461 
474 gboolean gnc_numeric_to_decimal(gnc_numeric * a,
475  guint8 * max_decimal_places);
476 
482 gnc_numeric gnc_numeric_invert (gnc_numeric num);
488 GType gnc_numeric_get_type( void );
489 #define GNC_TYPE_NUMERIC (gnc_numeric_get_type ())
490 
491 #ifdef __cplusplus
492 }
493 #endif
494 
495 #endif
GNC_HOW_RND_NEVER was specified, but the result could not be converted to the desired denominator wit...
Definition: gnc-numeric.h:232
Never round at all, and signal an error if there is a fractional result in a computation.
Definition: gnc-numeric.h:177
gboolean gnc_numeric_equal(gnc_numeric a, gnc_numeric b)
Equivalence predicate: Returns TRUE (1) if a and b represent the same number.
Round toward +infinity.
Definition: gnc-numeric.h:149
gchar * gnc_num_dbg_to_string(gnc_numeric n)
Convert to string.
gnc_numeric double_to_gnc_numeric(double n, gint64 denom, gint how)
Convert a floating-point number to a gnc_numeric.
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:58
All arguments are required to have the same denominator, that denominator is to be used in the output...
Definition: gnc-numeric.h:206
GNCNumericErrorCode
Error codes.
Definition: gnc-numeric.h:221
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.
Intermediate result overflow.
Definition: gnc-numeric.h:225
Use any denominator which gives an exactly correct ratio of numerator to denominator.
Definition: gnc-numeric.h:188
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.
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.
Round toward -infinity.
Definition: gnc-numeric.h:146
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;...
Find the least common multiple of the arguments&#39; denominators and use that as the denominator of the ...
Definition: gnc-numeric.h:200
Reduce the result value by common factor elimination, using the smallest possible value for the denom...
Definition: gnc-numeric.h:195
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:224
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...
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.
gnc_numeric gnc_numeric_sub(gnc_numeric a, gnc_numeric b, gint64 denom, gint how)
Return a-b.
Truncate fractions (round toward zero)
Definition: gnc-numeric.h:152
Promote fractions (round away from zero)
Definition: gnc-numeric.h:155
GNC_HOW_DENOM_FIXED was specified, but argument denominators differed.
Definition: gnc-numeric.h:228
Round to the nearest integer, rounding toward zero when there are two equidistant nearest integers...
Definition: gnc-numeric.h:160
gnc_numeric gnc_numeric_from_string(const gchar *str)
Read a gnc_numeric from str, skipping any leading whitespace.
Round to the nearest integer, rounding away from zero when there are two equidistant nearest integers...
Definition: gnc-numeric.h:165
GNCNumericErrorCode gnc_numeric_check(gnc_numeric a)
Check for error signal in value.
Use unbiased ("banker&#39;s") rounding.
Definition: gnc-numeric.h:172
Round to the number of significant figures given in the rounding instructions by the GNC_HOW_DENOM_SI...
Definition: gnc-numeric.h:211
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:223
#define GNC_DENOM_AUTO
Values that can be passed as the &#39;denom&#39; argument.
Definition: gnc-numeric.h:245