GnuCash c935c2f+
Loading...
Searching...
No Matches
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
54extern "C"
55{
56#endif
57
59{
60 gint64 num;
61 gint64 denom;
62};
63
67typedef 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
143enum
144{
147
150
153
156
161
166
173
177 GNC_HOW_RND_NEVER = 0x08
179
181enum
182{
189
196
201
207
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
234
235
245#define GNC_DENOM_AUTO 0
246
254static inline
255gnc_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
264static inline
265gnc_numeric gnc_numeric_zero(void)
266{
267 return gnc_numeric_create(0, 1);
268}
269
293gnc_numeric double_to_gnc_numeric(double n, gint64 denom,
294 gint how);
295
299gnc_numeric gnc_numeric_from_string (const gchar* str);
303gnc_numeric gnc_numeric_error(GNCNumericErrorCode error_code);
304
314static inline
315gint64 gnc_numeric_num(gnc_numeric a)
316{
317 return a.num;
318}
320static inline
321gint64 gnc_numeric_denom(gnc_numeric a)
322{
323 return a.denom;
324}
325
327gdouble gnc_numeric_to_double(gnc_numeric n);
328
331gchar *gnc_numeric_to_string(gnc_numeric n);
332
335gchar * gnc_num_dbg_to_string(gnc_numeric n);
346
348gint gnc_numeric_compare(gnc_numeric a, gnc_numeric b);
349
351gboolean gnc_numeric_zero_p(gnc_numeric a);
352
354gboolean gnc_numeric_negative_p(gnc_numeric a);
355
357gboolean gnc_numeric_positive_p(gnc_numeric a);
358
362gboolean gnc_numeric_eq(gnc_numeric a, gnc_numeric b);
363
368gboolean gnc_numeric_equal(gnc_numeric a, gnc_numeric b);
369
382gint gnc_numeric_same(gnc_numeric a, gnc_numeric b,
383 gint64 denom, gint how);
390gnc_numeric gnc_numeric_add(gnc_numeric a, gnc_numeric b,
391 gint64 denom, gint how);
392
394gnc_numeric gnc_numeric_sub(gnc_numeric a, gnc_numeric b,
395 gint64 denom, gint how);
396
402gnc_numeric gnc_numeric_mul(gnc_numeric a, gnc_numeric b,
403 gint64 denom, gint how);
404
412gnc_numeric gnc_numeric_div(gnc_numeric x, gnc_numeric y,
413 gint64 denom, gint how);
417gnc_numeric gnc_numeric_neg(gnc_numeric a);
418
422gnc_numeric gnc_numeric_abs(gnc_numeric a);
423
428static inline
429gnc_numeric gnc_numeric_add_fixed(gnc_numeric a, gnc_numeric b)
430{
431 return gnc_numeric_add(a, b, GNC_DENOM_AUTO,
433}
434
439static inline
440gnc_numeric gnc_numeric_sub_fixed(gnc_numeric a, gnc_numeric b)
441{
442 return gnc_numeric_sub(a, b, GNC_DENOM_AUTO,
444}
455gnc_numeric gnc_numeric_convert(gnc_numeric n, gint64 denom,
456 gint how);
457
460gnc_numeric gnc_numeric_reduce(gnc_numeric n);
461
474gboolean gnc_numeric_to_decimal(gnc_numeric * a,
475 guint8 * max_decimal_places);
476
482gnc_numeric gnc_numeric_invert (gnc_numeric num);
488GType gnc_numeric_get_type( void );
489#define GNC_TYPE_NUMERIC (gnc_numeric_get_type ())
490
491#ifdef __cplusplus
492}
493#endif
494
495#endif
GNCNumericErrorCode
Error codes.
const char * gnc_numeric_errorCode_to_string(GNCNumericErrorCode error_code)
Returns a string representation of the given GNCNumericErrorCode.
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.
gdouble gnc_numeric_to_double(gnc_numeric n)
Convert numeric to floating-point value.
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 ...
gnc_numeric gnc_numeric_sub(gnc_numeric a, gnc_numeric b, gint64 denom, gint how)
Return a-b.
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,...
gchar * gnc_numeric_to_string(gnc_numeric n)
Convert to string.
#define GNC_DENOM_AUTO
Values that can be passed as the 'denom' argument.
gnc_numeric double_to_gnc_numeric(double in, gint64 denom, gint how)
Convert a floating-point number to a gnc_numeric.
gnc_numeric gnc_numeric_div(gnc_numeric x, gnc_numeric y, gint64 denom, gint how)
Division.
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...
gnc_numeric gnc_numeric_from_string(const gchar *str)
Read a gnc_numeric from str, skipping any leading whitespace.
GNCNumericErrorCode gnc_numeric_check(gnc_numeric in)
Check for error signal in value.
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_mul(gnc_numeric a, gnc_numeric b, gint64 denom, gint how)
Multiply a times b, returning the product.
gboolean gnc_numeric_negative_p(gnc_numeric a)
Returns 1 if a < 0, otherwise returns 0.
gboolean gnc_numeric_zero_p(gnc_numeric a)
Returns 1 if the given gnc_numeric is 0 (zero), else returns 0.
gint gnc_numeric_compare(gnc_numeric a, gnc_numeric b)
Returns 1 if a>b, -1 if b>a, 0 if a == b
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 '...
gnc_numeric gnc_numeric_neg(gnc_numeric a)
Returns a newly created gnc_numeric that is the negative of the given gnc_numeric value.
gboolean gnc_numeric_equal(gnc_numeric a, gnc_numeric b)
Equivalence predicate: Returns TRUE (1) if a and b represent the same number.
gnc_numeric gnc_numeric_invert(gnc_numeric num)
Invert a gnc_numeric.
gnc_numeric gnc_numeric_reduce(gnc_numeric n)
Return input after reducing it by Greater Common Factor (GCF) elimination.
gchar * gnc_num_dbg_to_string(gnc_numeric n)
Convert to string.
gboolean gnc_numeric_positive_p(gnc_numeric a)
Returns 1 if a > 0, otherwise returns 0.
gnc_numeric gnc_numeric_add(gnc_numeric a, gnc_numeric b, gint64 denom, gint how)
Return a+b.
@ GNC_ERROR_REMAINDER
GNC_HOW_RND_NEVER was specified, but the result could not be converted to the desired denominator wit...
@ GNC_ERROR_OVERFLOW
Intermediate result overflow.
@ GNC_ERROR_DENOM_DIFF
GNC_HOW_DENOM_FIXED was specified, but argument denominators differed.
@ GNC_ERROR_OK
No error.
@ GNC_ERROR_ARG
Argument is not a valid number.
@ GNC_HOW_RND_ROUND_HALF_UP
Round to the nearest integer, rounding away from zero when there are two equidistant nearest integers...
@ GNC_HOW_RND_ROUND_HALF_DOWN
Round to the nearest integer, rounding toward zero when there are two equidistant nearest integers.
@ GNC_HOW_RND_FLOOR
Round toward -infinity.
@ GNC_HOW_RND_PROMOTE
Promote fractions (round away from zero)
@ GNC_HOW_RND_NEVER
Never round at all, and signal an error if there is a fractional result in a computation.
@ GNC_HOW_RND_CEIL
Round toward +infinity.
@ GNC_HOW_RND_TRUNC
Truncate fractions (round toward zero)
@ GNC_HOW_RND_ROUND
Use unbiased ("banker's") rounding.
@ GNC_HOW_DENOM_REDUCE
Reduce the result value by common factor elimination, using the smallest possible value for the denom...
@ GNC_HOW_DENOM_FIXED
All arguments are required to have the same denominator, that denominator is to be used in the output...
@ GNC_HOW_DENOM_LCD
Find the least common multiple of the arguments' denominators and use that as the denominator of the ...
@ GNC_HOW_DENOM_SIGFIG
Round to the number of significant figures given in the rounding instructions by the GNC_HOW_DENOM_SI...
@ GNC_HOW_DENOM_EXACT
Use any denominator which gives an exactly correct ratio of numerator to denominator.
An rational-number type.
Definition gnc-numeric.h:59