Skip to content

Commit

Permalink
fix: remove fcmp library and rewrite expressions without using its ma…
Browse files Browse the repository at this point in the history
…cros (#131)

Fixes #107
  • Loading branch information
ToddFincannon authored Oct 8, 2021
1 parent 34f25f8 commit df76872
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 149 deletions.
66 changes: 0 additions & 66 deletions src/c/fcmp.c

This file was deleted.

55 changes: 0 additions & 55 deletions src/c/fcmp.h

This file was deleted.

2 changes: 1 addition & 1 deletion src/c/makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Do "export P={c-file-basename}" before running this makefile.
OBJECTS=main.o vensim.o model.o macros.o fcmp.o
OBJECTS=main.o vensim.o model.o macros.o
CFLAGS=-Wall -O3
LDLIBS=

Expand Down
18 changes: 0 additions & 18 deletions src/c/sde.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,6 @@ extern "C" {

EXTERN double _epsilon;

#ifdef PRECISION_FCMP
// Optional high-precision but slow floating point comparison macros
#include "fcmp.h"
#define fz(x) (fcmp(x, 0.0, _epsilon) == 0)
#define feq(x1,x2) (fcmp(x1, x2, _epsilon) == 0)
#define flt(x1,x2) (fcmp(x1, x2, _epsilon) == -1)
#define fle(x1,x2) (fcmp(x1, x2, _epsilon) <= 0)
#define fgt(x1,x2) (fcmp(x1, x2, _epsilon) == 1)
#define fge(x1,x2) (fcmp(x1, x2, _epsilon) >= 0)
#else
#define fz(x) (fabs(x) < _epsilon)
#define feq(x1,x2) (x1 == x2)
#define flt(x1,x2) (x1 < x2)
#define fle(x1,x2) (x1 <= x2)
#define fgt(x1,x2) (x1 > x2)
#define fge(x1,x2) (x1 >= x2)
#endif

// Enable this to add print statements in initLevels and evalAux for debugging.
// #define PRDBG
#ifdef PRDBG
Expand Down
16 changes: 8 additions & 8 deletions src/c/vensim.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ double _PULSE(double start, double width) {
}
double _PULSE_TRAIN(double start, double width, double interval, double end) {
double n = floor((end - start) / interval);
for (double k = 0; fle(k, n); k++) {
if (_PULSE(start + k * interval, width) && fle(_time, end)) {
for (double k = 0; k <= n; k++) {
if (_PULSE(start + k * interval, width) && _time <= end) {
return 1.0;
}
}
Expand All @@ -31,8 +31,8 @@ double _RAMP(double slope, double start_time, double end_time) {
// Interpolate from start time to end time.
// Hold at the end time value.
// Allow start time > end time.
if (fgt(_time, start_time)) {
if (flt(_time, end_time) || fgt(start_time, end_time)) {
if (_time > start_time) {
if (_time < end_time || start_time > end_time) {
return slope * (_time - start_time);
} else {
return slope * (end_time - start_time);
Expand All @@ -41,9 +41,9 @@ double _RAMP(double slope, double start_time, double end_time) {
return 0.0;
}
}
double _XIDZ(double a, double b, double x) { return fz(b) ? x : a / b; }
double _XIDZ(double a, double b, double x) { return fabs(b) < _epsilon ? x : a / b; }
double _ZIDZ(double a, double b) {
if (fz(b)) {
if (fabs(b) < _epsilon) {
return 0.0;
} else {
return a / b;
Expand Down Expand Up @@ -117,14 +117,14 @@ double __lookup(Lookup* lookup, double input, bool use_inverted_data, LookupMode
for (size_t xi = start_index; xi < max; xi += 2) {
double x = data[xi];

if (fge(x, input)) {
if (x >= input) {
// We went past the input, or hit it exactly.
if (use_cached_values) {
lookup->last_input = input;
lookup->last_hit_index = xi;
}

if (xi == 0 || feq(x, input)) {
if (xi == 0 || x == input) {
// The input is less than the first x, or this x equals the input; return the
// associated y without interpolation.
return data[xi + 1];
Expand Down
2 changes: 1 addition & 1 deletion src/c/vensim.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ extern "C" {
#define _SAMPLE_IF_TRUE(current, condition, input) (bool_cond(condition) ? (input) : (current))
#define _SIN(x) sin(x)
#define _SQRT(x) sqrt(x)
#define _STEP(height, step_time) (fgt(_time + _time_step / 2.0, (step_time)) ? (height) : 0.0)
#define _STEP(height, step_time) (_time + _time_step / 2.0 > (step_time) ? (height) : 0.0)

double* _ALLOCATE_AVAILABLE(double* requested_quantities, double* priority_profiles, double available_resource, size_t num_requesters);
double _PULSE(double start, double width);
Expand Down

0 comments on commit df76872

Please sign in to comment.