From 9e72296e40e4c3dcb90d5c463a61b7932fc34088 Mon Sep 17 00:00:00 2001 From: rlar Date: Sat, 20 Sep 2014 12:42:36 +0200 Subject: [PATCH] src/spicelib/parse, implement `nint()' (`B' language) --- src/include/ngspice/inpptree.h | 1 + src/spicelib/parser/inpptree.c | 5 +++++ src/spicelib/parser/inpxx.h | 1 + src/spicelib/parser/ptfuncs.c | 9 +++++++++ 4 files changed, 16 insertions(+) diff --git a/src/include/ngspice/inpptree.h b/src/include/ngspice/inpptree.h index 2343220ed..2aaffbb46 100644 --- a/src/include/ngspice/inpptree.h +++ b/src/include/ngspice/inpptree.h @@ -128,6 +128,7 @@ void INPptPrint(char *str, IFparseTree * ptree); #define PTF_MAX 33 #define PTF_CEIL 34 #define PTF_FLOOR 35 +#define PTF_NINT 36 /* The following things are used by the parser -- these are the token types the * lexer returns. diff --git a/src/spicelib/parser/inpptree.c b/src/spicelib/parser/inpptree.c index 02a2d2f63..b292621a3 100644 --- a/src/spicelib/parser/inpptree.c +++ b/src/spicelib/parser/inpptree.c @@ -156,6 +156,7 @@ static struct func { { "uramp", PTF_URAMP, (void(*)(void)) PTuramp } , { "ceil", PTF_CEIL, (void(*)(void)) PTceil } , { "floor", PTF_FLOOR, (void(*)(void)) PTfloor } , + { "nint", PTF_NINT, (void(*)(void)) PTnint } , { "-", PTF_UMINUS, (void(*)(void)) PTuminus }, /* MW. cif function added */ { "u2", PTF_USTEP2, (void(*)(void)) PTustep2}, @@ -491,6 +492,10 @@ static INPparseNode *PTdifferentiate(INPparseNode * p, int varnum) arg1 = mkcon(0.0); break; + case PTF_NINT: /* naive: D(nint(u)) = 0 */ + arg1 = mkcon(0.0); + break; + /* MW. PTF_CIF for new cif function */ case PTF_USTEP2: /* ustep2=uramp(x)-uramp(x-1) ustep2'=ustep(x)-ustep(x-1) */ diff --git a/src/spicelib/parser/inpxx.h b/src/spicelib/parser/inpxx.h index 0bcb5c407..83dc2da72 100644 --- a/src/spicelib/parser/inpxx.h +++ b/src/spicelib/parser/inpxx.h @@ -76,5 +76,6 @@ double PTge0(double arg); double PTle0(double arg); double PTceil(double arg); double PTfloor(double arg); +double PTnint(double arg); #endif diff --git a/src/spicelib/parser/ptfuncs.c b/src/spicelib/parser/ptfuncs.c index 3dc41f646..6986f236e 100644 --- a/src/spicelib/parser/ptfuncs.c +++ b/src/spicelib/parser/ptfuncs.c @@ -339,3 +339,12 @@ PTfloor(double arg1) return (floor(arg1)); } +double +PTnint(double arg1) +{ + /* round to "nearest integer", + * round half-integers to the nearest even integer + * rely on default rounding mode of IEEE 754 to do so + */ + return nearbyint(arg1); +}