[Python-ideas] Re: division of integers should result in fractions not floats

2021-05-14 Thread Chris Angelico
On Sat, May 15, 2021 at 2:55 PM Steven D'Aprano wrote: > > On Sat, May 15, 2021 at 01:57:29AM +1000, Chris Angelico wrote: > > > Decimal literals have a number of awkward wrinkles, so I'd leave them > > aside for now; > > I'm surprised at this. > > Decimal literals have come up at least twice in t

[Python-ideas] Re: division of integers should result in fractions not floats

2021-05-14 Thread Steven D'Aprano
On Fri, May 14, 2021 at 05:49:57PM -, Martin Teichmann wrote: > 2/3 would just be the constant > two-thirds. 2 / 3 would then mean the same as it used to: divide 2 by > 3, giving some 0.666ish. I don't like your changes of getting a core developer to champion the introduction of significan

[Python-ideas] Re: division of integers should result in fractions not floats

2021-05-14 Thread Steven D'Aprano
On Sat, May 15, 2021 at 01:57:29AM +1000, Chris Angelico wrote: > Decimal literals have a number of awkward wrinkles, so I'd leave them > aside for now; I'm surprised at this. Decimal literals have come up at least twice in the past, with a general consensus that they are a good idea. This is

[Python-ideas] Re: division of integers should result in fractions not floats

2021-05-14 Thread Steven D'Aprano
On Fri, May 14, 2021 at 07:05:43PM -, Martin Teichmann wrote: > Hi Paul, > > > Also consider the section in the PEP format "How would we teach this?" > > How would you explain to someone with no programming background, maybe > > a high school student, that 3/4 and 3 / 4 mean different things i

[Python-ideas] Re: division of integers should result in fractions not floats

2021-05-14 Thread Steven D'Aprano
On Fri, May 14, 2021 at 04:58:08PM -, Martin Teichmann wrote: > Could I please see an example? This is a real question, by now I ran > my prototyped interpreter through quite some libraries, and none made > problems like this. Any script or application that does calculations and formats the

[Python-ideas] Re: division of integers should result in fractions not floats

2021-05-14 Thread Rob Cliffe via Python-ideas
On 14/05/2021 22:34, Eric V. Smith wrote: My understanding of the proposal is that OP is only talking about / becomes a Fraction. So: x=1 x/2 # unchanged, still yields a float. It's only literals like "1/2" that would become Fraction(1,2). This would appear to limit the usefulness of th

[Python-ideas] Re: division of integers should result in fractions not floats

2021-05-14 Thread Ethan Furman
On 5/14/21 2:34 PM, Eric V. Smith wrote: > My understanding of the proposal is that OP is only talking about / becomes a > Fraction. So: > > x=1 > x/2 # unchanged, still yields a float. > > It's only literals like "1/2" that would become Fraction(1,2). Ah -- which means we end up with fractio

[Python-ideas] Re: division of integers should result in fractions not floats

2021-05-14 Thread Eric V. Smith
On 5/14/2021 5:29 PM, Ethan Furman wrote: On 5/14/21 5:12 AM, Martin Teichmann wrote: > In order to showcase how that would look like, let me give an example session: > >  >>> 5/6-4/15 >  17/30 >  >>> a=22/7 >  >>> f"{a}" >  '22/7' >  >>> f"{a:f}" >  '3.142857' >   

[Python-ideas] Re: division of integers should result in fractions not floats

2021-05-14 Thread David Mertz
On Fri, May 14, 2021, 4:31 PM Jonathan Fine wrote: > >>> 1/2 + 1/3 >> 5/6 >> 1 / 2 + 1 / 3 >> 0.83 >> > > I'm sighted. I can see the difference. I suspect a blind person using a > screen reader would struggle a lot to spot the difference. (I don't know > enough about scre

[Python-ideas] Re: division of integers should result in fractions not floats

2021-05-14 Thread Ethan Furman
On 5/14/21 5:12 AM, Martin Teichmann wrote: > In order to showcase how that would look like, let me give an example session: > > >>> 5/6-4/15 > 17/30 > >>> a=22/7 > >>> f"{a}" > '22/7' > >>> f"{a:f}" > '3.142857' > >>> from decimal import Decimal > >>>

[Python-ideas] Re: division of integers should result in fractions not floats

2021-05-14 Thread Ethan Furman
On 5/14/21 1:07 PM, Paul Moore wrote: > All I'm trying to say is "I think that having 1/2 > mean something different than 1 / 2 is unacceptable, because it's too > easy for people to misunderstand". Agreed. Significant white space for control flow is great; for specifying data types it's horri

[Python-ideas] Re: division of integers should result in fractions not floats

2021-05-14 Thread Oscar Benjamin
On Fri, 14 May 2021 at 19:41, Paul Moore wrote: > > On Fri, 14 May 2021 at 16:54, Martin Teichmann > wrote: > > That is absolutely what I would like to have. The fractions module is very > > small, it can easily be made a builtin. This would also speed it up > > significantly, I hope. Probably

[Python-ideas] Re: division of integers should result in fractions not floats

2021-05-14 Thread Jonathan Fine
Martin wrote So, if you show them (the following is fake) > > >>> 1/2 + 1/3 > 5/6 > 1 / 2 + 1 / 3 > 0.83 > > They will immediately spot what's going on. > I'm sighted. I can see the difference. I suspect a blind person using a screen reader would struggle a lot to spot th

[Python-ideas] Re: division of integers should result in fractions not floats

2021-05-14 Thread Paul Moore
On Fri, 14 May 2021 at 20:06, Martin Teichmann wrote: > > Also consider the section in the PEP format "How would we teach this?" > > How would you explain to someone with no programming background, maybe > > a high school student, that 3/4 and 3 / 4 mean different things in > > Python? Your audien

[Python-ideas] Re: division of integers should result in fractions not floats

2021-05-14 Thread Martin Teichmann
Hi Paul, > Also consider the section in the PEP format "How would we teach this?" > How would you explain to someone with no programming background, maybe > a high school student, that 3/4 and 3 / 4 mean different things in > Python? Your audience might not even know that there is a difference > b

[Python-ideas] Re: division of integers should result in fractions not floats

2021-05-14 Thread Martin Teichmann
Hi Chris, > Not seeing anything there about decoupling the two modules? Well, the PR removes the line "from decimal import Decimal", I think that is quite some decoupling. The other place decimal is imported is in a classmethod only existing for backwards compatibility, so with this patch the d

[Python-ideas] Re: division of integers should result in fractions not floats

2021-05-14 Thread Guido van Rossum
I should probably explain (again) why I am not a fan of such a change. I blogged about this before -- this is mostly a treatise about / vs. //, but it explains my reservations about this proposal as well: http://python-history.blogspot.com/2009/03/problem-with-integer-division.html In particular:

[Python-ideas] Re: division of integers should result in fractions not floats

2021-05-14 Thread Paul Moore
On Fri, 14 May 2021 at 16:54, Martin Teichmann wrote: > That is absolutely what I would like to have. The fractions module is very > small, it can easily be made a builtin. This would also speed it up > significantly, I hope. Probably close to the speed of floats, given that most > of the time

[Python-ideas] Re: division of integers should result in fractions not floats

2021-05-14 Thread Richard Damon
On 5/14/21 12:58 PM, Martin Teichmann wrote: > Hi Richard, > >> I would say that I have enough code that does division of numbers that >> might be integers that expect it to be relatively efficient as floats, >> and I suspect so do others, that the backwards breaks would be >> significant. > Could

[Python-ideas] Re: division of integers should result in fractions not floats

2021-05-14 Thread Chris Angelico
On Sat, May 15, 2021 at 4:18 AM Ricky Teachey wrote: > > On Fri, May 14, 2021 at 2:09 PM Ricky Teachey wrote: >> >> On Fri, May 14, 2021 at 1:14 PM André Roberge >> wrote: >>> >>> >>> You can already experiment with this. >>> >> >> Thanks Andre I tried it out and it works great. >> >> Do the ap

[Python-ideas] Re: division of integers should result in fractions not floats

2021-05-14 Thread André Roberge
On Fri, May 14, 2021 at 3:09 PM Ricky Teachey wrote: > On Fri, May 14, 2021 at 1:14 PM André Roberge > wrote: > >> >> You can already experiment with this. >> >> >> First, install a few packages >> >> python -m pip install ideas # will also install token-utils >> python -m pip install fraction-

[Python-ideas] Re: division of integers should result in fractions not floats

2021-05-14 Thread Paul Bryan
s/non-integer/non-decimal-integer/ On Fri, 2021-05-14 at 11:23 -0700, Paul Bryan wrote: > I'll vote: bad. I don't think non-integer representations of fraction > attributes should be considered valid when expressing as a literal. > > On Fri, 2021-05-14 at 14:17 -0400, Ricky Teachey wrote: > > On

[Python-ideas] Re: division of integers should result in fractions not floats

2021-05-14 Thread Paul Bryan
I'll vote: bad. I don't think non-integer representations of fraction attributes should be considered valid when expressing as a literal. On Fri, 2021-05-14 at 14:17 -0400, Ricky Teachey wrote: > On Fri, May 14, 2021 at 2:09 PM Ricky Teachey > wrote: > > On Fri, May 14, 2021 at 1:14 PM André Robe

[Python-ideas] Re: division of integers should result in fractions not floats

2021-05-14 Thread Ricky Teachey
On Fri, May 14, 2021 at 2:09 PM Ricky Teachey wrote: > On Fri, May 14, 2021 at 1:14 PM André Roberge > wrote: > >> >> You can already experiment with this. >> >> > Thanks Andre I tried it out and it works great. > > Do the appended capital Fs make these numbers look like some kind of > hexadecim

[Python-ideas] Re: division of integers should result in fractions not floats

2021-05-14 Thread Ricky Teachey
On Fri, May 14, 2021 at 1:14 PM André Roberge wrote: > > You can already experiment with this. > > > First, install a few packages > > python -m pip install ideas # will also install token-utils > python -m pip install fraction-literal > > > Next, start your standard Python interpreter and do th

[Python-ideas] Re: division of integers should result in fractions not floats

2021-05-14 Thread Chris Angelico
On Sat, May 15, 2021 at 3:51 AM Martin Teichmann wrote: > > Hi Chris, > > I think you did not get my point. I do not want to allow x/y. I want to only > allow literals, as in 3/2. This would then be a new kind of literal, that has > the type of Fraction. Much like 2.5 is a float, but x.y means s

[Python-ideas] Re: division of integers should result in fractions not floats

2021-05-14 Thread Martin Teichmann
Hi Chris, I think you did not get my point. I do not want to allow x/y. I want to only allow literals, as in 3/2. This would then be a new kind of literal, that has the type of Fraction. Much like 2.5 is a float, but x.y means something completely different, even though it has no spaces. So x/y

[Python-ideas] Re: division of integers should result in fractions not floats

2021-05-14 Thread Serhiy Storchaka
14.05.21 15:12, Martin Teichmann пише: > when dividing two integers, the result is a float, which means we immediately > lose precision. This is not good if you want to use code which supports > higher precision. Decimals come to mind, but also sympy. This loss of > precision could be avoided if

[Python-ideas] Re: division of integers should result in fractions not floats

2021-05-14 Thread André Roberge
On Fri, May 14, 2021 at 12:40 PM Martin Teichmann < martin.teichm...@gmail.com> wrote: > Hi Paul, > > I would be fine with a new division operator or fraction literal as well, > and in the beginning this is what I actually wanted to propose. > You can already experiment with this. First, instal

[Python-ideas] Re: division of integers should result in fractions not floats

2021-05-14 Thread Chris Angelico
Premature send with incomplete info, sorry! On Sat, May 15, 2021 at 3:01 AM Chris Angelico wrote: > > On Sat, May 15, 2021 at 2:55 AM Martin Teichmann > wrote: > > > > Hi Chris, > > > > I would be willing to write such a PEP. It will take a while though, I am > > not fast at those kinda things.

[Python-ideas] Re: division of integers should result in fractions not floats

2021-05-14 Thread Chris Angelico
On Sat, May 15, 2021 at 2:55 AM Martin Teichmann wrote: > > Hi Chris, > > I would be willing to write such a PEP. It will take a while though, I am not > fast at those kinda things. Cool cool. Feel free to email me off-list about getting started; here's some handy info to read: https://www.pyth

[Python-ideas] Re: division of integers should result in fractions not floats

2021-05-14 Thread Martin Teichmann
Hi Richard, > I would say that I have enough code that does division of numbers that > might be integers that expect it to be relatively efficient as floats, > and I suspect so do others, that the backwards breaks would be > significant. Could I please see an example? This is a real question, by

[Python-ideas] Re: division of integers should result in fractions not floats

2021-05-14 Thread Martin Teichmann
Hi Chris, I would be willing to write such a PEP. It will take a while though, I am not fast at those kinda things. > Currently, fractions.py imports decimal.py, mainly (purely?) for the > sake of being able to construct a Fraction from a Decimal. The decimal > module is *large* and has other re

[Python-ideas] Re: division of integers should result in fractions not floats

2021-05-14 Thread Richard Damon
On 5/14/21 12:16 PM, Ricky Teachey wrote: > On Fri, May 14, 2021 at 12:02 PM Chris Angelico > wrote: > > On Sat, May 15, 2021 at 1:47 AM Martin Teichmann > mailto:martin.teichm...@gmail.com>> > wrote: > > > > Hi David, > > > > > A toy example wi

[Python-ideas] Re: division of integers should result in fractions not floats

2021-05-14 Thread Ricky Teachey
On Fri, May 14, 2021 at 12:02 PM Chris Angelico wrote: > On Sat, May 15, 2021 at 1:47 AM Martin Teichmann > wrote: > > > > Hi David, > > > > > A toy example with a half dozen operations won't make huge fractions. A > > > loop over a million operations will often be a gigantic memory hog. > > > >

[Python-ideas] Re: division of integers should result in fractions not floats

2021-05-14 Thread Chris Angelico
On Sat, May 15, 2021 at 1:47 AM Martin Teichmann wrote: > > Hi David, > > > A toy example with a half dozen operations won't make huge fractions. A > > loop over a million operations will often be a gigantic memory hog. > > I do not propose to do that, and I agree that it would be stupid. Usually,

[Python-ideas] Re: division of integers should result in fractions not floats

2021-05-14 Thread Chris Angelico
On Sat, May 15, 2021 at 1:39 AM Paul Moore wrote: > > On Fri, 14 May 2021 at 16:29, David Mertz wrote: > > That said, Chris's idea for a literal spelling of "Fraction" is very > > appealing. One extra letter or symbol could indicate that you want to work > > in the Fraction domain rather than f

[Python-ideas] Re: division of integers should result in fractions not floats

2021-05-14 Thread David Mertz
In truth, when I want fractions, I write: from fractions import Fraction as F So a literal doesn't really save many characters anyway. I guess 2 characters during first use. E.g. x = F(1, 3) vs. a possible future: x = 1F / 3 The next stuff comes free either way: y = ((x / 7) + 13) * 11 On F

[Python-ideas] Re: division of integers should result in fractions not floats

2021-05-14 Thread Martin Teichmann
Hi Paul, > Agreed, it is appealing. The problem (and this is not a new > suggestion, it's come up a number of times) is that of having language > syntax depend on non-builtin classes. So either you have to *also* > propose making the fractions module a builtin That is absolutely what I would like

[Python-ideas] Re: division of integers should result in fractions not floats

2021-05-14 Thread Martin Teichmann
Hi David, > A toy example with a half dozen operations won't make huge fractions. A > loop over a million operations will often be a gigantic memory hog. I do not propose to do that, and I agree that it would be stupid. Usually, the fraction gets very quickly converted into a float, and your mil

[Python-ideas] Re: division of integers should result in fractions not floats

2021-05-14 Thread Jonathan Fine
Hi Martin I think it important to realise there are at least two issues. The first is whether sometimes fraction is better than float, and vice versa. The second is whether the default behaviour for int / int should be changed. A third issue is whether some of the benefits you are seeking can be

[Python-ideas] Re: division of integers should result in fractions not floats

2021-05-14 Thread Martin Teichmann
Hi Paul, I would be fine with a new division operator or fraction literal as well, and in the beginning this is what I actually wanted to propose. But then I started prototyping, and given that it is not so easy to change the parser, I just changed the normal division operator. And interestingl

[Python-ideas] Re: division of integers should result in fractions not floats

2021-05-14 Thread Paul Moore
On Fri, 14 May 2021 at 16:29, David Mertz wrote: > > The memory simply blows up too fast for this to be practical (at least as a > default) a float is always 64 bits, a fraction is unboundedly large if > numerator and denominator are coprime. > > A toy example with a half dozen operations won't

[Python-ideas] Re: division of integers should result in fractions not floats

2021-05-14 Thread David Mertz
The memory simply blows up too fast for this to be practical (at least as a default) a float is always 64 bits, a fraction is unboundedly large if numerator and denominator are coprime. A toy example with a half dozen operations won't make huge fractions. A loop over a million operations will ofte

[Python-ideas] Re: division of integers should result in fractions not floats

2021-05-14 Thread Paul Bryan
On Fri, 2021-05-14 at 15:15 +, Martin Teichmann wrote: > So, my argument is, this change will benefit many people, and be of > only a small disadvantage to others. That disadvantage is that yes, > there will be places where it does not work, so libraries need to get > fixed. But this is the ca

[Python-ideas] Re: division of integers should result in fractions not floats

2021-05-14 Thread Chris Angelico
On Sat, May 15, 2021 at 1:16 AM Michael Smith wrote: > > Jonathan Fine said > > But here there is a cost. Arbitrary precision arithmetic (and roots) uses > > more memory and takes longer. > > I think a wider (perhaps naive) interpretation of the idea could be, "can we > defer certain lossy calcu

[Python-ideas] Re: division of integers should result in fractions not floats

2021-05-14 Thread Martin Teichmann
Hi Jonathan, I would agree with you if what I was asking was arbitrary precision math. But this is not what I am asking for, what I am asking for is that integer true division should result in a Fraction. The difference is huge: arbitrary precision math is costly, and while arbitrarily precise,

[Python-ideas] Re: division of integers should result in fractions not floats

2021-05-14 Thread Michael Smith
Jonathan Fine said > But here there is a cost. Arbitrary precision arithmetic (and roots) uses more memory and takes longer. I think a wider (perhaps naive) interpretation of the idea could be, "can we defer certain lossy calculations until they have to be done?" Fraction may do eager arbitrary p

[Python-ideas] Re: division of integers should result in fractions not floats

2021-05-14 Thread Jonathan Fine
Martin wrote: when dividing two integers, the result is a float, which means we > immediately lose precision. This is not good if you want to use code which > supports higher precision. I am of course in favour of keeping precision, if there is no cost. But here there is a cost. Arbitrary precis

[Python-ideas] Re: division of integers should result in fractions not floats

2021-05-14 Thread Paul Bryan
And I've just learned something. 🙂 On Fri, 2021-05-14 at 11:28 -0300, André Roberge wrote: > > > On Fri, May 14, 2021 at 11:23 AM Paul Bryan wrote: > > I like the idea of supporting fractions to maintain more precision > > in calculations. I wonder if this should fall outside the scope of > > a

[Python-ideas] Re: division of integers should result in fractions not floats

2021-05-14 Thread Chris Angelico
On Sat, May 15, 2021 at 12:23 AM Paul Bryan wrote: > > I like the idea of supporting fractions to maintain more precision in > calculations. I wonder if this should fall outside the scope of a standard > library and be implemented in an external library. I'd lean toward inclusion > in stdlib. >

[Python-ideas] Re: division of integers should result in fractions not floats

2021-05-14 Thread André Roberge
On Fri, May 14, 2021 at 11:23 AM Paul Bryan wrote: > I like the idea of supporting fractions to maintain more precision in > calculations. I wonder if this should fall outside the scope of a standard > library and be implemented in an external library. I'd lean toward > inclusion in stdlib. > It

[Python-ideas] Re: division of integers should result in fractions not floats

2021-05-14 Thread Paul Bryan
I like the idea of supporting fractions to maintain more precision in calculations. I wonder if this should fall outside the scope of a standard library and be implemented in an external library. I'd lean toward inclusion in stdlib. A couple of thoughts, which should result in zero breakage of exi

[Python-ideas] Re: division of integers should result in fractions not floats

2021-05-14 Thread Martin Teichmann
Hi Joao, I actually had the same fear as you. But as stated before, I managed to run most of the tests, and even compile and test numpy, with only some 30 tests failing. This means the entire tool chain, setuptools, pytest, cython and much more just worked. No, I did not need to touch one of th

[Python-ideas] Re: division of integers should result in fractions not floats

2021-05-14 Thread Joao S. O. Bueno
On Fri, 14 May 2021 at 09:24, Martin Teichmann wrote: > Hi list, > > when dividing two integers, the result is a float, which means we > immediately lose precision. This is not good if you want to use code which > supports higher precision. Decimals come to mind, but also sympy. This loss > of pr

[Python-ideas] division of integers should result in fractions not floats

2021-05-14 Thread Martin Teichmann
Hi list, when dividing two integers, the result is a float, which means we immediately lose precision. This is not good if you want to use code which supports higher precision. Decimals come to mind, but also sympy. This loss of precision could be avoided if the result of a division is a fracti