Hi Lev, all,

your explanation makes perfect sense, but shouldn't this be considered a bug in `linspace`?

Best
Klaus


On 29/12/2021 18:15, Lev Maximov wrote:
• Short answer:

It's because
 >>> f64_info.max - f64_info.min
inf

• Long answer:

linspace(a,b,n) tries to calculate the step by (b-a)/n and fails at (b-a).

You need to either
– split your range into two parts and then glue them back:
np.r_[np.linspace(f64_info.min, 0, 5), np.linspace(0, f64_info.max, 5)[1:]]

– or select a range that fits into float64:
np.linspace(f64_info.min/2, f64_info.max/2, 10)

– or select np.float128 as a dtype for linspace (linux/macos only):
np.linspace(np.float128(f64_info.min), np.float128(f64_info.max), 10)

Best regards,
Lev


On Wed, Dec 29, 2021 at 8:01 PM Sebastian Gurovich <seb...@gmail.com <mailto:seb...@gmail.com>> wrote:

    Could it be you need to get a handle on the "epsilon machine"?

    On Wed, 29 Dec 2021, 9:21 am , <alejandro.giacome...@gmail.com
    <mailto:alejandro.giacome...@gmail.com>> wrote:

        I am getting an interesting result, and I'm wondering if anyone
        would care to give me some intuition of why.

        The example is simple enough, I want to get a range of values
        that are representable by a type:

        ```python
        f64_info = np.finfo(np.float64)
        valid_range = np.linspace(
             start=f64_info.min, stop=f64_info.max, num=10
        )
valid_range => array([            nan,             inf,    inf,             inf,                    inf,             inf,             inf,        inf,
                            inf, 1.79769313e+308])
        ```

        The minimum value is representable by the type, I can see it:

        ```python
        f64_info.min => -1.7976931348623157e+308
        ```

        I thought that maybe the valid range cannot start with the
        minimun value, so I've tried a few alternatives:

        ```python

        valid_range = np.linspace(
             start=f64_info.min + f64_info.eps, stop=f64_info.max, num=10
        )
valid_range => array([            nan,             inf,    inf,             inf,                    inf,             inf,             inf,        inf,
                            inf, 1.79769313e+308])


        valid_range = np.linspace(
             start=f64_info.min + f64_info.tiny, stop=f64_info.max, num=10
        )
valid_range => array([            nan,             inf,    inf,             inf,                    inf,             inf,             inf,        inf,
                            inf, 1.79769313e+308])
        ```

        I thought maybe the range is too wide, but I can do this:

        ```python
        valid_range = np.linspace(
             start=0, stop=f64_info.max, num=10
        )
        valid_range => array([0.00000000e+000, 1.99743682e+307,
        3.99487363e+307, 5.99231045e+307,
                            7.98974727e+307, 9.98718408e+307,
        1.19846209e+308, 1.39820577e+308,
                            1.59794945e+308, 1.79769313e+308])

        ...

        valid_range = np.linspace(
             start=f64_info.tiny, stop=f64_info.max, num=10
        )
        valid_range => array([2.22507386e-308, 1.99743682e+307,
        3.99487363e+307, 5.99231045e+307,
                            7.98974727e+307, 9.98718408e+307,
        1.19846209e+308, 1.39820577e+308,
                            1.59794945e+308, 1.79769313e+308])

        ...

        f32_info = np.finfo(np.float32)
        valid_range = np.linspace(
             start=f32_info.tiny, stop=f32_info.max, num=10,
        dtype=np.float32,
        )
        valid_range => array([1.1754944e-38, 3.7809150e+37,
        7.5618299e+37, 1.1342745e+38,
                            1.5123660e+38, 1.8904575e+38, 2.2685490e+38,
        2.6466405e+38,
                            3.0247320e+38, 3.4028235e+38], dtype=float32)

        ```

        I know that linear space is arbitrary, and perhaps not that
        useful. In fact this is valid:

        ```python
        valid_range = np.logspace(
             start=f64_info.minexp, stop=f64_info.maxexp, num=10,
        base=2, endpoint=False
        )
        valid_range => array([2.22507386e-308, 8.67124674e-247,
        3.37923704e-185, 1.31690901e-123,
                    5.13207368e-062, 2.00000000e+000, 7.79412037e+061,
        3.03741562e+123,
                    1.18369915e+185, 4.61294681e+246])
        ```

        But I'm still confused on why linear space is invalid

        Thanks!
        _______________________________________________
        NumPy-Discussion mailing list -- numpy-discussion@python.org
        <mailto:numpy-discussion@python.org>
        To unsubscribe send an email to
        numpy-discussion-le...@python.org
        <mailto:numpy-discussion-le...@python.org>
        https://mail.python.org/mailman3/lists/numpy-discussion.python.org/
        <https://mail.python.org/mailman3/lists/numpy-discussion.python.org/>
        Member address: seb...@gmail.com <mailto:seb...@gmail.com>

    _______________________________________________
    NumPy-Discussion mailing list -- numpy-discussion@python.org
    <mailto:numpy-discussion@python.org>
    To unsubscribe send an email to numpy-discussion-le...@python.org
    <mailto:numpy-discussion-le...@python.org>
    https://mail.python.org/mailman3/lists/numpy-discussion.python.org/
    <https://mail.python.org/mailman3/lists/numpy-discussion.python.org/>
    Member address: lev.maxi...@gmail.com <mailto:lev.maxi...@gmail.com>


_______________________________________________
NumPy-Discussion mailing list -- numpy-discussion@python.org
To unsubscribe send an email to numpy-discussion-le...@python.org
https://mail.python.org/mailman3/lists/numpy-discussion.python.org/
Member address: klaus.zimmerm...@smhi.se
_______________________________________________
NumPy-Discussion mailing list -- numpy-discussion@python.org
To unsubscribe send an email to numpy-discussion-le...@python.org
https://mail.python.org/mailman3/lists/numpy-discussion.python.org/
Member address: arch...@mail-archive.com

Reply via email to