/*******************************************************************************
* Copyright (c) 2013-2021, Andrés Martinelli <andmarti@gmail.com> *
* All rights reserved. *
* *
* This file is a part of sc-im *
* *
* sc-im is a spreadsheet program that is based on sc. The original authors *
* of sc are James Gosling and Mark Weiser, and mods were later added by *
* Chuck Martin. *
* *
* Redistribution and use in source and binary forms, with or without *
* modification, are permitted provided that the following conditions are met: *
* 1. Redistributions of source code must retain the above copyright *
* notice, this list of conditions and the following disclaimer. *
* 2. Redistributions in binary form must reproduce the above copyright *
* notice, this list of conditions and the following disclaimer in the *
* documentation and/or other materials provided with the distribution. *
* 3. All advertising materials mentioning features or use of this software *
* must display the following acknowledgement: *
* This product includes software developed by Andrés Martinelli *
* <andmarti@gmail.com>. *
* 4. Neither the name of the Andrés Martinelli nor the *
* names of other contributors may be used to endorse or promote products *
* derived from this software without specific prior written permission. *
* *
* THIS SOFTWARE IS PROVIDED BY ANDRES MARTINELLI ''AS IS'' AND ANY *
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED *
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE *
* DISCLAIMED. IN NO EVENT SHALL ANDRES MARTINELLI BE LIABLE FOR ANY *
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES *
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;*
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND *
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT *
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE *
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
*******************************************************************************/
/**
* \file function.c
* \author Andrés Martinelli <andmarti@gmail.com>
* \date 24/05/2021
* \brief Source file that implement the different functions that sc-im can handle
* Based on sc
*
*/
#include <string.h>
#include <stdlib.h>
#include <ctype.h> // for islower toupper tolower isalpha isalnum isupper
#include <math.h>
#include "sc.h"
#include "macros.h"
#include "cmds/cmds.h"
#include "function.h"
#include "tui.h"
#include "interp.h"
#include "xmalloc.h" // for scxfree
#include "conf.h"
#include "utils/string.h"
extern struct session * session;
extern int cellerror; // get rid of this
extern int rowoffset, coloffset; /* row & col offsets for range functions */
#ifndef M_PI
#define M_PI (double)3.14159265358979323846
#endif
/**
* \brief finfunc()
* \param[in] fun
* \param[in] v1
* \param[in] v2
* \param[in] v3
* \return double
*/
double finfunc(int fun, double v1, double v2, double v3) {
double answer,p;
p = fn2_eval(pow, 1 + v2, v3);
switch (fun) {
case PV:
if (v2)
answer = v1 * (1 - 1/p) / v2;
else {
cellerror = CELLERROR;
answer = (double)0;
}
break;
case FV:
if (v2)
answer = v1 * (p - 1) / v2;
else {
cellerror = CELLERROR;
answer = (double)0;
}
break;
case PMT:
/* CHECK IF ~= 1 - 1/1 */
if (p && p != (double)1)
answer = v1 * v2 / (1 - 1/p);
else {
cellerror = CELLERROR;
answer = (double)0;
}
break;
default:
sc_error("Unknown function in finfunc");
cellerror = CELLERROR;
return ((double)0);
}
return (answer);
}
/**
* \brief dostindex()
* \param[in] minr
* \param[in] minc
* \param[in] maxr
* \param[in] maxc
* \param[in] val
* \return char *
*/
char * dostindex(struct sheet * sh, int minr, int minc, int maxr, int maxc, struct enode * val) {
int r, c;
struct ent * p;
char * pr;
p = (struct ent *) 0;
if (minr == maxr) {