Hello, why is it that when running the while, the ...
# xschem
j
Hello, why is it that when running the while, the last value is not the same as when defining the value of L, for example I tried directly for a value of L 1.5 and it gives me 2.44, but if I run with the while that same curve gives 2,633, the strange thing is that the same number of curves appears as programmed in the while, I can't see what the error could be.
s
Try this: • the reset must be placed after the alterparam command • To ensure the last iteration is performed (L=1.5) set
stop_l=1.5001
to get rid of numerical errros while incrementing
l_act
j
Why should that 0.0001 have to be specified? is it an ngspice bug?
and is that always 0.0001, independent of the value? or if the initial value changes does that value move?
s
no, it's not a ngspice bug. The same will probably happen in a C program. In floating point arithmetic if you add many times 0.05 to a floating point number there are rounding errors. Never check for a==b if a and b are floating point numbers and one of the two is the result of a calculation. Test this C program:
Copy code
#include <stdio.h>

int main(int argc, char *argv[])
{
  double start_l = 1.0;
  double stop_l = 1.5;
  double delta_l = 0.05;
  double act_l = start_l;

  while( act_l <= stop_l) {
    printf("act_l = %g\n", act_l);
    act_l += delta_l;
  }
  return 0;
}
the output is:
Copy code
schippes@asus:~$ ./test
act_l = 1
act_l = 1.05
act_l = 1.1
act_l = 1.15
act_l = 1.2
act_l = 1.25
act_l = 1.3
act_l = 1.35
act_l = 1.4
act_l = 1.45
You see the last iteration is not performed because the last
w_act
is not exactly 1.5 since adding 0,05 many times produces rounding errors. There is no exact representation of 0.05 in binary:
0,05 (dec)  = 0.000011001100110011001100110011001100110011....(bin)
j
I understand, that is what it is about, that is, that while is working as while(i<10) and not as (i<=10)
s
add a small value to the
stop_l
and you will be ok.
you can set
stop_l = 1.5 +delta_l / 2.0
j
thanks