Cosmetic patch for fn-financial.c



This removes some inconsistencies and improves code clarity (there's
room for plenty more of these I think).

Neil.

2002-01-18  Neil Booth  <neil daikokuya demon co uk>

        * functions/fn-financial.c (is_valid_basis, is_valid_freq): New.
        (func_coup_cd, gnumeric_accrint, gnumeric_accrintm,
        gnumeric_intrate, gnumeric_received, gnumeric_pricedisc,
        gnumeric_pricemat, gnumeric_disc, gnumeric_price, gnumeric_yield,
        gnumeric_oddfprice, gnumeric_coupdaybs, gnumeric_coupdays,
        gnumeric_coupdaysnc, gnumeric_coupnum): Use them.

Index: fn-financial.c
===================================================================
RCS file: /cvs/gnome/gnumeric/src/functions/fn-financial.c,v
retrieving revision 1.116
diff -u -p -r1.116 fn-financial.c
--- fn-financial.c      2002/01/16 14:01:36     1.116
+++ fn-financial.c      2002/01/18 21:11:41
@@ -24,6 +24,8 @@
 #include <limits.h>
 #include <string.h>
 
+#define is_valid_basis(B) (B >= 0 && B <= 5)
+#define is_valid_freq(F) (F == 1 || F == 2 || F == 4)
 
 /***************************************************************************
  *
@@ -324,20 +326,21 @@ func_coup_cd (FunctionEvalInfo *ei, Valu
        }
 
        if (xl) {
-               if (basis < 0 || basis > 4 || (freq != 1 && freq != 2 && freq != 4)
+               if (!is_valid_basis (basis)
+                   || !is_valid_freq (freq)
                    || g_date_compare (settlement, maturity) >= 0) {
                        result = value_new_error (ei->pos, gnumeric_err_NUM);
                        goto out;
                }
        }
-       else
-               if (basis < 0 || basis > 5 || (freq == 0) || (12 % freq != 0)
-                   || g_date_compare (settlement, maturity) > (next ? -1 : 0)) {
-                       result = value_new_error (ei->pos, gnumeric_err_NUM);
-                       goto out;
-               }
+       else if (!is_valid_basis (basis)
+                || !is_valid_freq (freq)
+                || g_date_compare (settlement, maturity) > (next ? -1 : 0)) {
+               result = value_new_error (ei->pos, gnumeric_err_NUM);
+               goto out;
+       }
 
-        date = xl ? coup_cd_xl (settlement, maturity, freq, next) : 
+        date = xl ? coup_cd_xl (settlement, maturity, freq, TRUE, next) : 
                coup_cd (settlement, maturity, freq, eom, next);
        result = value_new_int (datetime_g_to_serial (date));
        g_date_free (date);
@@ -419,7 +422,8 @@ gnumeric_accrint (FunctionEvalInfo *ei, 
                goto out;
        }
 
-        if (basis < 0 || basis > 4 || (freq != 1 && freq != 2 && freq != 4)
+        if (!is_valid_basis (basis)
+           || !is_valid_freq (freq)
            || g_date_compare (settlement, first_interest) > 0
            || g_date_compare (first_interest, maturity) < 0) {
                result = value_new_error (ei->pos, gnumeric_err_NUM);
@@ -429,8 +433,7 @@ gnumeric_accrint (FunctionEvalInfo *ei, 
        a = days_monthly_basis (argv[0], argv[2], basis);
        d = annual_year_basis (argv[0], basis);
 
-       if (a < 0 || d <= 0 || par <= 0 || rate <= 0 || basis < 0 || basis > 4
-           || freq == 0) {
+       if (a < 0 || d <= 0 || par <= 0 || rate <= 0) {
                result = value_new_error (ei->pos, gnumeric_err_NUM);
                goto out;
        }
@@ -494,7 +497,8 @@ gnumeric_accrintm (FunctionEvalInfo *ei,
        a = days_monthly_basis (argv[0], argv[1], basis);
        d = annual_year_basis (argv[0], basis);
 
-       if (a < 0 || d <= 0 || par <= 0 || rate <= 0 || basis < 0 || basis > 4)
+       if (a < 0 || d <= 0 || par <= 0 || rate <= 0
+           || !is_valid_basis (basis))
                 return value_new_error (ei->pos, gnumeric_err_NUM);
 
        return value_new_float (par * rate * a/d);
@@ -554,7 +558,7 @@ gnumeric_intrate (FunctionEvalInfo *ei, 
        a = days_monthly_basis (argv[0], argv[1], basis);
        d = annual_year_basis (argv[0], basis);
 
-       if (basis < 0 || basis > 4 || a <= 0 || d <= 0 || investment == 0)
+       if (!is_valid_basis (basis) || a <= 0 || d <= 0 || investment == 0)
                 return value_new_error (ei->pos, gnumeric_err_NUM);
 
        return value_new_float ((redemption - investment) / investment *
@@ -606,7 +610,7 @@ gnumeric_received (FunctionEvalInfo *ei,
        a = days_monthly_basis (argv[0], argv[1], basis);
        d = annual_year_basis (argv[0], basis);
 
-       if (a <= 0 || d <= 0 || basis < 0 || basis > 4)
+       if (a <= 0 || d <= 0 || !is_valid_basis (basis))
                 return value_new_error (ei->pos, gnumeric_err_NUM);
 
        n = 1.0 - (discount * a/d);
@@ -662,7 +666,7 @@ gnumeric_pricedisc (FunctionEvalInfo *ei
        a = days_monthly_basis (argv[0], argv[1], basis);
        d = annual_year_basis (argv[0], basis);
 
-       if (a <= 0 || d <= 0 || basis < 0 || basis > 4)
+       if (a <= 0 || d <= 0 || !is_valid_basis (basis))
                 return value_new_error (ei->pos, gnumeric_err_NUM);
 
        return value_new_float (redemption - discount * redemption * a/d);
@@ -715,8 +719,8 @@ gnumeric_pricemat (FunctionEvalInfo *ei,
        a   = days_monthly_basis (argv[2], argv[0], basis);
        b   = annual_year_basis (argv[0], basis);
 
-       if (a <= 0 || b <= 0 || dsm <= 0 || dim <= 0 || basis < 0 ||
-           basis > 4)
+       if (a <= 0 || b <= 0 || dsm <= 0 || dim <= 0
+           || !is_valid_basis (basis))
                 return value_new_error (ei->pos, gnumeric_err_NUM);
 
        n = 1 + (dsm/b * yield);
@@ -771,7 +775,7 @@ gnumeric_disc (FunctionEvalInfo *ei, Val
        b = annual_year_basis (argv[0], basis);
        dsm = days_monthly_basis (argv[0], argv[1], basis);
 
-       if (dsm <= 0 || b <= 0 || dsm <= 0 || basis < 0 || basis > 4
+       if (dsm <= 0 || b <= 0 || dsm <= 0 || !is_valid_basis (basis)
            || redemption == 0)
                 return value_new_error (ei->pos, gnumeric_err_NUM);
 
@@ -2318,7 +2322,8 @@ gnumeric_price (FunctionEvalInfo *ei, Va
                goto out;
        }
 
-        if (basis < 0 || basis > 4 || (freq != 1 && freq != 2 && freq != 4)
+        if (!is_valid_basis (basis)
+           || !is_valid_freq (freq)
             || g_date_compare (settlement, maturity) > 0) {
                result = value_new_error (ei->pos, gnumeric_err_NUM);
                goto out;
@@ -2406,7 +2411,8 @@ gnumeric_yield (FunctionEvalInfo *ei, Va
                goto out;
        }
 
-        if (basis < 0 || basis > 4 || (freq != 1 && freq != 2 && freq != 4)
+        if (!is_valid_basis (basis)
+           || !is_valid_freq (freq)
             || g_date_compare (settlement, maturity) > 0) {
                result = value_new_error (ei->pos, gnumeric_err_NUM);
                goto out;
@@ -2575,7 +2581,8 @@ gnumeric_oddfprice (FunctionEvalInfo *ei
                goto out;
        }
 
-        if (basis < 0 || basis > 4 || (freq != 1 && freq != 2 && freq != 4)
+        if (!is_valid_basis (basis)
+           || !is_valid_freq (freq)
             || g_date_compare (issue, settlement) > 0
            || g_date_compare (settlement, first_coupon) > 0
            || g_date_compare (first_coupon, maturity) > 0) {
@@ -2858,18 +2865,19 @@ gnumeric_coupdaybs (FunctionEvalInfo *ei
        }
 
        if (xl) {
-               if (basis < 0 || basis > 4 || (freq != 1 && freq != 2 && freq != 4)
+               if (!is_valid_basis (basis)
+                   || !is_valid_freq (freq)
                    || g_date_compare (settlement, maturity) >= 0) {
                        result = value_new_error (ei->pos, gnumeric_err_NUM);
                        goto out;
                }
        }
-       else
-               if (basis < 0 || basis > 5 || (freq == 0) || (12 % freq != 0)
-                   || g_date_compare (settlement, maturity) > 0) {
-                       result = value_new_error (ei->pos, gnumeric_err_NUM);
-                       goto out;
-               }
+       else if (!is_valid_basis (basis)
+                || !is_valid_freq (freq)
+                || g_date_compare (settlement, maturity) > 0) {
+               result = value_new_error (ei->pos, gnumeric_err_NUM);
+               goto out;
+       }
 
        result = value_new_int (coupdaybs (settlement, maturity, freq, basis, eom, xl));
 
@@ -2942,18 +2950,19 @@ gnumeric_coupdays (FunctionEvalInfo *ei,
        }
 
        if (xl) {
-               if (basis < 0 || basis > 4 || (freq != 1 && freq != 2 && freq != 4)
+               if (!is_valid_basis (basis)
+                   || !is_valid_freq (freq)
                    || g_date_compare (settlement, maturity) >= 0) {
                        result = value_new_error (ei->pos, gnumeric_err_NUM);
                        goto out;
                }
        }
-       else
-               if (basis < 0 || basis > 5 || (freq == 0) || (12 % freq != 0)
-                   || g_date_compare (settlement, maturity) > 0) {
-                       result = value_new_error (ei->pos, gnumeric_err_NUM);
-                       goto out;
-               }
+       else if (!is_valid_basis (basis)
+                || !is_valid_freq (freq)
+                || g_date_compare (settlement, maturity) > 0) {
+               result = value_new_error (ei->pos, gnumeric_err_NUM);
+               goto out;
+       }
 
         result = value_new_float (coupdays (settlement, maturity, freq, basis, eom, xl));
 
@@ -3028,18 +3037,19 @@ gnumeric_coupdaysnc (FunctionEvalInfo *e
        }
 
        if (xl) {
-               if (basis < 0 || basis > 4 || (freq != 1 && freq != 2 && freq != 4)
+               if (!is_valid_basis (basis)
+                   || !is_valid_freq (freq)
                    || g_date_compare (settlement, maturity) >= 0) {
                        result = value_new_error (ei->pos, gnumeric_err_NUM);
                        goto out;
                }
        }
-       else
-               if (basis < 0 || basis > 5 || (freq == 0) || (12 % freq != 0)
-                   || g_date_compare (settlement, maturity) > 0) {
-                       result = value_new_error (ei->pos, gnumeric_err_NUM);
-                       goto out;
-               }
+       else if (!is_valid_basis (basis)
+                || !is_valid_freq (freq)
+                || g_date_compare (settlement, maturity) > 0) {
+               result = value_new_error (ei->pos, gnumeric_err_NUM);
+               goto out;
+       }
 
        result = value_new_int (coupdaysnc (settlement, maturity, freq, basis, eom, xl));
 
@@ -3191,7 +3201,8 @@ gnumeric_coupnum (FunctionEvalInfo *ei, 
                goto out;
        }
 
-        if (basis < 0 || basis > 4 || (freq != 1 && freq != 2 && freq != 4)
+       if (!is_valid_basis (basis)
+           || !is_valid_freq (freq)
            || g_date_compare (settlement, maturity) > 0) {
                result = value_new_error (ei->pos, gnumeric_err_NUM);
                goto out;



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]