fn-financial.c 86.8 KB
Newer Older
1 2 3
/*
 * fn-financial.c:  Built in financial functions and functions registration
 *
4
 * Authors:
5
 *  Vladimir Vuksan (vuksan@veus.hr)
6
 *  Jukka-Pekka Iivonen (iivonen@iki.fi)
7
 *
8

9 10
 */
#include <config.h>
11
#include <math.h>
Morten Welinder's avatar
Morten Welinder committed
12
#include <limits.h>
13
#include "gnumeric.h"
14
#include "parse-util.h"
15
#include "cell.h"
16
#include "func.h"
Morten Welinder's avatar
Morten Welinder committed
17
#include "goal-seek.h"
18
#include "collect.h"
Morten Welinder's avatar
Morten Welinder committed
19
#include "auto-format.h"
Morten Welinder's avatar
Morten Welinder committed
20
#include "datetime.h"
21

22 23 24 25 26
/*

Below are some of the functions that are used quite often in
financial analysis.

27
Present value interest factor
28

Morten Welinder's avatar
Morten Welinder committed
29
	 PVIF = (1 + k) ^ n
30

31
Future value interest factor
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48

         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



49 50
	 PV * PVIF(k%, nper) + PMT * ( 1 + rate * type ) *
	      FVIFA(k%, nper) + FV = 0
51 52 53

 */

54 55
static float_t
calculate_pvif (float_t rate, float_t nper)
56
{
Morten Welinder's avatar
Morten Welinder committed
57
	return (pow (1 + rate, nper));
58 59
}

Morten Welinder's avatar
Morten Welinder committed
60
#if 0
61 62
static float_t
calculate_fvif (float_t rate, float_t nper)
63
{
64
	return (1.0 / calculate_pvif (rate,nper));
65 66
}

67 68
static float_t
calculate_pvifa (float_t rate, float_t nper)
69
{
Morten Welinder's avatar
Morten Welinder committed
70
	return ((1.0 / rate) - (1.0 / (rate * pow (1 + rate, nper))));
71
}
Morten Welinder's avatar
Morten Welinder committed
72
#endif
73

74 75
static float_t
calculate_fvifa (float_t rate, float_t nper)
76
{
Morten Welinder's avatar
Morten Welinder committed
77
	return ((pow (1 + rate, nper) - 1) / rate);
78 79
}

80 81 82 83 84 85 86 87
/*

Principal for period x is calculated using this formula

PR(x) = PR(0) * ( 1 + rate ) ^ x + PMT * ( ( 1 + rate ) ^ x - 1 ) / rate )

*/

88
static float_t
89 90
calculate_principal (float_t starting_principal, float_t payment,
		     float_t rate, float_t period)
91
{
92
	return (starting_principal * pow (1.0 + rate, period) + payment *
Morten Welinder's avatar
Morten Welinder committed
93
		((pow (1 + rate, period) - 1) / rate));
94 95
}

96 97
static float_t
calculate_pmt (float_t rate, float_t nper, float_t pv, float_t fv, int type)
98
{
99
	float_t pvif, fvifa;
100 101 102

	/* Calculate the PVIF and FVIFA */

103 104
	pvif = calculate_pvif (rate,nper);
	fvifa = calculate_fvifa (rate,nper);
105

Morten Welinder's avatar
Morten Welinder committed
106
        return ((-pv * pvif - fv ) / ((1.0 + rate * type) * fvifa));
107 108
}

109
static int
Morten Welinder's avatar
Morten Welinder committed
110
annual_year_basis (Value *value_date, int basis)
111 112 113 114 115 116 117 118
{
        GDate    *date;
        gboolean leap_year;

	switch (basis) {
	case 0:
	        return 360;
	case 1:
Morten Welinder's avatar
Morten Welinder committed
119
	        date = datetime_value_to_g (value_date);
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
		if (date != NULL) {
		        leap_year = g_date_is_leap_year (g_date_year (date));
			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;
	}
}

static int
Morten Welinder's avatar
Morten Welinder committed
138
days_monthly_basis (Value *issue_date, Value *maturity_date, int basis)
139
{
140 141 142 143 144 145
        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;
146

Morten Welinder's avatar
Morten Welinder committed
147 148
	date_i = datetime_value_to_g (issue_date);
	date_m = datetime_value_to_g (maturity_date);
149
	if (date_i != NULL && date_m != NULL) {
150 151 152 153 154 155 156 157 158 159 160
	        issue_year = g_date_year (date_i);
	        issue_month = g_date_month (date_i);
	        issue_day = g_date_day (date_i);
	        maturity_year = g_date_year (date_m);
	        maturity_month = g_date_month (date_m);
	        maturity_day = g_date_day (date_m);

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

161
		months = years * 12 + months;
162 163
		leap_year = g_date_is_leap_year (issue_year);

164 165 166 167 168 169 170 171 172 173
		g_date_free (date_i);
		g_date_free (date_m);
	} else {
	        g_date_free (date_i);
	        g_date_free (date_m);
	        return -1;
	}

	switch (basis) {
	case 0:
174
	        if (issue_month == 2 && maturity_month != 2 &&
Morten Welinder's avatar
Morten Welinder committed
175
		    issue_year == maturity_year) {
176 177 178 179 180
			if (leap_year)
				return months * 30 + days - 1;
			else
				return months * 30 + days - 2;
		}
181 182 183 184
	        return months * 30 + days;
	case 1:
	case 2:
	case 3:
Morten Welinder's avatar
Morten Welinder committed
185 186
	        issue = datetime_value_to_serial (issue_date);
	        maturity = datetime_value_to_serial (maturity_date);
187 188 189 190 191 192 193 194
	        return maturity - issue;
	case 4:
	        return months * 30 + days;
	default:
	        return -1;
	}
}

Michael Meeks's avatar
Michael Meeks committed
195 196
/*
 * Returns the number of days in the coupon period of the settlement date.
197 198 199
 * Currently, returns negative numbers if the branch is not implemented.
 */
static float_t
Michael Meeks's avatar
Michael Meeks committed
200
coupdays (GDate *settlement, GDate *maturity, int freq, int basis)
201
{
202 203 204 205
        GDateYear  sy, my;
	GDateMonth sm, mm;
	GDateDay   sd, md;

206 207 208 209 210 211 212 213 214 215 216 217 218 219
        switch (basis) {
        case 0:
        case 2:
        case 4:
                if (freq == 1)
                        return 360.0;
                else if (freq == 2)
                        return 180.0;
                else
                        return 90.0;
                break;
        case 3:
                return 365.0 / freq;
        case 1:
Michael Meeks's avatar
Michael Meeks committed
220 221
		sy = g_date_year  (settlement);
		my = g_date_year  (maturity);
222 223
		sm = g_date_month (settlement);
		mm = g_date_month (maturity);
Michael Meeks's avatar
Michael Meeks committed
224 225
		sd = g_date_day   (settlement);
		md = g_date_day   (maturity);
226 227 228

	        if (freq == 1) {
		        if (g_date_is_leap_year (sy)) {
Michael Meeks's avatar
Michael Meeks committed
229
			        if (sm == 1 || sm == 2) {
230 231 232 233
				        if (sm < mm || (sm == mm && sd < md))
					        return 365.0;
					else
					        return 366.0;
Michael Meeks's avatar
Michael Meeks committed
234
				}
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
				if (mm == 1 || mm == 2)
				        return 366.0;
				if (sm < mm || (sm == mm && sd < md))
				        return 366.0;
				else
				        return 365.0;
			} else if (g_date_is_leap_year (sy-1)) {
			        if (sm == 1 || sm == 2)
				        if (mm == 1 || mm == 2)
					        if (sm < mm || 
						    (sm == mm && sd < md))
						        return 366.0;
						else
						        return 365.0;
					else
					        return 365.0;
				else
				        return 365.0;
			} else if (g_date_is_leap_year (sy+1)) {
			        if (sm == 1 || sm == 2)
				        return 365.0;
				if (mm == 1 || mm == 2)
				        return 365.0;
				if (sm < mm || (sm == mm && sd < md))
				        return 365.0;
				else
				        return 366.0;
			} else
			        return 365.0;
		}
265 266 267 268 269
        default:
                return -1.0;
        }
}

Michael Meeks's avatar
Michael Meeks committed
270 271
/*
 * Returns the number of days from the beginning of the coupon period to 
272 273 274 275 276 277 278 279 280 281 282
 * the settlement date.  Currently, returns negative numbers if the branch
 * is not implemented.
 */
static int
coupdaybs(GDate *settlement, GDate *maturity, int freq, int basis)
{
        int        d, months, days;
	GDateYear  sy, my;
	GDateMonth sm, mm;
	GDateDay   sd, md;

Michael Meeks's avatar
Michael Meeks committed
283 284
	sy = g_date_year  (settlement);
	my = g_date_year  (maturity);
285 286
	sm = g_date_month (settlement);
	mm = g_date_month (maturity);
Michael Meeks's avatar
Michael Meeks committed
287 288
	sd = g_date_day   (settlement);
	md = g_date_day   (maturity);
289 290 291 292 293 294 295

	months = mm - sm;

        switch (basis) {
	case 0: /* US 30/360 */
	        days = md - sd;

Michael Meeks's avatar
Michael Meeks committed
296 297 298
	        if (! g_date_is_leap_year (sy) &&
		    g_date_is_leap_year (my) &&
		    mm == 2 && md == 29)
299
		        --days;
Michael Meeks's avatar
Michael Meeks committed
300 301 302
		else if (g_date_is_leap_year (sy) &&
			 !g_date_is_leap_year (my) &&
			 mm == 2 && md == 28) {
303 304 305 306
		        if (sd == 29 && sm == 2)
			        return 0;
			else
			        days += 2;
Michael Meeks's avatar
Michael Meeks committed
307
		}
308 309 310 311

		d = 360 - months*30 - days;

	        if (freq == 1) {
Jody Goldberg's avatar
Jody Goldberg committed
312
			if (d >= 360) {
313 314 315 316 317 318
			        if (days == 0 && months == 0)
				        d = 0;
			        else if (d % 360 == 0)
				        d = 360;
				else
				        d %= 360;
Jody Goldberg's avatar
Jody Goldberg committed
319
			}
Michael Meeks's avatar
Michael Meeks committed
320
		} else if (freq == 2) {
321 322 323 324
			if ((d % 180) == 0 && days)
			        d = 180;
			else
			        d %= 180;
Michael Meeks's avatar
Michael Meeks committed
325
		} else {
326 327 328 329
			if ((d % 90) == 0 && days)
			        d = 90;
			else
			        d %= 90;
Michael Meeks's avatar
Michael Meeks committed
330
		}
331 332 333 334 335

		return d;
	case 1:
	case 2:
	case 3:
Jukka-Pekka Iivonen's avatar
Jukka-Pekka Iivonen committed
336
	        return -1;
337
	case 4: /* European 30/360 */
Michael Meeks's avatar
Michael Meeks committed
338 339 340
	        if (!g_date_is_leap_year (sy) &&
		    g_date_is_leap_year (my) &&
		    mm == 2 && md == 29)
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
		        return 0;

	        if (sd == 31)
		        sd = 30;
		if (md == 31)
		        md = 30;

		days = md - sd;
		d = 360 - months*30 - days;

	        if (freq == 1) {
		        if ((d % 360) == 0 && days)
			        d = 360;
			else
			        d %= 360;
		} else if (freq == 2) {
		        if ((d % 180) == 0 && days)
			        d = 180;
			else
			        d %= 180;
		} else
		        if ((d % 90) == 0 && days)
			        d = 90;
			else
			        d %= 90;
		return d;
	default:
	        return -1;
	}
}

Jukka-Pekka Iivonen's avatar
Jukka-Pekka Iivonen committed
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
static GDateDay
days_in_month(GDateYear year, GDateMonth month)
{
        switch (month) {
	case 1:
	case 3:
	case 5:
	case 7:
	case 8:
	case 10:
	case 12:
	        return 31;
	case 4:
	case 6:
	case 9:
	case 11:
	        return 30;
	case 2:
	        if (g_date_is_leap_year(year))
		        return 29;
		else
		        return 28;
	default:
	        return 0;
	}
}

399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
static int
coupnum(GDate *settlement, GDate *maturity, int freq, int basis)
{
        int        years, months, days;
	GDateYear  sy, my;
	GDateMonth sm, mm;
	GDateDay   sd, md;

	sy = g_date_year  (settlement);
	sm = g_date_month (settlement);
	sd = g_date_day   (settlement);
	my = g_date_year  (maturity);
	mm = g_date_month (maturity);
	md = g_date_day   (maturity);

	years = my - sy;
	months = mm - sm;
	if (md == days_in_month (my, mm) && sd == days_in_month (sy, sm))
	        days = 0;
	else
	        days = md - sd + (g_date_is_leap_year (sy) && sd==28 && sm==2);

	if (freq == 1)
	        return years + (months > 0) + (months == 0 && days > 0);
	else if (freq == 2)
	        return years*2
		        + (months >  6) + (months ==  6 && days > 0)
		        + (months >  0) + (months ==  0 && days > 0)
		        - (months < -6) - (months == -6 && days < 0);
	else
	        return years*4
		        + (months >  9) + (months ==  9 && days > 0)
		        + (months >  6) + (months ==  6 && days > 0)
		        + (months >  3) + (months ==  3 && days > 0)
		        + (months >  0) + (months ==  0 && days > 0)
		        - (months < -3) - (months == -3 && days < 0)
		        - (months < -6) - (months == -6 && days < 0)
		        - (months < -9) - (months == -9 && days < 0);
}

Jukka-Pekka Iivonen's avatar
Jukka-Pekka Iivonen committed
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
static GDate *
coupncd(GDate *settlement, GDate *maturity, int freq, int basis)
{
        int        months, days;
	GDateYear  sy, my, year;
	GDateMonth sm, mm, month;
	GDateDay   sd, md, day;

	sy = g_date_year (settlement);
	sm = g_date_month (settlement);
	sd = g_date_day (settlement);
	my = g_date_year (maturity);
	mm = g_date_month (maturity);
	md = g_date_day (maturity);

	if (freq == 1) {
	        day = md;
		month = mm;
		if (sy ==  my || sm < mm || (sm == mm && sd < md))
		        year = sy;
		else
		        year = sy + 1;
	} else if (freq == 2) {
	        days = md - sd;
		months = mm - sm;

		if ((months > 0 || (months == 0 && days > 0)) &&
		    (months < 6 || (months == 6 && days <= 0))) {
		        month = mm;
			year = sy;
		} else if (months > 6 || (months == 6 && days > 0)) {
		        month = mm - 6;
			year = sy;
		} else if (months < -6 || (months == -6 && days <= 0)) {
		        month = mm;
			year = sy + 1;
		} else {
		        month = mm + 6;
			year = sy;
			if (month > 12) {
			        month -= 12;
				year++;
			}
		}
		if (md == days_in_month (my, mm))
		        day = days_in_month (year, month);
		else
		        day = md;
	}  else {
	        days = md - sd;
		months = mm - sm;
		
		if ((months > 0 || (months == 0 && days > 0)) &&
		    (months < 3 || (months == 3 && days <= 0))) {
		        month = mm;
			year = sy;
		} else if ((months > 3 || (months == 3 && days > 0))
			   && (months < 6 || (months == 6 && days <= 0))) {
		        month = mm - 3;
			year = sy;
		} else if ((months > 6 || (months == 6 && days > 0))
			   && (months < 9 || (months == 9 && days <= 0))) {
		        month = mm - 6;
			year = sy;
		} else if ((months > 9 || (months == 9 && days > 0))) {
		        month = mm - 9;
			year = sy;
		} else if (months < -9 || (months == -9 && days <= 0)) {
		        month = mm;
			year = sy + 1;
		} else if (months < -6 || (months == -6 && days <= 0)) {
		        month = mm + 9;
			year = sy;
			if (month > 12) {
			        month -= 12;
				year++;
			}
		} else if (months < -3 || (months == -3 && days <= 0)) {
		        month = mm + 6;
			year = sy;
			if (month > 12) {
			        month -= 12;
				year++;
			}
		} else {
		        month = mm + 3;
			year = sy;
			if (month > 12) {
			        month -= 12;
				year++;
			}
		}

		if (md == days_in_month(my, mm))
		        day = days_in_month(year, month);
		else
		        day = md;
	}

	if (! g_date_is_leap_year (year) && day == 29 && month == 2)
	        day = 28;

	if ((year > my || (year == my && month > mm)
	     || (year == my && month == mm && day > md))) {
	        year = my;
		month = mm;
		day = md;
	}

	if (year < sy || (year == sy && month < sm)
	    || (year == sy && month == sm && day < sd)) {
	        year = sy;
		month = mm;
		day = md;
	}

	return g_date_new_dmy (day, month, year);
}
557

558
/***************************************************************************/
559

560 561 562 563 564
static char *help_accrint = {
	N_("@FUNCTION=ACCRINT\n"
	   "@SYNTAX=ACCRINT(issue,first_interest,settlement,rate,par,"
	   "frequency[,basis])\n"
	   "@DESCRIPTION="
565
	   "ACCRINT calculates the accrued interest for a "
566 567 568
	   "security that pays periodic interest.  The @rate is the annual "
	   "rate of the security and @par is the par value of the security. "
	   "@basis is the type of day counting system you want to use:\n"
569 570 571 572 573 574 575 576
	   "\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"
	   "If @basis is omitted, US 30/360 is applied. "
577 578
	   "If @issue date or @settlement date is not valid, ACCRINT returns "
	   "#NUM! error. "
Valek Frob's avatar
Valek Frob committed
579 580
	   "If @rate or @par is zero or negative, ACCRINT returns #NUM! error. "
	   "If @basis < 0 or @basis > 4, ACCRINT returns #NUM! error. "
581
	   "If @issue date is after @settlement date or they are the same, "
Valek Frob's avatar
Valek Frob committed
582
	   "ACCRINT returns #NUM! error. "
583
	   "\n"
584 585
	   "@EXAMPLES=\n"
	   "\n"
586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605
	   "@SEEALSO=ACCRINTM")
};

static Value *
gnumeric_accrint (FunctionEvalInfo *ei, Value **argv)
{
	float_t rate, a, d, par, freq, coefficient, x;
	int     basis;

	rate = value_get_as_float (argv[3]);
	par = value_get_as_float (argv[4]);
	freq = value_get_as_float (argv[5]);
	if (argv[6] == NULL)
	        basis = 0;
	else
	        basis = value_get_as_int (argv[6]);

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

606 607
	if (a < 0 || d <= 0 || par <= 0 || rate <= 0 || basis < 0 || basis > 4
	    || freq == 0)
608
                return value_new_error (ei->pos, gnumeric_err_NUM);
609 610 611 612 613 614 615

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

	return value_new_float (coefficient * freq * x);
}

616 617
/***************************************************************************/

618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634
static char *help_accrintm = {
	N_("@FUNCTION=ACCRINTM\n"
	   "@SYNTAX=ACCRINTM(issue,maturity,rate[,par,basis])\n"
	   "@DESCRIPTION="
	   "ACCRINTM calculates and returns the accrued interest for a "
	   "security from @issue to @maturity date.  @rate is the annual "
	   "rate of the security and @par is the par value of the security. "
	   "If you omit @par, ACCRINTM applies $1,000 instead.  @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"
	   "If @basis is omitted, US 30/360 is applied. "
635 636
	   "If @issue date or @maturity date is not valid, ACCRINTM returns "
	   "#NUM! error. "
Valek Frob's avatar
Valek Frob committed
637 638
	   "If @rate or @par is zero or negative, ACCRINTM returns #NUM! error. "
	   "If @basis < 0 or @basis > 4, ACCRINTM returns #NUM! error. "
639
	   "If @issue date is after @maturity date or they are the same, "
Valek Frob's avatar
Valek Frob committed
640
	   "ACCRINTM returns #NUM! error. "
641
	   "\n"
642 643
	   "@EXAMPLES=\n"
	   "\n"
644
	   "@SEEALSO=ACCRINT")
645 646 647 648 649 650 651 652 653
};

static Value *
gnumeric_accrintm (FunctionEvalInfo *ei, Value **argv)
{
	float_t rate, a, d, par;
	int     basis;

	rate = value_get_as_float (argv[2]);
Morten Welinder's avatar
Morten Welinder committed
654 655
	par = argv[3] ? value_get_as_float (argv[3]) : 1000;
	basis = argv[4] ? value_get_as_int (argv[4]) : 0;
656 657 658 659

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

Dom Lachowicz's avatar
Dom Lachowicz committed
660
	if (a < 0 || d <= 0 || par <= 0 || rate <= 0 || basis < 0 || basis > 4)
661
                return value_new_error (ei->pos, gnumeric_err_NUM);
662 663 664 665

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

666 667
/***************************************************************************/

668 669 670 671 672
static char *help_intrate = {
	N_("@FUNCTION=INTRATE\n"
	   "@SYNTAX=INTRATE(settlement,maturity,investment,redemption"
	   "[,basis])\n"
	   "@DESCRIPTION="
673 674
	   "INTRATE calculates and returns the interest rate of a fully "
	   "vested security. "
675 676 677 678 679 680 681 682 683 684 685 686
	   "@investment is the prize of the security paid at @settlement "
	   "date and @redemption is the amount to be received at @maturity "
	   "date.  @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"
	   "If @basis is omitted, US 30/360 is applied. "
687 688
	   "If @settlement date or @maturity date is not valid, INTRATE returns "
	   "#NUM! error. "
Valek Frob's avatar
Valek Frob committed
689
	   "If @basis < 0 or @basis > 4, INTRATE returns #NUM! error. "
690
	   "If @settlement date is after @maturity date or they are the same, "
Valek Frob's avatar
Valek Frob committed
691
	   "INTRATE returns #NUM! error. "
692
	   "\n"
693 694
	   "@EXAMPLES=\n"
	   "\n"
695 696 697 698 699 700 701 702 703
	   "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")
704 705 706 707 708 709 710 711 712 713
};

static Value *
gnumeric_intrate (FunctionEvalInfo *ei, Value **argv)
{
	float_t investment, redemption, a, d;
	int     basis;

	investment = value_get_as_float (argv[2]);
	redemption = value_get_as_float (argv[3]);
Morten Welinder's avatar
Morten Welinder committed
714
	basis = argv[4] ? value_get_as_int (argv[4]) : 0;
715 716 717 718

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

Dom Lachowicz's avatar
Dom Lachowicz committed
719
	if (basis < 0 || basis > 4 || a <= 0 || d <= 0 || investment == 0)
720
                return value_new_error (ei->pos, gnumeric_err_NUM);
721 722 723 724 725

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

726 727
/***************************************************************************/

728 729
static char *help_received = {
	N_("@FUNCTION=RECEIVED\n"
730
	   "@SYNTAX=RECEIVED(settlement,maturity,investment,rate[,basis])\n"
731 732 733 734 735 736 737 738 739 740 741 742 743
	   "@DESCRIPTION="
	   "RECEIVED calculates and returns the amount to be received at "
	   "@maturity date for a security bond. "
	   "@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"
	   "If @basis is omitted, US 30/360 is applied. "
744
	   "If @settlement date or @maturity date is not valid, RECEIVED "
Valek Frob's avatar
Valek Frob committed
745 746
	   "returns #NUM! error. "
	   "If @basis < 0 or @basis > 4, RECEIVED returns #NUM! error. "
747
	   "If @settlement date is after @maturity date or they are the same, "
Valek Frob's avatar
Valek Frob committed
748
	   "RECEIVED returns #NUM! error. "
749
	   "\n"
750 751
	   "@EXAMPLES=\n"
	   "\n"
752 753 754 755 756 757
	   "@SEEALSO=INTRATE")
};

static Value *
gnumeric_received (FunctionEvalInfo *ei, Value **argv)
{
758
	float_t investment, discount, a, d, n;
759 760 761 762
	int     basis;

	investment = value_get_as_float (argv[2]);
	discount = value_get_as_float (argv[3]);
Morten Welinder's avatar
Morten Welinder committed
763
	basis = argv[4] ? value_get_as_int (argv[4]) : 0;
764 765 766 767 768

	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)
769
                return value_new_error (ei->pos, gnumeric_err_NUM);
770

771 772 773 774 775
	n = 1.0 - (discount * a/d);
	if (n == 0)
	        return value_new_error (ei->pos, gnumeric_err_NUM);

	return value_new_float (investment / n);
776 777
}

778 779
/***************************************************************************/

780 781
static char *help_pricedisc = {
	N_("@FUNCTION=PRICEDISC\n"
782
	   "@SYNTAX=PRICEDISC(settlement,maturity,discount,redemption[,basis])\n"
783 784 785 786 787 788 789 790 791 792 793 794 795 796 797
	   "@DESCRIPTION="
	   "PRICEDISC calculates and returns the price per $100 face value "
	   "of a security bond.  The security does not pay interest at "
	   "maturity.  @discount is the rate for which the security "
	   "is discounted.  @redemption is the amount to be received on "
	   "@maturity date.  @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"
	   "If @basis is omitted, US 30/360 is applied. "
798
	   "If @settlement date or @maturity date is not valid, PRICEDISC "
Valek Frob's avatar
Valek Frob committed
799 800
	   "returns #NUM! error. "
	   "If @basis < 0 or @basis > 4, PRICEDISC returns #NUM! error. "
801
	   "If @settlement date is after @maturity date or they are the same, "
Valek Frob's avatar
Valek Frob committed
802
	   "PRICEDISC returns #NUM! error. "
803
	   "\n"
804 805
	   "@EXAMPLES=\n"
	   "\n"
806 807 808 809 810 811 812 813 814 815 816
	   "@SEEALSO=PRICEMAT")
};

static Value *
gnumeric_pricedisc (FunctionEvalInfo *ei, Value **argv)
{
	float_t discount, redemption, a, d;
	int     basis;

	discount = value_get_as_float (argv[2]);
	redemption = value_get_as_float (argv[3]);
Morten Welinder's avatar
Morten Welinder committed
817
	basis = argv[4] ? value_get_as_int (argv[4]) : 0;
818 819 820 821 822

	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)
823
                return value_new_error (ei->pos, gnumeric_err_NUM);
824 825 826 827

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

828 829
/***************************************************************************/

830 831
static char *help_pricemat = {
	N_("@FUNCTION=PRICEMAT\n"
832
	   "@SYNTAX=PRICEMAT(settlement,maturity,issue,rate,yield[,basis])\n"
833 834 835 836 837 838 839 840 841 842 843 844
	   "@DESCRIPTION="
	   "PRICEMAT calculates and returns the price per $100 face value "
	   "of a security.  The security pays interest at maturity. "
	   "@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"
	   "If @basis is omitted, US 30/360 is applied. "
845
	   "If @settlement date or @maturity date is not valid, PRICEMAT "
Valek Frob's avatar
Valek Frob committed
846 847
	   "returns #NUM! error. "
	   "If @basis < 0 or @basis > 4, PRICEMAT returns #NUM! error. "
848
	   "If @settlement date is after @maturity date or they are the same, "
Valek Frob's avatar
Valek Frob committed
849
	   "PRICEMAT returns #NUM! error. "
850
	   "\n"
851 852
	   "@EXAMPLES=\n"
	   "\n"
853 854 855 856 857 858
	   "@SEEALSO=PRICEDISC")
};

static Value *
gnumeric_pricemat (FunctionEvalInfo *ei, Value **argv)
{
859
	float_t discount, yield, a, b, dsm, dim, n;
860 861 862 863
	int     basis;

	discount = value_get_as_float (argv[3]);
	yield = value_get_as_float (argv[4]);
Morten Welinder's avatar
Morten Welinder committed
864
	basis = argv[5] ? value_get_as_int (argv[5]) : 0;
865 866 867 868 869 870

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

Morten Welinder's avatar
Morten Welinder committed
871
	if (a <= 0 || b <= 0 || dsm <= 0 || dim <= 0 || basis < 0 ||
872
	    basis > 4)
873
                return value_new_error (ei->pos, gnumeric_err_NUM);
874

875 876 877 878
	n = 1 + (dsm/b * yield);
	if (n == 0)
	        return value_new_error (ei->pos, gnumeric_err_NUM);

879
	return value_new_float (((100 + (dim/b * discount * 100)) /
880
				 (n)) - (a/b * discount * 100));
881
}
882

883 884
/***************************************************************************/

885 886
static char *help_disc = {
	N_("@FUNCTION=DISC\n"
Jon K Hellan's avatar
Jon K Hellan committed
887
	   "@SYNTAX=DISC(settlement,maturity,par,redemption[,basis])\n"
888 889 890 891 892 893 894 895 896 897 898
	   "@DESCRIPTION="
	   "DISC calculates and returns the discount rate for a sequrity. "
	   "@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"
	   "If @basis is omitted, US 30/360 is applied. "
899
	   "If @settlement date or @maturity date is not valid, DISC "
Valek Frob's avatar
Valek Frob committed
900 901
	   "returns #NUM! error. "
	   "If @basis < 0 or @basis > 4, DISC returns #NUM! error. "
902
	   "If @settlement date is after @maturity date or they are the same, "
Valek Frob's avatar
Valek Frob committed
903
	   "DISC returns #NUM! error. "
904
	   "\n"
905 906
	   "@EXAMPLES=\n"
	   "\n"
907 908 909 910 911 912 913 914 915 916 917
	   "@SEEALSO=")
};

static Value *
gnumeric_disc (FunctionEvalInfo *ei, Value **argv)
{
	float_t par, redemption, dsm, b;
	int     basis;

	par = value_get_as_float (argv[2]);
	redemption = value_get_as_float (argv[3]);
Morten Welinder's avatar
Morten Welinder committed
918
	basis = argv[4] ? value_get_as_int (argv[4]) : 0;
919 920 921 922

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

Dom Lachowicz's avatar
Dom Lachowicz committed
923
	if (dsm <= 0 || b <= 0 || dsm <= 0 || basis < 0 || basis > 4 || redemption == 0)
924
                return value_new_error (ei->pos, gnumeric_err_NUM);
925 926 927 928

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

929 930
/***************************************************************************/

931 932
static char *help_effect = {
	N_("@FUNCTION=EFFECT\n"
933
	   "@SYNTAX=EFFECT(r,nper)\n"
934 935
	   "@DESCRIPTION="
	   "EFFECT calculates the effective interest rate from "
936 937 938
	   "a given nominal rate.\n"
	   "Effective interest rate is calculated using this formulae:\n"
	   "\n"
939 940 941
	   "         @r"
           "( 1 + ------ ) ^ @nper - 1"
           "       @nper"
942 943
	   "\n"
	   "where:\n"
944
	   "\n"
945 946
	   "@r = nominal interest rate (stated in yearly terms)\n"
	   "@nper = number of periods used for compounding"
947
	   "\n"
948
	   "@EXAMPLES=\n"
949 950
	   "For example credit cards will list an APR (annual percentage "
	   "rate) which is a nominal interest rate."
951
	   "\n"
952 953 954
	   "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:"
955
	   "\n"
956 957
	   "=EFFECT(.19,12) and you would get .2075 or 20.75%. That is the "
	   "effective percentage you will pay on your loan."
958 959
	   "\n"
	   "@SEEALSO=NOMINAL")
960
};
961

962
static Value *
963
gnumeric_effect (FunctionEvalInfo *ei, Value **argv)
964
{
965
	float_t rate;
966 967
	int nper;

968 969
	rate = value_get_as_float (argv[0]);
	nper = value_get_as_int (argv[1]);
970

971
	/* Rate or number of periods cannot be negative */
Dom Lachowicz's avatar
Dom Lachowicz committed
972
	if ((rate < 0) || (nper <= 0))
973
		return value_new_error (ei->pos, _("effect - domain error"));
974

Morten Welinder's avatar
Morten Welinder committed
975
        return value_new_float ( pow ( (1 + rate/nper) , nper) - 1 );
976
}
977

978 979
/***************************************************************************/

980 981
static char *help_nominal = {
	N_("@FUNCTION=NOMINAL\n"
982
	   "@SYNTAX=NOMINAL(r,nper)\n"
983 984
	   "@DESCRIPTION="
	   "NOMINAL calculates the nominal interest rate from "
985 986 987
	   "a given effective rate.\n"
	   "Nominal interest rate is given by a formula:\n"
	   "\n"
988
           "@nper * (( 1 + @r ) ^ (1 / @nper) - 1 )"
989 990 991
	   "\n"
	   "where:\n"
	   "\n"
992 993
	   "@r = effective interest rate\n"
	   "@nper = number of periods used for compounding"
994
	   "\n"
995 996
	   "@EXAMPLES=\n"
	   "\n"
997 998 999 1000
	   "@SEEALSO=EFFECT")
};

static Value *
1001
gnumeric_nominal (FunctionEvalInfo *ei, Value **argv)
1002
{
1003
	float_t rate;
1004
	int nper;
1005

1006 1007
	rate = value_get_as_float (argv[0]);
	nper = value_get_as_int (argv[1]);
1008 1009

	/* Rate or number of periods cannot be negative */
1010
	if (rate < 0 || nper <= 0)
1011
		return value_new_error (ei->pos, _("nominal - domain error"));
1012

Morten Welinder's avatar
Morten Welinder committed
1013
        return value_new_float ( nper * ( pow ( 1 + rate, 1.0/nper ) - 1 ) );
1014 1015 1016

}

1017 1018
/***************************************************************************/

Jukka-Pekka Iivonen's avatar
Jukka-Pekka Iivonen committed
1019 1020 1021 1022
static char *help_ispmt = {
	N_("@FUNCTION=ISPMT\n"
	   "@SYNTAX=ISPMT(rate,per,nper,pv)\n"
	   "@DESCRIPTION="
Morten Welinder's avatar
Morten Welinder committed
1023
	   "ISPMT function returns the interest paid on a given period. "
Jukka-Pekka Iivonen's avatar
Jukka-Pekka Iivonen committed
1024 1025 1026
	   "\n"
	   "If @per < 1 or @per > @nper, ISPMT returns #NUM! error. "
	   "\n"
1027 1028
	   "@EXAMPLES=\n"
	   "\n"
Jukka-Pekka Iivonen's avatar
Jukka-Pekka Iivonen committed
1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043
	   "@SEEALSO=PV")
};

static Value *
gnumeric_ispmt (FunctionEvalInfo *ei, Value **argv)
{
	float_t rate, pv, tmp;
	int     nper, per;

	rate = value_get_as_float (argv[0]);
	per = value_get_as_int (argv[1]);
	nper = value_get_as_int (argv[2]);
	pv = value_get_as_float (argv[3]);

	if (per < 1 || per > nper)
1044
                return value_new_error (ei->pos, gnumeric_err_NUM);
Jukka-Pekka Iivonen's avatar
Jukka-Pekka Iivonen committed
1045 1046 1047 1048 1049 1050

	tmp = - pv * rate;

	return value_new_float (tmp - (tmp / nper * per));
}

1051 1052
/***************************************************************************/

1053 1054
static char *help_db = {
	N_("@FUNCTION=DB\n"
Jukka-Pekka Iivonen's avatar
Jukka-Pekka Iivonen committed
1055
	   "@SYNTAX=DB(cost,salvage,life,period[,month])\n"
1056
	   "@DESCRIPTION="
1057
	   "DB calculates the depreciation of an asset for a given period "
Jukka-Pekka Iivonen's avatar
Jukka-Pekka Iivonen committed
1058
	   "using the fixed-declining balance method.  @cost is the "
1059
	   "initial value of the asset.  @salvage is the value after the depreciation. "
Jukka-Pekka Iivonen's avatar
Jukka-Pekka Iivonen committed
1060 1061 1062 1063 1064
	   "@life is the number of periods overall.  @period is the period "
	   "for which you want the depreciation to be calculated.  @month "
	   "is the number of months in the first year of depreciation. "
	   "If @month is omitted, it is assumed to be 12. "
	   "\n"
1065 1066