-- make the gnc-numeric error codes be a typdef enum

-- try to inline the numeric-check for slightly better performance


git-svn-id: svn+ssh://svn.gnucash.org/repo/gnucash/trunk@9978 57a11ea4-9604-0410-9ed3-97b8803252fd
This commit is contained in:
Linas Vepstas 2004-05-29 22:43:49 +00:00
parent bd7a8b732d
commit c5c92f5099
2 changed files with 45 additions and 33 deletions

View File

@ -53,6 +53,30 @@ static const char * _numeric_error_strings[] =
static gint64 gnc_numeric_lcd(gnc_numeric a, gnc_numeric b);
/* This function is small, simple, and used everywhere below,
* lets try to inline it.
*/
inline GNCNumericErrorCode
gnc_numeric_check(gnc_numeric in)
{
if(in.denom != 0)
{
return GNC_ERROR_OK;
}
else if(in.num)
{
if ((0 < in.num) || (-4 > in.num))
{
in.num = (gint64) GNC_ERROR_OVERFLOW;
}
return (GNCNumericErrorCode) in.num;
}
else
{
return GNC_ERROR_ARG;
}
}
/********************************************************************
* gnc_numeric_zero_p
********************************************************************/
@ -375,10 +399,12 @@ gnc_numeric_mul(gnc_numeric a, gnc_numeric b,
product.num = a.num*b.num;
product.denom = a.denom*b.denom;
#if 0 /* currently, product denom won't ever be zero */
if(product.denom < 0) {
product.num = -product.num;
product.denom = -product.denom;
}
#endif
if((denom == GNC_DENOM_AUTO) &&
((how & GNC_NUMERIC_DENOM_MASK) == GNC_DENOM_LCD))
@ -963,10 +989,8 @@ gnc_numeric_create(gint64 num, gint64 denom) {
********************************************************************/
gnc_numeric
gnc_numeric_error(int error_code) {
if(abs(error_code) < 5) {
/* PERR("%s", _numeric_error_strings[ - error_code]); */
}
gnc_numeric_error(GNCNumericErrorCode error_code)
{
return gnc_numeric_create(error_code, 0LL);
}
@ -1084,19 +1108,6 @@ gnc_numeric_div_with_error(gnc_numeric a, gnc_numeric b,
return quot;
}
int
gnc_numeric_check(gnc_numeric in) {
if(in.denom != 0) {
return GNC_ERROR_OK;
}
else if(in.num) {
return in.num;
}
else {
return GNC_ERROR_ARG;
}
}
/********************************************************************
* gnc_numeric text IO
********************************************************************/

View File

@ -54,7 +54,10 @@ struct _gnc_numeric {
* This is a rational number, defined by nominator and denominator. */
typedef struct _gnc_numeric gnc_numeric;
/** bitmasks for HOW flags */
/** bitmasks for HOW flags.
* bits 8-15 of 'how' are reserved for the number of significant
* digits to use in the output with GNC_DENOM_SIGFIG */
#define GNC_NUMERIC_RND_MASK 0x0000000f
#define GNC_NUMERIC_DENOM_MASK 0x000000f0
#define GNC_NUMERIC_SIGFIGS_MASK 0x0000ff00
@ -80,17 +83,14 @@ enum {
GNC_DENOM_SIGFIG = 0x50
};
/** bits 8-15 of 'how' are reserved for the number of significant
* digits to use in the output with GNC_DENOM_SIGFIG */
/** errors */
enum {
GNC_ERROR_OK = 0,
GNC_ERROR_ARG = -1,
GNC_ERROR_OVERFLOW = -2,
GNC_ERROR_DENOM_DIFF = -3,
GNC_ERROR_REMAINDER = -4
};
/** Error codes */
typedef enum {
GNC_ERROR_OK = 0, /**< No error */
GNC_ERROR_ARG = -1, /**< Argument is not a valid number */
GNC_ERROR_OVERFLOW = -2, /**< Intermediate result overflow */
GNC_ERROR_DENOM_DIFF = -3, /**< Argument denoms differ in GNC_DENOM_FIXED operation */
GNC_ERROR_REMAINDER = -4 /**< Remainder part in GNC_RND_NEVER operation */
} GNCNumericErrorCode;
#define GNC_DENOM_AUTO 0
@ -116,7 +116,7 @@ gnc_numeric double_to_gnc_numeric(double in, gint64 denom,
const gchar *string_to_gnc_numeric(const gchar* str, gnc_numeric *n);
/** make a special error-signalling gnc_numeric */
gnc_numeric gnc_numeric_error(int error_code);
gnc_numeric gnc_numeric_error(GNCNumericErrorCode error_code);
/*@}*/
/** @name Value accessors */
@ -137,9 +137,10 @@ gchar *gnc_numeric_to_string(gnc_numeric n);
/** @name Tests */
/*@{*/
/** Check for error signal in value. Returns GNC_ERROR_OK (==0) if
* there is no error, or any error code if there is one
* (e.g. GNC_ERROR_OVERFLOW) */
int gnc_numeric_check(gnc_numeric a);
* the number appears to be valid, otherwise it returns the
* type of error.
*/
GNCNumericErrorCode gnc_numeric_check(gnc_numeric a);
/** Returns 1 if the given gnc_numeric is 0 (zeros), else returns 0. */
int gnc_numeric_zero_p(gnc_numeric a);