3

How to write a for loop that values varies from 1e-70 to 1e-10 with arbitrary step like 0.000001.

for i in np.arange (1e-70, 1e-10):

or writing a for loop for big values as 1e20 to 1e33

I didint know what should I search to find the relevant answer.

Ma Y
  • 696
  • 8
  • 19
  • 1
    Possible duplicate of [How to use a decimal range() step value?](https://stackoverflow.com/questions/477486/how-to-use-a-decimal-range-step-value) – Sayse Nov 12 '19 at 14:09
  • @Sayse it is not duplicate. I am noy saying how to use decimal in for loop. I am saying hot to use very small or very big numbers – Ma Y Nov 12 '19 at 14:11
  • 1
    both of which are floats, hence the duplicate. – Sayse Nov 12 '19 at 14:11
  • @Sayse this is like you tell me to wirte `for i in range(0.00000000000000000000000000000000001, 0.0000000000000000000000001):` this does not make sense – Ma Y Nov 12 '19 at 14:13
  • Please try to be a bit more specific. What kind of values would you expect in your sequence? Equally-spaced values? Or logarithmically-spaced? Or something else? What would be an example of that "arbitrary step"? – jdehesa Nov 12 '19 at 14:13
  • @jdehesa I wrote a loop between `1e-70` to `1e-20` for instance. a very small values – Ma Y Nov 12 '19 at 14:15
  • 2
    The top answer to that question shows exactly how to do that, just replace their numbers with yours... https://stackoverflow.com/a/477635/7203433 – Josh Karpel Nov 12 '19 at 14:19
  • @JoshKarpel These answers are for small decimals nt for a number which has `70` decimal and more. I wonder how to write this amount of zero in one loop. – Ma Y Nov 12 '19 at 14:20
  • 1
    You can just write `np.arange(1e-70, 1e-10, 0.000001)`? But it will only contain `1e-70` because the step that you are proposing (`0.000001`) is larger than the distance between the two extremes. Can you please give an example of what values you would expect to see in your result? – jdehesa Nov 12 '19 at 14:22

2 Answers2

3

If your objective is to get[1e-70, 1e-69, 1e-68,...], why not use np.logspace?

np.logspace(-70, -10, 61)

returns exactly that.

neko
  • 379
  • 1
  • 5
2

You should specify a step in np.arange() like this:

for i in np.aragne(1e-70, 1e-10, 1e-20):

But note, if your step is bigger than values range, you'll get only a start of your range. So in case np.arange(1e-70, 1e-10, step=1e-6) you'll have only one iteration with i=1e-70.

Or if you want to have N equally spaced numbers, you can use np.linspace():

for i in np.linspace(1e-70, 1e-10, 1000): # 1000 numbers in [1e-70, 1e-10]

P.S. Looking at your question, it seem's that you want to get a sequence like [1e-70, 1e-69, 1e-68, ...] instead of [1e-70, 1e-70+step, 1e-70+2*step, ...]. In it is so, you can do it like this:

for i in np.logspace(-70, -10, 61):
Taras Khalymon
  • 614
  • 4
  • 11