Small cleanup in fn-financial.c



This is a trivial patch to merge common code.  Also, Excel gives #NUM!
if settlement == maturity, so we should do that too.  (IMO, that's
stupid for pcd, but makes sense for ncd.  I think we should allow it
for pcd, but I wanted to get Excel compatibility before talking about
mini-extensions).

Neil.

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

        * fn-financial.c (gnumeric_coupncd): Rename gnumeric_coupcd,
        and take a boolean indicating whether the previous or next
        coupon date is required.  Give #NUM! if settlement == maturity.
        (gnumeric_coupncd, gnumeric_coupcd): Update to use the
        common function.

Index: functions/fn-financial.c
===================================================================
RCS file: /cvs/gnome/gnumeric/src/functions/fn-financial.c,v
retrieving revision 1.114
diff -u -p -r1.114 fn-financial.c
--- functions/fn-financial.c    2002/01/14 06:13:46     1.114
+++ functions/fn-financial.c    2002/01/14 20:48:38
@@ -2972,7 +2972,7 @@ static char *help_coupncd = {
 };
 
 static Value *
-gnumeric_coupncd (FunctionEvalInfo *ei, Value **argv)
+gnumeric_coupcd (FunctionEvalInfo *ei, Value **argv, gboolean next)
 {
         GDate   *settlement;
         GDate   *maturity;
@@ -2993,12 +2993,12 @@ gnumeric_coupncd (FunctionEvalInfo *ei, 
        }
 
         if (basis < 0 || basis > 4 || (freq != 1 && freq != 2 && freq != 4)
-           || g_date_compare (settlement, maturity) > 0) {
+           || g_date_compare (settlement, maturity) >= 0) {
                result = value_new_error (ei->pos, gnumeric_err_NUM);
                goto out;
        }
 
-        date = coup_cd (settlement, maturity, freq, oem,TRUE);
+        date = coup_cd (settlement, maturity, freq, oem, next);
        result = value_new_int (datetime_g_to_serial (date));
        g_date_free (date);
 
@@ -3009,6 +3009,12 @@ gnumeric_coupncd (FunctionEvalInfo *ei, 
        return result;
 }
 
+static Value *
+gnumeric_coupncd (FunctionEvalInfo *ei, Value **argv)
+{
+       return gnumeric_coupcd (ei, argv, TRUE);
+}
+
 /***************************************************************************/
 
 static char *help_couppcd = {
@@ -3041,39 +3047,7 @@ static char *help_couppcd = {
 static Value *
 gnumeric_couppcd (FunctionEvalInfo *ei, Value **argv)
 {
-        GDate   *settlement;
-        GDate   *maturity;
-        GDate   *date;
-        int     freq, basis;
-       gboolean oem, err = FALSE;
-       Value   *result;
-
-        settlement = datetime_value_to_g (argv[0]);
-        maturity   = datetime_value_to_g (argv[1]);
-        freq       = value_get_as_int (argv[2]);
-       basis      = argv[3] ? value_get_as_int (argv[3]) : 0;
-       oem        = argv[4] ? value_get_as_bool (argv[4], &err) : TRUE;
-
-       if (!maturity || !settlement || err) {
-               result = value_new_error (ei->pos, gnumeric_err_VALUE);
-               goto out;
-       }
-
-        if (basis < 0 || basis > 4 || (freq != 1 && freq != 2 && freq != 4)
-           || g_date_compare (settlement, maturity) > 0) {
-               result = value_new_error (ei->pos, gnumeric_err_NUM);
-               goto out;
-       }
-
-        date = coup_cd (settlement, maturity, freq, oem, FALSE);
-       result = value_new_int (datetime_g_to_serial (date));
-       g_date_free (date);
-
- out:
-       g_date_free (settlement);
-       g_date_free (maturity);
-
-       return result;
+       return gnumeric_coupcd (ei, argv, FALSE);
 }
 
 /***************************************************************************/




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