0

I'm trying to generate the value between given range i.e. values from 10^-1 up to 10^−14 and I'm getting a wrong output

I have implemented the logic to understand this logspace, but couldn't figure it out.

import numpy as np

delta = np.logspace(1, 14, dtype=np.float64)
print(delta)

Expected output is :

>>> print(delta)
[  1.00000000e-01   1.00000000e-02   1.00000000e-03   1.00000000e-04
   1.00000000e-05   1.00000000e-06   1.00000000e-07   1.00000000e-08
   1.00000000e-09   1.00000000e-10   1.00000000e-11   1.00000000e-12
   1.00000000e-13   1.00000000e-14]
1
  • 1
    Read the docs? np.logspace(1, 14, 14) Commented Sep 13, 2019 at 5:05

1 Answer 1

1

From the documentation:

In linear space, the sequence starts at base ** start (base to the power of start) and ends with base ** stop.

So np.logspace(-1, -14, 14) gives:

array([1.e-01, 1.e-02, 1.e-03, 1.e-04, 1.e-05, 1.e-06, 1.e-07, 1.e-08,
   1.e-09, 1.e-10, 1.e-11, 1.e-12, 1.e-13, 1.e-14])

Or with more "precision" (as asked in the comment):

np.set_printoptions(formatter=dict(float='{:10.8e}'.format))
np.logspace(-1, -14, 14)

Gives:

[1.00000000e-01 1.00000000e-02 1.00000000e-03 1.00000000e-04
 1.00000000e-05 1.00000000e-06 1.00000000e-07 1.00000000e-08
 1.00000000e-09 1.00000000e-10 1.00000000e-11 1.00000000e-12
 1.00000000e-13 1.00000000e-14]

Note: this changes every numpy array with floats that you print afterwards..!

Sign up to request clarification or add additional context in comments.

3 Comments

how can I show the decimal precision? Like expected output in question?
You can control that with np.set_printoptions(), e.g. np.set_printoptions(formatter=dict(float='{:10.8e}'.format)). Then the result is 1.00000000e-01 1.00000000e-02 1.00000000e-03 ....
I was using the set_precisions() and thank you for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.