On 25/08/2016 22:06, Ken Kundert wrote: > Here is a fairly typical example that illustrates the usefulness of > supporting > SI scale factors and units in Python. > > This is simple simulation code that is used to test a current mirror driving > an > 100kOhm resistor ... > > Here is some simulation code that uses SI scale factors > > for delta in [-500nA, 0, 500nA]: > input = 2.75uA + delta > wait(1us) > expected = 100kOhm*(2.75uA + delta) > tolerance = 2.2mV > fails = check_output(expected, tolerance) > print('%s: I(in)=%rA, measured V(out)=%rV, expected V(out)=%rV, > diff=%rV.' % ( > 'FAIL' if fails else 'pass', > input, get_output(), expected, get_output() - expected > )) > > with the output being: > > pass: I(in)=2.25uA, measured V(out)=226.7mV, expected V(out)=225mV, > diff=1.7mV. > pass: I(in)=2.75uA, measured V(out)=276.8mV, expected V(out)=275mV, > diff=1.8mV. > FAIL: I(in)=3.25uA, measured V(out)=327.4mV, expected V(out)=325mV, > diff=2.4mV. > > And the same code in Python today ... > > for delta in [-5e-7, 0, 5e-7]: > input = 2.75e-6 + delta > wait(1e-6) > expected = 1e5*(2.75e-6 + delta) > tolerance = 2.2e-3 > fails = check_output(expected, tolerance) > print('%s: I(in)=%eA, measured V(out)=%eV, expected V(out)=%eV, > diff=%eV.' % ( > 'FAIL' if fails else 'pass', > input, get_output(), expected, get_output() - expected > )) > > with the output being: > > pass: I(in)=2.25e-6A, measured V(out)=226.7e-3V, expected V(out)=225e-3V, > diff=1.7e-3V. > pass: I(in)=2.75e-6A, measured V(out)=276.8e-3V, expected V(out)=275e-3V, > diff=1.8e-3V. > FAIL: I(in)=3.25e-6A, measured V(out)=327.4e-3V, expected V(out)=325e-3V, > diff=2.4e-3V. > > There are two things to notice. In the first example the numbers are easier to > read: for example, 500nA is easier to read the 5e-7. Second, there is > information in the units that provides provides useful information. One can > easily see that the input signal is a current and that the output is a > voltage. > Furthermore, anybody can look at this code and do a simple sanity check on > the > expressions even if they don't understand the system being simulated. They > can > check the units on the input and output expressions to assure that they are > all > consistent. > > -Ken > _______________________________________________ > And the same code with python today
def wait(delay): pass def check_output(expected, tolerance): return False def get_output(): return 1*uA def f(): for delta in [-500*nA, 0, 500*nA]: input = 2.75*uA + delta wait(1*us) expected = 100*kOhm*(2.75*uA + delta) tolerance = 2.2*mV fails = check_output(expected, tolerance) print('%s: I(in)=%rA, measured V(out)=%rV, expected V(out)=%rV, diff=%rV.' % ( 'FAIL' if fails else 'pass', input, get_output(), expected, get_output() - expected )) f() pass: I(in)=2.25e-05A, measured V(out)=1e-05V, expected V(out)=22.5V, diff=-22.49999V. pass: I(in)=2.75e-05A, measured V(out)=1e-05V, expected V(out)=27.5V, diff=-27.49999V. pass: I(in)=3.2500000000000004e-05A, measured V(out)=1e-05V, expected V(out)=32.50000000000001V, diff=-32.499990000000004V. Do you really see fundamental difference with your original code? _______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/