Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

I don't think that

sage: -0.5 in srange(-1,1,.01)
False

is because a mismatch in type. I think this is because .01 is not exactly 10^(-2) because the latter isn't exactly representable as an IEEE float). Hence we have:

sage: -0.5 - srange(-1,1,.01)[50]
-4.44089209850063e-16

(adding .01 50 times to -1 yields a different float). The types are taken care of:

sage: -0.5 in srange(-10,10,0.5)
True

Don't use equality testing (and hence don't use "in") when working with floats. Also: don't use srange with floats. You'll lose precision.

sage: [ (n*0.01)-a for n,a in enumerate(srange(0,0.2,0.01))]
[0.000000000000000,
 0.000000000000000,
 0.000000000000000,
 0.000000000000000,
 0.000000000000000,
 0.000000000000000,
 -6.93889390390723e-18,
 0.000000000000000,
 0.000000000000000,
 0.000000000000000,
 1.38777878078145e-17,
 1.38777878078145e-17,
 1.38777878078145e-17,
 2.77555756156289e-17,
 2.77555756156289e-17,
 0.000000000000000,
 0.000000000000000,
 0.000000000000000,
 -2.77555756156289e-17,
 -2.77555756156289e-17]

(it gets worse as you go further). srange could in principle test its arguments and revert to a more stable generation formula in case the arguments are floats.