功能

The sections on expressions and parameters shows how component and source values can be defined using arithmetic equations. The examples used to illustrate this used only simple linear expressions. This section introduces the concept of functions.

Functions hugely expand the power of parameters and expressions by allowing the creation of expressions including non-linear functions of other parameters.

Predefined functions

EasyEDA has a number of pre-defined functions. Many of them are immediately available to be used in expressions because they are built-in to ngspice or they are automatically added in to the spice netlist by EasyEDA at the time of first saving a schematic.

Some are not yet automatically added in to the netlist so their definitions must be pasted in manually before they can be used. These function definitions are clearly indicated in the examples illustrating each function.

These functions will be appended to the list of those automatically added to the netlist soon.

All the currently available pre-defined functions are listed, together with illustrative examples, in the table below.

Note that all the functions in this list can be used in any context in EasyEDA: in the value fields and in expressions for component values (E), Independent Sources (I) and for B Sources (B).

Table of functions

Function Description ngspice native or EasyEDA special Where useable
abs(x) Absolute value of x ngspice E, I, B
acos(x) Arc cosine of x. Fails to converge for x < -1 and x > +1. Use invcos(x) instead. ngspice E, I, B
acosh(x) Real part of the arc hyperbolic cosine of x, e.g., acosh(.5) returns 0, not 1.0472i EasyEDA E, I, B
arctan(x) Alternate syntax to atan(x) ngspice E, I, B
asin(x) Arc sine of x. Fails to converge for x < -1 and x > +1. Use invsin(x) instead. ngspice E, I, B
asinh(x) Arc hyperbolic sine EasyEDA E, I, B
atan(x) Arc tangent of x ngspice E, I, B
atan2(y,x) 4 quadrant arc tangent of x/y (tan^-1(x/y)) EasyEDA E, I, B
atanh(x) Arc hyperbolic tangent. (Limited output swing to avoid numerical under/overflow failure.) EasyEDA E, I, B
buf(x) Returns 1 if x > 0.5, else 0 EasyEDA E, I, B
ceil(x) Integer equal or greater than x ngspice E, I, B
cos(x) Cosine of x ngspice E, I, B
cosh(x) Hyperbolic cosine of x ngspice E, I, B
exp(x) e to the power of x ngspice E, I, B
floor(x) Integer equal to or less than x ngspice E, I, B
if(x,y,z) IF x > 0.5, THEN y ELSE z EasyEDA E, I, B
ifx(x,y,z) IF x, THEN y ELSE z EasyEDA E, I, B
int(x) Convert x to integer EasyEDA E, I, B
inv(x) Returns 0 if x > 0.5, else 1 EasyEDA E, I, B
invcos(x) Real part of the arc cosine of x, e.g., acos(-5) returns 3.14159, not 3.14159+2.29243i EasyEDA E, I, B
invsin(x) Real part of the arc sine of x, asin(-5) returns -1.57080, not -1.57080+2.29243i EasyEDA E, I, B
invtan(x) Alternate syntax to atan(x) EasyEDA E, I, B
limit(x, L, U) Value of x, bounded by L and U EasyEDA E, I, B
ln(x) Natural logarithm of x. Fails with errors for negative x. Use log(x) instead. ngspice E, I, B
log(x) Natural logarithm of x. Generates a real valued output for all x, limited to a minimum of approximately -230.5 for x <= 1e-100. EasyEDA E, I, B
log10(x) Base 10 logarithm. Generates a real valued output for all x, limited to a minimum of -100 for x <= 1e-100. EasyEDA E, I, B
max(x,y) The greater of x or y ngspice E, I, B
min(x,y) The smaller of x or y ngspice E, I, B
pow(x,a) Real part of x raised to the power of a. Zero for negative x and fractional a EasyEDA E, I, B
pwr(x,a) The absolute value of x raised to the power of a EasyEDA E, I, B
pwrs(x,a) pwr(x) multiplied by the sign of x EasyEDA E, I, B
sgn(x) Sign of x. Returns -1 for x < 0, 0 for x == 0 (where == means ‘exactly equal to’) and 1 for x > 0 ngspice E, I, B
sin(x) Sine of x ngspice E, I, B
sinh(x) Hyperbolic sine of x ngspice E, I, B
softlim(ip, lo, hi, sharp) Value of ip, bounded by lo and hi with the sharpness of the transition between linear and limited regions defined by ‘sharp’. EasyEDA E, I, B
sqr(x) Square of x EasyEDA E, I, B
sqrt(x) Real part of the square root of x. Zero for negative x EasyEDA E, I, B
stp(x) Alternate syntax for u(x) EasyEDA E, I, B
tan(x) Tangent of x ngspice E, I, B
tanh(x) Hyperbolic tangent of x ngspice E, I, B
u(x) Unit step, i.e., 1 if x > 0., else 0 EasyEDA E, I, B

User defined functions

There may be occasions where a function is required that maybe has to be used in several places in a schematic or it is useful in several different schematics. To save having to copy and paste a complicated expression as a block of text each time it is needed, the .func statement makes it is possible to create a user defined function.

The syntax of the .func statement is very simple:

.func myfunctionname(a,b,c, ...n) {expression of functions of a, b, c ... n}

For example:

.func hypotenuse(x,y) {sqrt(x^2+y^2)}

defines a function that calculates the length of the hypotenuse of a right angle triangle with sides length x and y.

Once a function has been defined in a schematic in a project in this way it can be used anywhere in any schematic in that project, i.e. it does not have to be defined separately in every sheet in which it is used in a project. It does, however, have to be defined in a sheet in every project in which it is to be used but, if it’s a really useful function, let us know and maybe we’ll add it to the growing list of pre-defined functions!

To use the function all that is then required is to paste hypotenuse(x,y) into wherever it is needed and to substitute the ‘x’ and ‘y’ with the required variables. So, for example to use the function in a parametric expression:

.param a=3 b=4 hypot=hypotenuse(a,b)

or in a current output B Source driven by two voltages, V(oneside) and V(otherside):

I=hypotenuse(V(oneside), V(otherside))

There are many examples of functions defined by the .func statement in the simulations linked to in the table above and in all EasyEDA spice netlists for the automatically appended predefined functions.

Note that when used in .func statements, expressions can wrap over more than one line by using the ‘+’ continuation character.

There are several examples of this in the simulations linked to in the table above and in all EasyEDA spice netlists for the automatically appended predefined functions. For example:

can be found appended to every EasyEDA spice netlist. 