sinc always returns double precision

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

sinc always returns double precision

Paul Woodford

Sinc seems to always return double precision, even when the input is single precision:

 

In [125]: np.sinc(np.float32(0)).dtype

Out[125]: dtype('float64')

 

Is this desired behavior?  I’m guessing the promotion occurs in the multiplication by pi:

 

y = pi * where(x == 0, 1.0e-20, x)

 

Paul

_________

Paul Woodford, Ph.D.

Principal Research Engineer

KeyW Corporation

7763 Old Telegraph Road | Severn, MD 21144

Direct: 443.274.1466 | Main: 443.733.1500

 


_______________________________________________
NumPy-Discussion mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/numpy-discussion

smime.p7s (6K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: sinc always returns double precision

Marten van Kerkwijk
It is actually a bit more subtle (annoyingly so), the reason you get a
float64 is that you pass in a scalar, and for scalars, the dtype of
`pi` indeed "wins", as there is little reason to possibly loose
precision.  If you pass in an array instead, then you do get
`float32`:
```
np.sinc(np.array([1.], dtype=np.float32)).dtype
dtype('float32')
```
The rationale here is that for an array you generally do not want to
just blow up the memory usage, so its dtype has precedent (as long as
it is float). So, there is a reason, but it certainly leads to a lot
of confusion (e.g., https://github.com/numpy/numpy/issues/10322)

All that said, the implementation of `np.sinc` is not super - really
could do with a few more in-place operations!

-- Marten
_______________________________________________
NumPy-Discussion mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/numpy-discussion
Reply | Threaded
Open this post in threaded view
|

Re: sinc always returns double precision

Paul Woodford

Ah, I see.  That rationale makes sense.

 

Paul

 

From: NumPy-Discussion <numpy-discussion-bounces+pwoodford=[hidden email]> on behalf of Marten van Kerkwijk <[hidden email]>
Reply-To: Discussion of Numerical Python <[hidden email]>
Date: Tuesday, May 8, 2018 at 2:40 PM
To: Discussion of Numerical Python <[hidden email]>
Subject: Re: [Numpy-discussion] sinc always returns double precision

 

float64 is that you pass in a scalar, and for scalars, the dtype of

`pi` indeed "wins", as there is little reason to possibly loose

precision.  If you pass in an array instead, then you do get

`float32`:

```

np.sinc(np.array([1.], dtype=np.float32)).dtype

dtype('float32')

```

The rationale here is that for an array you generally do not want to

just blow up the memory usage, so its dtype has precedent (as long as

it is float). So, there is a reason, but it certainly leads to a lot

 

All that said, the implementation of `np.sinc` is not super - really

could do with a few more in-place operations!

 

-- Marten

_______________________________________________

NumPy-Discussion mailing list

 


_______________________________________________
NumPy-Discussion mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/numpy-discussion

smime.p7s (6K) Download Attachment