This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Left shift and Right shift for floats
Type: enhancement Stage:
Components: Interpreter Core Versions: Python 3.3
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: Nosy List: daniel.urban, dtorp, eric.smith, jcea, loewis, mark.dickinson, rhettinger
Priority: normal Keywords:

Created on 2011-05-01 03:05 by dtorp, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (6)
msg134897 - (view) Author: David Albert Torpey (dtorp) Date: 2011-05-01 03:05
I would like to left and right shift floats as a fast way to multiply or divide by a power of 2 without rounding error.  The only way to do that now is t=frexp(x) and y=ldexp(t[0],t[1]+2).  But would be better to type y=x<<2.  Thank you.
msg134904 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2011-05-01 09:51
"The only way ..." That's not true. y=x*(1<<n) is also an exact multiplication with a power of two, and it's also fairly fast. Likewise for y=x/(1<<n).
msg134916 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2011-05-01 17:25
Would you want x >> 2 to be equivalent to x / 4.0 or x // 4.0?
msg134917 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2011-05-01 17:50
> The only way to do that now is t=frexp(x) and y=ldexp(t[0],t[1]+2).

What's wrong with the more direct ldexp(x, 2)?

N.B.  There *are* edge cases where Martin's suggested alternative won't work.  E.g., to compute 1e-300 * 2**1500:

>>> ldexp(1e-300, 1500)
3.507466211043404e+151

But:

>>> 1e-300 * (1 << 1500)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OverflowError: long int too large to convert to float

I'm -1 on the proposal:  I recognise the need for these operations, but I think 'ldexp(x, n)' gives an adequate solution.  Fitting the functionality to the << and >> operators is awkward, because that's not what those operators do for integers (>> produces the *floor* of the quotient, and << doesn't currently accept negative arguments on the right, so there's no neat way for integers to divide by a power of 2).

See also the rejected issue 1205239.
msg134954 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2011-05-02 08:10
Ok, I'm closing this as won't fix. Mark is our authority on these matters, and having two spellings for this fairly uncommon operation seems enough.
msg135002 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2011-05-02 19:20
A hint that the idea would be useful is that there is an FPU instruction, FSCALE, devoted to this.  However, a hint that this isn't needed is that Fortran and MATLAB don't have it.
History
Date User Action Args
2022-04-11 14:57:16adminsetgithub: 56176
2011-05-02 20:37:47jceasetnosy: + jcea
2011-05-02 19:20:42rhettingersetmessages: + msg135002
2011-05-02 08:10:16loewissetstatus: open -> closed
resolution: wont fix
messages: + msg134954
2011-05-01 17:50:53mark.dickinsonsetmessages: + msg134917
2011-05-01 17:25:29mark.dickinsonsetmessages: + msg134916
2011-05-01 09:51:28loewissetnosy: + loewis
messages: + msg134904
2011-05-01 08:50:05eric.smithsetnosy: + eric.smith
2011-05-01 07:24:05daniel.urbansetnosy: + daniel.urban
2011-05-01 03:27:16rhettingersetnosy: + rhettinger
type: enhancement
components: + Interpreter Core
2011-05-01 03:09:23ezio.melottisetnosy: + mark.dickinson
2011-05-01 03:05:21dtorpcreate