I am trying to get my head around the specific details of how to use curly braces in LTspice. My end goal is to have a random value be generated and used before the start of a simulation for e.g. a resistor value. While playing around I happened to have something like this:
R=myfunc({flat(1)})+1
as the value of a resistor.
Then I define the function
.func myfunc( x ) { ( x + x ) - 2*x }
Now the LTspice documentation states:
When curly braces are encountered, the enclosed expression is evaluated on the basis of all relations available at the scope and reduced to a floating point value.
or
To invoke parameter substitution and expression evaluation with these user-defined functions, enclose the expression in curly braces. The enclosed expression will be replaced with the floating-point value.
at various places in .func
and .param
documentation. From this I would expect the above to always return 1 Ω for the resistor, because the random value is passed as a float to the function, however, it looks like x
is evaluated multiple times, as for many runs via .step
I get multiple different values but never 1... For transient and DC operating point it's the same...
What is going on here? Did x become a reference to that flat(1)
call and only really substituted for the float value when it is "really used"? That doesn't really sound like it's replaced with the floating-point value at that specific point where it is encountered...
As requested an example .asc
file of the function in use ( I guess thats a bit more convenient than the netlist ):
Version 4
SHEET 1 880 680
FLAG 144 176 0
FLAG 64 96 0
SYMBOL res 128 80 R0
SYMATTR InstName R1
SYMATTR Value R=myfunc({flat(1)})+1
SYMBOL voltage 160 96 R90
SYMATTR InstName V1
SYMATTR Value 1
TEXT 206 210 Left 2 !.tran 1m
TEXT 424 48 Left 2 !.func myfunc( x ) { ( x + x ) - 2*x }
TEXT 208 240 Left 2 !.step param r 0 10 1
TEXT 208 272 Left 2 !.meas ii avg I(R1)
myfunc(x)
function. Just take any simulation with a resistor and pluck in the mentioned R= expression for the value, and put that function in there. \$\endgroup\$