GnuCash  4.8a-80-g9825132ea+
Data Structures

An exact-rational-number library for gnucash. More...

#include <glib-object.h>

Go to the source code of this file.

Data Structures

struct  gnc_numeric
 An rational-number type. More...
 

Functions

Constructors
gnc_numeric double_to_gnc_numeric (double n, gint64 denom, gint how)
 Convert a floating-point number to a gnc_numeric. More...
 
gboolean string_to_gnc_numeric (const gchar *str, gnc_numeric *n)
 Read a gnc_numeric from str, skipping any leading whitespace. More...
 
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 number.
 
const char * gnc_numeric_errorCode_to_string (GNCNumericErrorCode error_code)
 Returns a string representation of the given GNCNumericErrorCode.
 
Value Accessors
gdouble gnc_numeric_to_double (gnc_numeric n)
 Convert numeric to floating-point value. More...
 
gchar * gnc_numeric_to_string (gnc_numeric n)
 Convert to string. More...
 
gchar * gnc_num_dbg_to_string (gnc_numeric n)
 Convert to string. More...
 
Comparisons and Predicates
GNCNumericErrorCode gnc_numeric_check (gnc_numeric a)
 Check for error signal in value. More...
 
gint gnc_numeric_compare (gnc_numeric a, gnc_numeric b)
 Returns 1 if a>b, -1 if b>a, 0 if a == b.
 
gboolean gnc_numeric_zero_p (gnc_numeric a)
 Returns 1 if the given gnc_numeric is 0 (zero), else returns 0. More...
 
gboolean gnc_numeric_negative_p (gnc_numeric a)
 Returns 1 if a < 0, otherwise returns 0. More...
 
gboolean gnc_numeric_positive_p (gnc_numeric a)
 Returns 1 if a > 0, otherwise returns 0. More...
 
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 denominator)
 
gboolean gnc_numeric_equal (gnc_numeric a, gnc_numeric b)
 Equivalence predicate: Returns TRUE (1) if a and b represent the same number. More...
 
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, and compare numerators the results using gnc_numeric_equal. More...
 
Arithmetic Operations
gnc_numeric gnc_numeric_add (gnc_numeric a, gnc_numeric b, gint64 denom, gint how)
 Return a+b. More...
 
gnc_numeric gnc_numeric_sub (gnc_numeric a, gnc_numeric b, gint64 denom, gint how)
 Return a-b. More...
 
gnc_numeric gnc_numeric_mul (gnc_numeric a, gnc_numeric b, gint64 denom, gint how)
 Multiply a times b, returning the product. More...
 
gnc_numeric gnc_numeric_div (gnc_numeric x, gnc_numeric y, gint64 denom, gint how)
 Division. More...
 
gnc_numeric gnc_numeric_neg (gnc_numeric a)
 Returns a newly created gnc_numeric that is the negative of the given gnc_numeric value. More...
 
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. More...
 
Change Denominator
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 'denom' and 'how'.
 
gnc_numeric gnc_numeric_reduce (gnc_numeric n)
 Return input after reducing it by Greater Common Factor (GCF) elimination.
 
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. More...
 
gnc_numeric gnc_numeric_invert (gnc_numeric num)
 Invert a gnc_numeric. More...
 

Arguments Standard Arguments to most functions

Most of the gnc_numeric arithmetic functions take two arguments
in addition to their numeric args: 'denom', which is the denominator
to use in the output gnc_numeric object, and 'how'.

which describes how the arithmetic result is to be converted to that denominator. This combination of output denominator and rounding policy allows the results of financial and other rational computations to be properly rounded to the appropriate units.

Watch out: You must specify a rounding policy such as GNC_HOW_RND_NEVER, otherwise the fractional part of the input value might silently get discarded!

Valid values for denom are: GNC_DENOM_AUTO – compute denominator exactly integer n – Force the denominator of the result to be this integer GNC_DENOM_RECIPROCAL – Use 1/n as the denominator (???huh???)

Valid values for 'how' are bitwise combinations of zero or one "rounding instructions" with zero or one "denominator types". Valid rounding instructions are: GNC_HOW_RND_FLOOR GNC_HOW_RND_CEIL GNC_HOW_RND_TRUNC GNC_HOW_RND_PROMOTE GNC_HOW_RND_ROUND_HALF_DOWN GNC_HOW_RND_ROUND_HALF_UP GNC_HOW_RND_ROUND GNC_HOW_RND_NEVER

The denominator type specifies how to compute a denominator if GNC_DENOM_AUTO is specified as the 'denom'. Valid denominator types are: GNC_HOW_DENOM_EXACT GNC_HOW_DENOM_REDUCE GNC_HOW_DENOM_LCD GNC_HOW_DENOM_FIXED GNC_HOW_DENOM_SIGFIGS(N)

To use traditional rational-number operational semantics (all results are exact and are reduced to relatively-prime fractions) pass the argument GNC_DENOM_AUTO as 'denom' and GNC_HOW_DENOM_REDUCE| GNC_HOW_RND_NEVER as 'how'.

To enforce strict financial semantics (such that all operands must have the same denominator as each other and as the result), use GNC_DENOM_AUTO as 'denom' and GNC_HOW_DENOM_FIXED | GNC_HOW_RND_NEVER as 'how'.

#define GNC_NUMERIC_RND_MASK   0x0000000f
 bitmasks for HOW flags. More...
 
#define GNC_NUMERIC_DENOM_MASK   0x000000f0
 
#define GNC_NUMERIC_SIGFIGS_MASK   0x0000ff00
 
#define GNC_HOW_DENOM_SIGFIGS(n)   ( ((( n ) & 0xff) << 8) | GNC_HOW_DENOM_SIGFIG)
 Build a 'how' value that will generate a denominator that will keep at least n significant figures in the result.
 
#define GNC_HOW_GET_SIGFIGS(a)   ( (( a ) & 0xff00 ) >> 8)
 
#define GNC_DENOM_AUTO   0
 Values that can be passed as the 'denom' argument. More...
 
enum  {
  GNC_HOW_RND_FLOOR = 0x01, GNC_HOW_RND_CEIL = 0x02, GNC_HOW_RND_TRUNC = 0x03, GNC_HOW_RND_PROMOTE = 0x04,
  GNC_HOW_RND_ROUND_HALF_DOWN = 0x05, GNC_HOW_RND_ROUND_HALF_UP = 0x06, GNC_HOW_RND_ROUND = 0x07, GNC_HOW_RND_NEVER = 0x08
}
 Rounding/Truncation modes for operations. More...
 
enum  {
  GNC_HOW_DENOM_EXACT = 0x10, GNC_HOW_DENOM_REDUCE = 0x20, GNC_HOW_DENOM_LCD = 0x30, GNC_HOW_DENOM_FIXED = 0x40,
  GNC_HOW_DENOM_SIGFIG = 0x50
}
 How to compute a denominator, or'ed into the "how" field. More...
 
enum  GNCNumericErrorCode {
  GNC_ERROR_OK = 0, GNC_ERROR_ARG = -1, GNC_ERROR_OVERFLOW = -2, GNC_ERROR_DENOM_DIFF = -3,
  GNC_ERROR_REMAINDER = -4
}
 Error codes. More...
 

GValue

#define GNC_TYPE_NUMERIC   (gnc_numeric_get_type ())
 
GType gnc_numeric_get_type (void)
 

Detailed Description

An exact-rational-number library for gnucash.

(to be renamed qofnumeric.h in libqof2)

Author
Copyright (C) 2000 Bill Gribble
Copyright (C) 2004 Linas Vepstas linas.nosp@m.@lin.nosp@m.as.or.nosp@m.g

Definition in file gnc-numeric.h.