functions.c 111 KB
Newer Older
Jody Goldberg's avatar
Jody Goldberg committed
1
/* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 3 4
/*
 * fn-financial.c:  Built in financial functions and functions registration
 *
5
 * Authors:
Morten Welinder's avatar
Morten Welinder committed
6 7 8 9
 *   Jukka-Pekka Iivonen (jiivonen@hutcs.cs.hut.fi)
 *   Morten Welinder (terra@diku.dk)
 *   Vladimir Vuksan (vuksan@veus.hr)
 *   Andreas J. Guelzow (aguelzow@taliesin.ca)
10
 *
Jukka-Pekka Iivonen's avatar
Jukka-Pekka Iivonen committed
11 12 13 14 15 16 17 18 19 20 21 22 23
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24
 */
25 26 27 28 29 30
#include <gnumeric-config.h>
#include <gnumeric.h>
#include <func.h>

#include <parse-util.h>
#include <cell.h>
Morten Welinder's avatar
Morten Welinder committed
31
#include <tools/goal-seek.h>
32 33 34
#include <collect.h>
#include <auto-format.h>
#include <datetime.h>
35
#include <value.h>
36
#include <str.h>
37

Jody Goldberg's avatar
Jody Goldberg committed
38 39 40
#include <math.h>
#include <limits.h>
#include <string.h>
41
#include <libgnome/gnome-i18n.h>
Jody Goldberg's avatar
Jody Goldberg committed
42

Morten Welinder's avatar
Morten Welinder committed
43 44 45
#include "plugin.h"
#include "plugin-util.h"
#include "module-plugin-defs.h"
46
#include "sc-fin.h"
Morten Welinder's avatar
Morten Welinder committed
47 48 49

GNUMERIC_MODULE_PLUGIN_INFO_DECL;

50 51
#define is_valid_basis(B) (B >= 0 && B <= 5)
#define is_valid_freq(F) (F == 1 || F == 2 || F == 4)
52

53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
/***************************************************************************
 *
 *
 * Below are some of the functions that are used quite often in
 * financial analysis.
 *
 * Present value interest factor
 *
 *	 PVIF = (1 + k) ^ n
 *
 * Future value interest factor
 *
 *       FVIF = 1 / PVIF
 *
 * Present value interest factor of annuities
 *
 *                1          1
 *	 PVIFA = --- - -----------
 *                k     k*(1+k)^n
 *
 * Future value interest factor of annuities
 *
 *                (1+k)^n - 1
 *       FVIFA = ----------------
 *	                k
 *
 *
 *
 *	 PV * PVIF(k%, nper) + PMT * ( 1 + rate * type ) *
 *	      FVIFA(k%, nper) + FV = 0
 *
84 85
 */

Jody Goldberg's avatar
Jody Goldberg committed
86 87
static gnum_float
calculate_pvif (gnum_float rate, gnum_float nper)
88
{
89
	return (powgnum (1 + rate, nper));
90 91
}

Morten Welinder's avatar
Morten Welinder committed
92
#if 0
Jody Goldberg's avatar
Jody Goldberg committed
93 94
static gnum_float
calculate_fvif (gnum_float rate, gnum_float nper)
95
{
96
	return (1.0 / calculate_pvif (rate,nper));
97 98
}

Jody Goldberg's avatar
Jody Goldberg committed
99 100
static gnum_float
calculate_pvifa (gnum_float rate, gnum_float nper)
101
{
102
	return ((1.0 / rate) - (1.0 / (rate * powgnum (1 + rate, nper))));
103
}
Morten Welinder's avatar
Morten Welinder committed
104
#endif
105

Jody Goldberg's avatar
Jody Goldberg committed
106 107
static gnum_float
calculate_fvifa (gnum_float rate, gnum_float nper)
108
{
109
	return ((powgnum (1 + rate, nper) - 1) / rate);
110 111
}

112

113 114 115 116 117 118 119
/*
 *
 * Principal for period x is calculated using this formula
 *
 * PR(x) = PR(0) * ( 1 + rate ) ^ x + PMT * ( ( 1 + rate ) ^ x - 1 ) / rate )
 *
 */
Jody Goldberg's avatar
Jody Goldberg committed
120 121 122
static gnum_float
calculate_principal (gnum_float starting_principal, gnum_float payment,
		     gnum_float rate, gnum_float period)
123
{
124 125
	return (starting_principal * powgnum (1.0 + rate, period) + payment *
		((powgnum (1 + rate, period) - 1) / rate));
126 127
}

Jody Goldberg's avatar
Jody Goldberg committed
128
static gnum_float
129 130
calculate_pmt (gnum_float rate, gnum_float nper, gnum_float pv, gnum_float fv,
	       int type)
131
{
Jody Goldberg's avatar
Jody Goldberg committed
132
	gnum_float pvif, fvifa;
133 134 135

	/* Calculate the PVIF and FVIFA */

136 137
	pvif = calculate_pvif (rate,nper);
	fvifa = calculate_fvifa (rate,nper);
138

Morten Welinder's avatar
Morten Welinder committed
139
        return ((-pv * pvif - fv ) / ((1.0 + rate * type) * fvifa));
140 141
}

142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
/***************************************************************************/

/*
 * Returns the number of days in the year for the given date accoring to
 * the day counting system specified by 'basis' argument.  Basis may have
 * one of the following values:
 *
 *	0  for US 30/360 (days in a month/days in a year)
 *	1  for actual days/actual days
 *	2  for actual days/360
 *	3  for actual days/365
 *	4  for European 30/360
 *
 * This function returns 360 for basis 0, 2, and 4, it returns value
 * 365 for basis 3, and value 365 or 366 for basis 1 accoring to the
 * year of the given date (366 is returned if the date is in a leap
 * year).
 */
160
static int
Morten Welinder's avatar
Morten Welinder committed
161
annual_year_basis (Value *value_date, int basis)
162 163 164 165 166 167 168 169
{
        GDate    *date;
        gboolean leap_year;

	switch (basis) {
	case 0:
	        return 360;
	case 1:
Morten Welinder's avatar
Morten Welinder committed
170
	        date = datetime_value_to_g (value_date);
171
		if (date != NULL) {
Jon Kåre Hellan's avatar
Jon Kåre Hellan committed
172 173
		        leap_year =
				g_date_is_leap_year (g_date_get_year (date));
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
			g_date_free (date);
		} else
		        return -1;
	        return leap_year ? 366 : 365;
	case 2:
	        return 360;
	case 3:
	        return 365;
	case 4:
	        return 360;
	default:
	        return -1;
	}
}

189 190 191 192 193 194 195 196 197 198 199
/* Returns the number of days between issue date and maturity date
 * accoring to the day counting system specified by the 'basis'
 * argument.  Basis may have one of the following values:
 *
 *	0  for US 30/360 (days in a month/days in a year)
 *	1  for actual days/actual days
 *	2  for actual days/360
 *	3  for actual days/365
 *	4  for European 30/360
 *
 */
200
static int
Morten Welinder's avatar
Morten Welinder committed
201
days_monthly_basis (Value *issue_date, Value *maturity_date, int basis)
202
{
203 204 205 206 207 208
        GDate    *date_i, *date_m;
	int      issue_day, issue_month, issue_year;
	int      maturity_day, maturity_month, maturity_year;
        int      months, days, years;
	gboolean leap_year;
	int      maturity, issue;
209

Morten Welinder's avatar
Morten Welinder committed
210 211
	date_i = datetime_value_to_g (issue_date);
	date_m = datetime_value_to_g (maturity_date);
212
	if (date_i != NULL && date_m != NULL) {
Jon Kåre Hellan's avatar
Jon Kåre Hellan committed
213 214 215 216 217 218
	        issue_year = g_date_get_year (date_i);
	        issue_month = g_date_get_month (date_i);
	        issue_day = g_date_get_day (date_i);
	        maturity_year = g_date_get_year (date_m);
	        maturity_month = g_date_get_month (date_m);
	        maturity_day = g_date_get_day (date_m);
219 220 221 222 223

	        years = maturity_year - issue_year;
	        months = maturity_month - issue_month;
	        days = maturity_day - issue_day;

224
		months = years * 12 + months;
225 226
		leap_year = g_date_is_leap_year (issue_year);

227 228
		datetime_g_free (date_i);
		datetime_g_free (date_m);
229
	} else {
230 231
	        datetime_g_free (date_i);
	        datetime_g_free (date_m);
232 233 234 235 236
	        return -1;
	}

	switch (basis) {
	case 0:
237
	        if (issue_month == 2 && maturity_month != 2 &&
Morten Welinder's avatar
Morten Welinder committed
238
		    issue_year == maturity_year) {
239 240 241 242 243
			if (leap_year)
				return months * 30 + days - 1;
			else
				return months * 30 + days - 2;
		}
244 245 246 247
	        return months * 30 + days;
	case 1:
	case 2:
	case 3:
Morten Welinder's avatar
Morten Welinder committed
248 249
	        issue = datetime_value_to_serial (issue_date);
	        maturity = datetime_value_to_serial (maturity_date);
250 251 252 253 254 255 256 257
	        return maturity - issue;
	case 4:
	        return months * 30 + days;
	default:
	        return -1;
	}
}

258 259 260 261 262
/***************************************************************************/

/* Returns the number of coupons to be paid between the settlement
 * and maturity dates.
 */
263
gnum_float
264 265
coupnum (GDate *settlement, GDate *maturity, int freq, basis_t basis,
	 gboolean eom)
266
{
267 268 269 270
        int        months;
	GDate      this_coupondate;

	g_date_clear (&this_coupondate, 1);
Jon Kåre Hellan's avatar
Jon Kåre Hellan committed
271
	g_date_set_julian (&this_coupondate, g_date_get_julian(maturity));
272

Jon Kåre Hellan's avatar
Jon Kåre Hellan committed
273 274 275
	months = g_date_get_month (maturity) - g_date_get_month (settlement) +
		12 *
		(g_date_get_year (maturity) - g_date_get_year  (settlement));
276 277 278 279 280 281

	g_date_subtract_months (&this_coupondate, months);

	if (eom &&  g_date_is_last_of_month (maturity))
		while (!g_date_is_last_of_month (&this_coupondate))
				g_date_add_days (&this_coupondate, 1);
Jody Goldberg's avatar
Jody Goldberg committed
282

Jon Kåre Hellan's avatar
Jon Kåre Hellan committed
283
	if (g_date_get_day (settlement) >= g_date_get_day (&this_coupondate))
284 285 286
	        months--;

	return (1 + months / (12 / freq));
287 288
}

Jody Goldberg's avatar
Jody Goldberg committed
289
static gnum_float
290 291
couppcd (GDate *settlement, GDate *maturity, int freq,  basis_t basis,
	 gboolean eom)
292 293 294
{
	GDate *date;
	int   serial_date;
Jody Goldberg's avatar
Jody Goldberg committed
295

296 297 298 299 300 301 302
	date = coup_cd (settlement, maturity, freq, eom, FALSE);
	serial_date = datetime_g_to_serial (date);
	g_date_free (date);

	return serial_date;
}

Jody Goldberg's avatar
Jody Goldberg committed
303
static gnum_float
304 305
coupncd (GDate *settlement, GDate *maturity, int freq,  basis_t basis,
	 gboolean eom)
306 307 308
{
	GDate *date;
	int   serial_date;
Jody Goldberg's avatar
Jody Goldberg committed
309

310 311 312 313 314
	date = coup_cd (settlement, maturity, freq, eom, TRUE);
	serial_date = datetime_g_to_serial (date);
	g_date_free (date);

	return serial_date;
315

316 317
}

Jukka Pekka's avatar
Fixed.  
Jukka Pekka committed
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
static gnum_float
price (GDate *settlement, GDate *maturity, gnum_float rate, gnum_float yield,
       gnum_float redemption, int freq, basis_t basis)
{
	gnum_float a, d, e, sum, den, base, exponent, first_term, last_term;
	gint       k, n;

	a = coupdaybs (settlement, maturity, freq, basis, TRUE);
	d = coupdaysnc (settlement, maturity, freq, basis, TRUE);
	e = coupdays (settlement, maturity, freq, basis, TRUE);
	n = coupnum (settlement, maturity, freq, basis, TRUE);

	sum = 0.0;
	den = 100.0 * rate / freq;
	base = 1.0 + yield / freq;
	exponent = d / e;
	for (k = 0; k < n; k++)
	        sum += den / powgnum (base, exponent + k);

	first_term = redemption / powgnum (base, (n - 1.0 + d / e));
	last_term = a / e * den;

	return (first_term + sum - last_term);
}

343 344
/************************************************************************
 *
Jody Goldberg's avatar
Jody Goldberg committed
345
 * Reading and verifying the arguments for the various COUP____
346 347 348 349
 * functions. Calls the passed coup_fn to do the real work
 *
 ***********************************************************************/

350
static Value *
Jody Goldberg's avatar
Jody Goldberg committed
351 352
func_coup (FunctionEvalInfo *ei, Value **argv,
	   gnum_float (coup_fn)(GDate *settlement, GDate *maturity,
353
				int freq, basis_t basis, gboolean eom))
354 355 356
{
        GDate   *settlement;
        GDate   *maturity;
357 358 359
        int     freq;
	basis_t basis;
	gboolean eom, err = FALSE;
360 361 362 363 364
	Value   *result;

        settlement = datetime_value_to_g (argv[0]);
        maturity   = datetime_value_to_g (argv[1]);
        freq       = value_get_as_int (argv[2]);
365
	basis      = argv[3] ? value_get_as_int (argv[3]) : BASIS_MSRB_30_360;
366
	eom        = argv[4] ? value_get_as_bool (argv[4], &err) : TRUE;
367 368 369 370 371 372

	if (!maturity || !settlement || err) {
		result = value_new_error (ei->pos, gnumeric_err_VALUE);
		goto out;
	}

373 374 375 376
	if (basis < 0 || basis > BASIS_LAST || (freq == 0) || (12 % freq != 0)
	    || g_date_compare (settlement, maturity) >= 0) {
		result = value_new_error (ei->pos, gnumeric_err_NUM);
		goto out;
377 378
	}

379 380
	result = value_new_float (coup_fn (settlement, maturity, freq, basis,
					   eom));
381 382

 out:
383 384
	datetime_g_free (settlement);
	datetime_g_free (maturity);
385 386 387

	return result;
}
388 389


390 391 392 393 394
/***************************************************************************
 *
 * Financial function implementations
 *
 */
395

Morten Welinder's avatar
Morten Welinder committed
396
static const char *help_accrint = {
397 398 399 400
	N_("@FUNCTION=ACCRINT\n"
	   "@SYNTAX=ACCRINT(issue,first_interest,settlement,rate,par,"
	   "frequency[,basis])\n"
	   "@DESCRIPTION="
401 402 403 404 405 406 407 408 409 410
	   "ACCRINT calculates the accrued interest for a security that "
	   "pays periodic interest.  "
	   "@issue is the issue date of the security.  @first_interest is "
	   "the first interest date of the security.  @settlement is the "
	   "settlement date of the sequrity.  The settlement date is always "
	   "after the issue date (the date when the security is bought). "
	   "@rate is the annual rate of the security and @par is the par "
	   "value of the security. "
	   "@frequency is the number of coupon payments per year. "
	   "Allowed frequencies are: 1 = annual, 2 = semi, 4 = quarterly. "
411
	   "@basis is the type of day counting system you want to use:\n"
412 413 414 415 416 417 418
	   "\n"
	   "0  US 30/360\n"
	   "1  actual days/actual days\n"
	   "2  actual days/360\n"
	   "3  actual days/365\n"
	   "4  European 30/360\n"
	   "\n"
419 420 421 422 423
	   "If @issue date, @first_interest date, or @settlement date is not "
	   "valid, ACCRINT returns #NUM! error. "
	   "The dates must be @issue < @first_interest < @settlement, or "
	   "ACCRINT returns #NUM! error. "
	   "If @rate <= 0 or @par <= 0 , ACCRINT returns #NUM! error. "
424
	   "If @basis is omitted, US 30/360 is applied. "
Valek Frob's avatar
Valek Frob committed
425
	   "If @basis < 0 or @basis > 4, ACCRINT returns #NUM! error. "
426
	   "If @issue date is after @settlement date or they are the same, "
Valek Frob's avatar
Valek Frob committed
427
	   "ACCRINT returns #NUM! error. "
428
	   "\n"
429 430
	   "@EXAMPLES=\n"
	   "\n"
431 432 433 434 435 436
	   "@SEEALSO=ACCRINTM")
};

static Value *
gnumeric_accrint (FunctionEvalInfo *ei, Value **argv)
{
437 438 439
        GDate      *settlement;
        GDate      *first_interest;
        GDate      *maturity;
Jody Goldberg's avatar
Jody Goldberg committed
440
	gnum_float rate, a, d, par, freq, coefficient, x;
441
	int        basis;
442
	Value      *result;
443

Morten Welinder's avatar
Fix...  
Morten Welinder committed
444
        maturity       = datetime_value_to_g (argv[0]);
445
	first_interest = datetime_value_to_g (argv[1]);
Morten Welinder's avatar
Fix...  
Morten Welinder committed
446
        settlement     = datetime_value_to_g (argv[2]);
447 448 449 450 451
	rate           = value_get_as_float (argv[3]);
	par            = value_get_as_float (argv[4]);
	freq           = value_get_as_float (argv[5]);
	basis          = argv[6] ? value_get_as_int (argv[6]) : 0;

Morten Welinder's avatar
Fix...  
Morten Welinder committed
452 453 454 455 456
	if (!maturity || !first_interest || !settlement) {
		result = value_new_error (ei->pos, gnumeric_err_VALUE);
		goto out;
	}

457 458
        if (!is_valid_basis (basis)
	    || !is_valid_freq (freq)
459
	    || g_date_compare (settlement, first_interest) > 0
Morten Welinder's avatar
Fix...  
Morten Welinder committed
460
	    || g_date_compare (first_interest, maturity) < 0) {
461 462 463
		result = value_new_error (ei->pos, gnumeric_err_NUM);
		goto out;
	}
464 465 466 467

	a = days_monthly_basis (argv[0], argv[2], basis);
	d = annual_year_basis (argv[0], basis);

468
	if (a < 0 || d <= 0 || par <= 0 || rate <= 0) {
469 470 471
		result = value_new_error (ei->pos, gnumeric_err_NUM);
		goto out;
	}
472 473 474 475

	coefficient = par * rate / freq;
	x = a / d;

476 477 478
	result = value_new_float (coefficient * freq * x);

 out:
479 480 481
	datetime_g_free (settlement);
	datetime_g_free (first_interest);
	datetime_g_free (maturity);
482 483

	return result;
484 485
}

486 487
/***************************************************************************/

Morten Welinder's avatar
Morten Welinder committed
488
static const char *help_accrintm = {
489 490 491 492
	N_("@FUNCTION=ACCRINTM\n"
	   "@SYNTAX=ACCRINTM(issue,maturity,rate[,par,basis])\n"
	   "@DESCRIPTION="
	   "ACCRINTM calculates and returns the accrued interest for a "
493 494 495
	   "security from @issue to @maturity date.  "
	   "@issue is the issue date of the security.  @maturity is "
	   "the maturity date of the security.  @rate is the annual "
496
	   "rate of the security and @par is the par value of the security. "
Jukka-Pekka Iivonen's avatar
Jukka-Pekka Iivonen committed
497 498
	   "If you omit @par, ACCRINTM applies $1,000 instead.  "
	   "@basis is the type of day counting system you want to use:\n"
499 500 501 502 503 504 505
	   "\n"
	   "0  US 30/360\n"
	   "1  actual days/actual days\n"
	   "2  actual days/360\n"
	   "3  actual days/365\n"
	   "4  European 30/360\n"
	   "\n"
506 507
	   "If @issue date or @maturity date is not valid, ACCRINTM returns "
	   "#NUM! error. "
508
	   "If @rate <= 0 or @par <= 0, ACCRINTM returns #NUM! error. "
509
	   "If @basis is omitted, US 30/360 is applied. "
Valek Frob's avatar
Valek Frob committed
510
	   "If @basis < 0 or @basis > 4, ACCRINTM returns #NUM! error. "
511
	   "If @issue date is after @maturity date or they are the same, "
Valek Frob's avatar
Valek Frob committed
512
	   "ACCRINTM returns #NUM! error. "
513
	   "\n"
514 515
	   "@EXAMPLES=\n"
	   "\n"
516
	   "@SEEALSO=ACCRINT")
517 518 519 520 521
};

static Value *
gnumeric_accrintm (FunctionEvalInfo *ei, Value **argv)
{
Jody Goldberg's avatar
Jody Goldberg committed
522
	gnum_float rate, a, d, par;
Morten Welinder's avatar
Morten Welinder committed
523
	int basis;
524

525 526
	rate  = value_get_as_float (argv[2]);
	par   = argv[3] ? value_get_as_float (argv[3]) : 1000;
Morten Welinder's avatar
Morten Welinder committed
527
	basis = argv[4] ? value_get_as_int (argv[4]) : 0;
528 529 530 531

	a = days_monthly_basis (argv[0], argv[1], basis);
	d = annual_year_basis (argv[0], basis);

532 533
	if (a < 0 || d <= 0 || par <= 0 || rate <= 0
	    || !is_valid_basis (basis))
534
                return value_new_error (ei->pos, gnumeric_err_NUM);
535 536 537 538

	return value_new_float (par * rate * a/d);
}

539 540
/***************************************************************************/

Morten Welinder's avatar
Morten Welinder committed
541
static const char *help_intrate = {
542 543 544 545
	N_("@FUNCTION=INTRATE\n"
	   "@SYNTAX=INTRATE(settlement,maturity,investment,redemption"
	   "[,basis])\n"
	   "@DESCRIPTION="
546
	   "INTRATE calculates and returns the interest rate of a fully "
547 548
	   "vested security.  @settlement is the settlement date of the "
	   "security.  @maturity is the maturity date of the security. "
549 550
	   "@investment is the prize of the security paid at @settlement "
	   "date and @redemption is the amount to be received at @maturity "
Jukka-Pekka Iivonen's avatar
Jukka-Pekka Iivonen committed
551 552
	   "date.  "
	   "@basis is the type of day counting system you want to use:\n"
553 554 555 556 557 558 559
	   "\n"
	   "0  US 30/360\n"
	   "1  actual days/actual days\n"
	   "2  actual days/360\n"
	   "3  actual days/365\n"
	   "4  European 30/360\n"
	   "\n"
560 561
	   "If @settlement date or @maturity date is not valid, INTRATE "
	   "returns #NUM! error. "
562
	   "If @basis is omitted, US 30/360 is applied. "
Valek Frob's avatar
Valek Frob committed
563
	   "If @basis < 0 or @basis > 4, INTRATE returns #NUM! error. "
564
	   "If @settlement date is after @maturity date or they are the same, "
Valek Frob's avatar
Valek Frob committed
565
	   "INTRATE returns #NUM! error. "
566
	   "\n"
567 568
	   "@EXAMPLES=\n"
	   "\n"
569 570 571 572 573 574 575 576 577
	   "If you had a bond with a settlement date of April 15, 2000, "
	   "maturity date September 30, 2000, investment of $100,000, "
	   "redemption value $103,525, using the actual/actual basis, the "
	   "bond discount rate is:"
	   "\n"
	   "=INTRATE(36631, 36799, 100000, 103525, 1) which equals 0.0648 "
	   "or 6.48%"
	   "\n"
	   "@SEEALSO=RECEIVED, DATE")
578 579 580 581 582
};

static Value *
gnumeric_intrate (FunctionEvalInfo *ei, Value **argv)
{
Jody Goldberg's avatar
Jody Goldberg committed
583
	gnum_float investment, redemption, a, d;
Morten Welinder's avatar
Morten Welinder committed
584
	int basis;
585 586 587

	investment = value_get_as_float (argv[2]);
	redemption = value_get_as_float (argv[3]);
588
	basis      = argv[4] ? value_get_as_int (argv[4]) : 0;
589 590 591 592

	a = days_monthly_basis (argv[0], argv[1], basis);
	d = annual_year_basis (argv[0], basis);

593
	if (!is_valid_basis (basis) || a <= 0 || d <= 0 || investment == 0)
594
                return value_new_error (ei->pos, gnumeric_err_NUM);
595 596 597 598 599

	return value_new_float ((redemption - investment) / investment *
				(d / a));
}

600 601
/***************************************************************************/

Morten Welinder's avatar
Morten Welinder committed
602
static const char *help_received = {
603
	N_("@FUNCTION=RECEIVED\n"
604
	   "@SYNTAX=RECEIVED(settlement,maturity,investment,rate[,basis])\n"
605 606
	   "@DESCRIPTION="
	   "RECEIVED calculates and returns the amount to be received at "
607 608 609 610
	   "@maturity date for a security bond.  @settlement is the "
	   "settlement date of the security.  @maturity is the maturity date "
	   "of the security.  The amount of investement is specified in "
	   "@investment.  @rate is the security's discount rate. "
611 612 613 614 615 616 617 618 619
	   "@basis is the type of day counting system you want to "
	   "use:\n"
	   "\n"
	   "0  US 30/360\n"
	   "1  actual days/actual days\n"
	   "2  actual days/360\n"
	   "3  actual days/365\n"
	   "4  European 30/360\n"
	   "\n"
620
	   "If @settlement date or @maturity date is not valid, RECEIVED "
Valek Frob's avatar
Valek Frob committed
621
	   "returns #NUM! error. "
622
	   "If @basis is omitted, US 30/360 is applied. "
Valek Frob's avatar
Valek Frob committed
623
	   "If @basis < 0 or @basis > 4, RECEIVED returns #NUM! error. "
624
	   "If @settlement date is after @maturity date or they are the same, "
Valek Frob's avatar
Valek Frob committed
625
	   "RECEIVED returns #NUM! error. "
626
	   "\n"
627 628
	   "@EXAMPLES=\n"
	   "\n"
629 630 631 632 633 634
	   "@SEEALSO=INTRATE")
};

static Value *
gnumeric_received (FunctionEvalInfo *ei, Value **argv)
{
Jody Goldberg's avatar
Jody Goldberg committed
635
	gnum_float investment, discount, a, d, n;
Morten Welinder's avatar
Morten Welinder committed
636
	int basis;
637 638

	investment = value_get_as_float (argv[2]);
639 640
	discount   = value_get_as_float (argv[3]);
	basis      = argv[4] ? value_get_as_int (argv[4]) : 0;
641 642 643 644

	a = days_monthly_basis (argv[0], argv[1], basis);
	d = annual_year_basis (argv[0], basis);

645
	if (a <= 0 || d <= 0 || !is_valid_basis (basis))
646
                return value_new_error (ei->pos, gnumeric_err_NUM);
647

648 649 650 651 652
	n = 1.0 - (discount * a/d);
	if (n == 0)
	        return value_new_error (ei->pos, gnumeric_err_NUM);

	return value_new_float (investment / n);
653 654
}

655 656
/***************************************************************************/

Morten Welinder's avatar
Morten Welinder committed
657
static const char *help_pricedisc = {
658
	N_("@FUNCTION=PRICEDISC\n"
659 660
	   "@SYNTAX=PRICEDISC(settlement,maturity,discount,redemption"
	   "[,basis])\n"
661 662 663
	   "@DESCRIPTION="
	   "PRICEDISC calculates and returns the price per $100 face value "
	   "of a security bond.  The security does not pay interest at "
664 665 666 667
	   "maturity.  @settlement is the settlement date of the security. "
	   "@maturity is the maturity date of the security.  @discount is "
	   "the rate for which the security is discounted.  @redemption is "
	   "the amount to be received on @maturity date.  "
Jukka-Pekka Iivonen's avatar
Jukka-Pekka Iivonen committed
668
	   "@basis is the type of day counting system you want to use:\n"
669 670 671 672 673 674 675
	   "\n"
	   "0  US 30/360\n"
	   "1  actual days/actual days\n"
	   "2  actual days/360\n"
	   "3  actual days/365\n"
	   "4  European 30/360\n"
	   "\n"
676
	   "If @settlement date or @maturity date is not valid, PRICEDISC "
Valek Frob's avatar
Valek Frob committed
677
	   "returns #NUM! error. "
678
	   "If @basis is omitted, US 30/360 is applied. "
Valek Frob's avatar
Valek Frob committed
679
	   "If @basis < 0 or @basis > 4, PRICEDISC returns #NUM! error. "
680
	   "If @settlement date is after @maturity date or they are the same, "
Valek Frob's avatar
Valek Frob committed
681
	   "PRICEDISC returns #NUM! error. "
682
	   "\n"
683 684
	   "@EXAMPLES=\n"
	   "\n"
685 686 687 688 689 690
	   "@SEEALSO=PRICEMAT")
};

static Value *
gnumeric_pricedisc (FunctionEvalInfo *ei, Value **argv)
{
Jody Goldberg's avatar
Jody Goldberg committed
691
	gnum_float discount, redemption, a, d;
Morten Welinder's avatar
Morten Welinder committed
692
	int basis;
693

694
	discount   = value_get_as_float (argv[2]);
695
	redemption = value_get_as_float (argv[3]);
696
	basis      = argv[4] ? value_get_as_int (argv[4]) : 0;
697 698 699 700

	a = days_monthly_basis (argv[0], argv[1], basis);
	d = annual_year_basis (argv[0], basis);

701
	if (a <= 0 || d <= 0 || !is_valid_basis (basis))
702
                return value_new_error (ei->pos, gnumeric_err_NUM);
703 704 705 706

	return value_new_float (redemption - discount * redemption * a/d);
}

707 708
/***************************************************************************/

Morten Welinder's avatar
Morten Welinder committed
709
static const char *help_pricemat = {
710
	N_("@FUNCTION=PRICEMAT\n"
711
	   "@SYNTAX=PRICEMAT(settlement,maturity,issue,rate,yield[,basis])\n"
712 713 714
	   "@DESCRIPTION="
	   "PRICEMAT calculates and returns the price per $100 face value "
	   "of a security.  The security pays interest at maturity. "
715 716 717 718
	   "@settlement is the settlement date of the security.  @maturity is "
	   "the maturity date of the security.  @issue is the issue date of "
	   "the security.  @rate is the discount rate of the security. "
	   "@yield is the annual yield of the security. "
719 720 721 722 723 724 725 726
	   "@basis is the type of day counting system you want to use:\n"
	   "\n"
	   "0  US 30/360\n"
	   "1  actual days/actual days\n"
	   "2  actual days/360\n"
	   "3  actual days/365\n"
	   "4  European 30/360\n"
	   "\n"
727
	   "If @settlement date or @maturity date is not valid, PRICEMAT "
Valek Frob's avatar
Valek Frob committed
728
	   "returns #NUM! error. "
729
	   "If @basis is omitted, US 30/360 is applied. "
Valek Frob's avatar
Valek Frob committed
730
	   "If @basis < 0 or @basis > 4, PRICEMAT returns #NUM! error. "
731
	   "If @settlement date is after @maturity date or they are the same, "
Valek Frob's avatar
Valek Frob committed
732
	   "PRICEMAT returns #NUM! error. "
733
	   "\n"
734 735
	   "@EXAMPLES=\n"
	   "\n"
736 737 738 739 740 741
	   "@SEEALSO=PRICEDISC")
};

static Value *
gnumeric_pricemat (FunctionEvalInfo *ei, Value **argv)
{
Jody Goldberg's avatar
Jody Goldberg committed
742
	gnum_float discount, yield, a, b, dsm, dim, n;
Morten Welinder's avatar
Morten Welinder committed
743
	int basis;
744 745

	discount = value_get_as_float (argv[3]);
746 747
	yield    = value_get_as_float (argv[4]);
	basis    = argv[5] ? value_get_as_int (argv[5]) : 0;
748 749 750

	dsm = days_monthly_basis (argv[0], argv[1], basis);
	dim = days_monthly_basis (argv[2], argv[1], basis);
751 752
	a   = days_monthly_basis (argv[2], argv[0], basis);
	b   = annual_year_basis (argv[0], basis);
753

754 755
	if (a <= 0 || b <= 0 || dsm <= 0 || dim <= 0
	    || !is_valid_basis (basis))
756
                return value_new_error (ei->pos, gnumeric_err_NUM);
757

758 759 760 761
	n = 1 + (dsm/b * yield);
	if (n == 0)
	        return value_new_error (ei->pos, gnumeric_err_NUM);

762
	return value_new_float (((100 + (dim/b * discount * 100)) /
763
				 (n)) - (a/b * discount * 100));
764
}
765

766 767
/***************************************************************************/

Morten Welinder's avatar
Morten Welinder committed
768
static const char *help_disc = {
769
	N_("@FUNCTION=DISC\n"
Jon K Hellan's avatar
Jon K Hellan committed
770
	   "@SYNTAX=DISC(settlement,maturity,par,redemption[,basis])\n"
771 772
	   "@DESCRIPTION="
	   "DISC calculates and returns the discount rate for a sequrity. "
773 774 775 776
	   "@settlement is the settlement date of the security.  @maturity "
	   "is the maturity date of the security.  @par is the price per $100 "
	   "face value of the security.  @redemption is the redeption value "
	   "per $100 face value of the security. "
777 778 779 780 781 782 783 784
	   "@basis is the type of day counting system you want to use:\n"
	   "\n"
	   "0  US 30/360\n"
	   "1  actual days/actual days\n"
	   "2  actual days/360\n"
	   "3  actual days/365\n"
	   "4  European 30/360\n"
	   "\n"
785
	   "If @settlement date or @maturity date is not valid, DISC "
Valek Frob's avatar
Valek Frob committed
786
	   "returns #NUM! error. "
787
	   "If @basis is omitted, US 30/360 is applied. "
Valek Frob's avatar
Valek Frob committed
788
	   "If @basis < 0 or @basis > 4, DISC returns #NUM! error. "
789
	   "If @settlement date is after @maturity date or they are the same, "
Valek Frob's avatar
Valek Frob committed
790
	   "DISC returns #NUM! error. "
791
	   "\n"
792 793
	   "@EXAMPLES=\n"
	   "\n"
794 795 796 797 798 799
	   "@SEEALSO=")
};

static Value *
gnumeric_disc (FunctionEvalInfo *ei, Value **argv)
{
Jody Goldberg's avatar
Jody Goldberg committed
800
	gnum_float par, redemption, dsm, b;
Morten Welinder's avatar
Morten Welinder committed
801
	int basis;
802

803
	par        = value_get_as_float (argv[2]);
804
	redemption = value_get_as_float (argv[3]);
805
	basis      = argv[4] ? value_get_as_int (argv[4]) : 0;
806 807 808 809

	b = annual_year_basis (argv[0], basis);
	dsm = days_monthly_basis (argv[0], argv[1], basis);

810
	if (dsm <= 0 || b <= 0 || dsm <= 0 || !is_valid_basis (basis)
811
	    || redemption == 0)
812
                return value_new_error (ei->pos, gnumeric_err_NUM);
813 814 815 816

	return value_new_float ((redemption - par) / redemption * (b / dsm));
}

817 818
/***************************************************************************/

Morten Welinder's avatar
Morten Welinder committed
819
static const char *help_effect = {
820
	N_("@FUNCTION=EFFECT\n"
821
	   "@SYNTAX=EFFECT(r,nper)\n"
822 823
	   "@DESCRIPTION="
	   "EFFECT calculates the effective interest rate from "
824
	   "a given nominal rate.\n"
825
	   "Effective interest rate is calculated using this formula:\n"
826
	   "\n"
827
           "    (1 + @r / @nper) ^ @nper - 1\n"
828 829
	   "\n"
	   "where:\n"
830
	   "\n"
831 832
	   "@r = nominal interest rate (stated in yearly terms)\n"
	   "@nper = number of periods used for compounding"
833
	   "\n"
834
	   "@EXAMPLES=\n"
835 836
	   "For example credit cards will list an APR (annual percentage "
	   "rate) which is a nominal interest rate."
837
	   "\n"
838 839 840
	   "For example if you wanted to find out how much you are actually "
	   "paying interest on your credit card that states an APR of 19% "
	   "that is compounded monthly you would type in:"
841
	   "\n"
842 843
	   "=EFFECT(.19,12) and you would get .2075 or 20.75%. That is the "
	   "effective percentage you will pay on your loan."
844 845
	   "\n"
	   "@SEEALSO=NOMINAL")
846
};
847

848
static Value *
849
gnumeric_effect (FunctionEvalInfo *ei, Value **argv)
850
{
Jody Goldberg's avatar
Jody Goldberg committed
851
	gnum_float rate;
852 853
	int nper;

854 855
	rate = value_get_as_float (argv[0]);
	nper = value_get_as_int (argv[1]);
856

857
	/* Rate or number of periods cannot be negative */
Morten Welinder's avatar
Morten Welinder committed
858
	if (rate < 0 || nper <= 0)
859
		return value_new_error (ei->pos, _("effect - domain error"));
860

861
        return value_new_float (powgnum ((1 + rate / nper), nper) - 1);
862
}
863

864 865
/***************************************************************************/

Morten Welinder's avatar
Morten Welinder committed
866
static const char *help_nominal = {
867
	N_("@FUNCTION=NOMINAL\n"
868
	   "@SYNTAX=NOMINAL(r,nper)\n"
869 870
	   "@DESCRIPTION="
	   "NOMINAL calculates the nominal interest rate from "
871 872 873
	   "a given effective rate.\n"
	   "Nominal interest rate is given by a formula:\n"
	   "\n"
874
           "@nper * (( 1 + @r ) ^ (1 / @nper) - 1 )"
875 876 877
	   "\n"
	   "where:\n"
	   "\n"
878 879
	   "@r = effective interest rate\n"
	   "@nper = number of periods used for compounding"
880
	   "\n"
881 882
	   "@EXAMPLES=\n"
	   "\n"
883 884 885 886
	   "@SEEALSO=EFFECT")
};

static Value *
887
gnumeric_nominal (FunctionEvalInfo *ei, Value **argv)
888
{
Jody Goldberg's avatar
Jody Goldberg committed
889
	gnum_float rate;
890
	int nper;
891

892 893
	rate = value_get_as_float (argv[0]);
	nper = value_get_as_int (argv[1]);
894 895

	/* Rate or number of periods cannot be negative */