fix divide by zero problem.

git-svn-id: svn+ssh://svn.gnucash.org/repo/gnucash/trunk@2445 57a11ea4-9604-0410-9ed3-97b8803252fd
This commit is contained in:
Linas Vepstas 2000-06-08 21:22:11 +00:00
parent ec38955ce4
commit dee29974be
2 changed files with 30 additions and 15 deletions

View File

@ -551,6 +551,20 @@ xaccTransLookup (const GUID *guid)
/********************************************************************\ /********************************************************************\
\********************************************************************/ \********************************************************************/
/* compute a=b/c unless c is zero ... */
#define DEVIDE(a,b,c) { \
if (DEQEPS (0.0, (c), 1.0e-15)) { \
if (DEQEPS (0.0, (b), 1.0e-6)) { \
(a) = 0.0; \
} else { \
PERR ("zero share price but non-zero value\n"); \
(a) = (b)/(c); \
} \
} else { \
(a) = (b)/(c); \
} \
}
void void
xaccSplitSetBaseValue (Split *s, double value, const char * base_currency) xaccSplitSetBaseValue (Split *s, double value, const char * base_currency)
{ {
@ -563,28 +577,29 @@ xaccSplitSetBaseValue (Split *s, double value, const char * base_currency)
*/ */
if (!(s->acc)) { if (!(s->acc)) {
if (force_double_entry) { if (force_double_entry) {
PERR ("split must have a parent\n");
assert (s->acc); assert (s->acc);
} else { } else {
s -> damount = (value / (s->share_price)); DEVIDE (s -> damount, value, s->share_price);
return; return;
} }
} }
/* be more precise -- the value depends on the currency /* The value of a split depends on the currency we express the
* we want it expressed in. * value in. This may or may not require a divide.
*/ */
if (!safe_strcmp(s->acc->currency, base_currency)) { if (!safe_strcmp(s->acc->currency, base_currency)) {
s -> damount = (value / (s->share_price)); DEVIDE (s -> damount, value, s->share_price);
} else } else
if (!safe_strcmp(s->acc->security, base_currency)) { if (!safe_strcmp(s->acc->security, base_currency)) {
s -> damount = value; s -> damount = value;
} else } else
if ((0x0==base_currency) && (0 == force_double_entry)) { if ((0x0==base_currency) && (0 == force_double_entry)) {
s -> damount = (value / (s->share_price)); DEVIDE (s -> damount, value, s->share_price);
} else } else
{ {
PERR (" inappropriate base currency %s " PERR ("inappropriate base currency %s "
" given split currency=%s and security=%s\n", "given split currency=%s and security=%s\n",
base_currency, s->acc->currency, s->acc->security); base_currency, s->acc->currency, s->acc->security);
return; return;
} }
@ -624,8 +639,8 @@ xaccSplitGetBaseValue (Split *s, const char * base_currency)
value = s->damount * s->share_price; value = s->damount * s->share_price;
} else } else
{ {
PERR (" inappropriate base currency %s " PERR ("inappropriate base currency %s "
" given split currency=%s and security=%s\n", "given split currency=%s and security=%s\n",
base_currency, s->acc->currency, s->acc->security); base_currency, s->acc->currency, s->acc->security);
return 0.0; return 0.0;
} }
@ -671,8 +686,7 @@ ComputeValue (Split **sarray, Split * skip_me, const char * base_currency)
if (!safe_strcmp(s->acc->security, base_currency)) { if (!safe_strcmp(s->acc->security, base_currency)) {
value += s->damount; value += s->damount;
} else { } else {
PERR ("Internal Error: " PERR ("inconsistent currencies\n");
" inconsistent currencies \n");
assert (0); assert (0);
} }
} }
@ -857,7 +871,7 @@ xaccSplitRebalance (Split *split)
base_currency = FindCommonCurrency (trans->splits, ra, rb); base_currency = FindCommonCurrency (trans->splits, ra, rb);
if (!base_currency) { if (!base_currency) {
PERR ("Internal Error: no common split currencies \n"); PERR ("no common split currencies\n");
s = trans->splits[0]; s = trans->splits[0];
while (s) { while (s) {
if (s->acc) { if (s->acc) {

View File

@ -124,7 +124,8 @@ size_t dcoresize();
#define isNum(x) (((x)-0x30) < 0) ? 0 : (((x)-0x30) > 9) ? 0 : 1 #define isNum(x) (((x)-0x30) < 0) ? 0 : (((x)-0x30) > 9) ? 0 : 1
#define EPS (1.0e-6) #define EPS (1.0e-6)
#define DEQ(x,y) (((((x)+EPS)>(y)) ? 1 : 0) && ((((x)-EPS)<(y)) ? 1 : 0)) #define DEQEPS(x,y,eps) (((((x)+(eps))>(y)) ? 1 : 0) && ((((x)-(eps))<(y)) ? 1 : 0))
#define DEQ(x,y) DEQEPS(x,y,EPS)
#define SAFE_STRCMP(da,db) { \ #define SAFE_STRCMP(da,db) { \