[Numpy-discussion] representation of valid float type range

2021-12-29 Thread alejandro . giacometti
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.e+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.e+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
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


[Numpy-discussion] Re: representation of valid float type range

2022-01-06 Thread alejandro . giacometti
Thanks for your answer.

I think i understand it - is it that `f64_info.max - f64_info.min` does not fit 
in float64? because it approximates `2 * f64_info.max`?

In that case, I agree with Klaus, linspace should be able to handle this?
___
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


[Numpy-discussion] Re: representation of valid float type range

2022-01-06 Thread alejandro . giacometti
I'm not sure I know what that is? do you have a reference I can follow?
___
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


[Numpy-discussion] Re: representation of valid float type range

2022-01-10 Thread alejandro . giacometti
I see what you mean, there is, however, some inconsistency on how this is 
handled, and it's not entirely intuitive

```
_type=np.int8
N=8
np.linspace(
start=np.iinfo(_type).min,
stop=np.iinfo(_type).max,
num=N,
dtype=_type,
)
=>array([-128,  -92,  -56,  -19,   17,   54,   90,  127], dtype=int8)

_type=np.float16
np.linspace(
start=np.finfo(_type).min,
stop=np.finfo(_type).max,
num=N,
dtype=_type,
)
=>array([-65504., -46784., -28080.,  -9360.,   9360.,  28080.,  46784.,
65504.], dtype=float16)


_type=np.float32
np.linspace(
start=np.finfo(_type).min,
stop=np.finfo(_type).max,
num=N,
dtype=_type,
)
=>array([-3.4028235e+38, -2.4305882e+38, -1.4583529e+38, -4.8611764e+37,
4.8611764e+37,  1.4583529e+38,  2.4305882e+38,  3.4028235e+38],
  dtype=float32)

_type=np.float64
np.linspace(
start=np.finfo(_type).min,
stop=np.finfo(_type).max,
num=N,
dtype=_type,
)
=>array([nan, inf, inf, inf,
   inf, inf, inf, 1.79769313e+308])

_type=np.float64
np.linspace(
start=0,
stop=np.finfo(_type).max,
num=N,
dtype=_type,
)
=>array([0.e+000, 2.56813305e+307, 5.13626610e+307, 7.70439915e+307,
   1.02725322e+308, 1.28406652e+308, 1.54087983e+308, 1.79769313e+308])

_type=np.float64
np.linspace(
start=-1e291,
stop=np.finfo(_type).max,
num=N,
dtype=_type,
)
=>array([-1.e+291,  2.56813305e+307,  5.13626610e+307,
7.70439915e+307,  1.02725322e+308,  1.28406652e+308,
1.54087983e+308,  1.79769313e+308])

_type=np.float64
np.linspace(
start=-1e292,
stop=np.finfo(_type).max,
num=N,
dtype=_type,
)
=>array([nan, inf, inf, inf,
   inf, inf, inf, 1.79769313e+308])


_type=np.float16
np.logspace(
start=np.finfo(_type).minexp,
stop=np.finfo(_type).maxexp,
num=N,
dtype=_type,
base=2,
)
=>array([6.104e-05, 1.190e-03, 2.322e-02, 4.529e-01, 8.836e+00, 1.722e+02,
   3.360e+03,   inf], dtype=float16)

_type=np.float16
np.logspace(
start=np.finfo(_type).minexp,
stop=np.finfo(_type).maxexp,
num=N,
dtype=_type,
base=2,
endpoint=False,
)
=>array([6.104e-05, 8.211e-04, 1.105e-02, 1.487e-01, 2.000e+00, 2.691e+01,
   3.620e+02, 4.872e+03], dtype=float16)

_type=np.float64
np.logspace(
start=np.finfo(_type).minexp,
stop=np.finfo(_type).maxexp,
num=N,
dtype=_type,
base=2,
endpoint=False,
)
=>array([2.22507386e-308, 2.16653556e-231, 2.10953732e-154, 2.05403862e-077,
   2.e+000, 1.94738306e+077, 1.89615038e+154, 1.84626556e+231])
```
___
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