Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #90149

Re: Bitten by my C/Java experience

References <87r3qwid3u.fsf@Equus.decebal.nl> <5547ad48$0$2864$e4fe514c@news.xs4all.nl> <mailman.96.1430761253.12865.python-list@python.org> <554c945c$0$2952$e4fe514c@dreader35.news.xs4all.nl>
Date 2015-05-08 21:32 +1000
Subject Re: Bitten by my C/Java experience
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.236.1431084730.12865.python-list@python.org> (permalink)

Show all headers | View raw


On Fri, May 8, 2015 at 8:47 PM, Albert van der Horst
<albert@spenarnc.xs4all.nl> wrote:
> In article <mailman.96.1430761253.12865.python-list@python.org>,
> Chris Angelico  <rosuav@gmail.com> wrote:
>>On Tue, May 5, 2015 at 3:32 AM, Irmen de Jong <irmen.NOSPAM@xs4all.nl> wrote:
>>> That is a broad question, but one thing that comes to mind is the
>>current (python 3)
>>> behavior of integer division. It gives the exact result and doesn't
>>truncate to integers:
>>>
>>>
>>>>>> 5/4
>>> 1.25
>>
>>Using the word "exact" around non-integer values can be a little
>>ambiguous, since floats are often inexact. But yes, int/int -> float,
>>and yes, it WILL bite C programmers.
>
> This should not be presented as a somewhat arbitrary decision.

I didn't, but my main point was that "exact" is a poor choice of word.

> Formerly we had
>
> 3e0/4e0
> 0.75
>
> and
>
> 3/4
> 0
>
> So the / operator worked on reals giving reals and integers
> giving integers. Great if you're used to it, but sometimes a pitfall.
> Also in practice it sometimes leads to rounding in unexpected places.
> The greatest disadvantage is when you have i and j and want their
> ratio. You have to do something like (real)i/j which feels unnatural.
> So we need two different division operators on the integers.
>
> Solution:
> introduce // for integer by integer given integer, giving the
> quotient (with a possible remainder).
> Now / is free to be used for the more rational  "i/j gives a ratio",
> i.e. a real number.

Where's that "i.e." come from? Why shouldn't i/j yield a Fraction, for
instance? That would be every bit as logical, and would mean we
effectively get "fraction literals" in their most obvious and natural
form. I can accept that int/int should yield something capable of
storing non-integral values, but I don't see that it absolutely has to
be IEEE floating point. However, that point has been much argued, and
it's definitely not changing now.

> Bottom line
> 3e0/4e0 and 3/4 gives the same result. It is nice to no longer
> have to be very careful in floating point calculation to avoid
> integer constants.
>
> On the other hand integer division is still available to solve
> the familiar egg-farm problems:
>
> I have 103 eggs. 12 eggs go in a box. How many boxes can I fill?

Of course they do... but you still actually have a difference. Compare:

>>> (1<<1000)//(1<<10)
10463951242053391806136963369726580181263718864311851635192874886429209483641954321222640418122029864527291727710479949464718215680589004332016189037791576956967351342601788071700268169006221818240189631008834448226154239518944108944497601509840881752510934060240763835605888507473266002770708660224

>>> (1<<1000)//(2.0**10)
1.0463951242053392e+298

>>> (1<<2000)//(2.0**10)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OverflowError: int too large to convert to float

So you do still need to worry about whether, with integer division,
you're working with integers or floats. Lots of programmers wish they
could just ignore all these problems and they'll go away, but floats
have inexactitude and range limits, where ints have neither (and
Fraction and Decimal values have their own notables), and ultimately,
you're going to have to know what data type your numbers are. That's
why I think it'd be better to have int/int -> Fraction; you might have
shocking performance, but then you can simply tell people to use
float() to speed up calculations at the expense of precision. At least
operations between int and Fraction are guaranteed to maintain
precision.

ChrisA

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Bitten by my C/Java experience Cecil Westerhof <Cecil@decebal.nl> - 2015-05-04 17:20 +0200
  Re: Bitten by my C/Java experience Tobiah <toby@tobiah.org> - 2015-05-04 10:18 -0700
  Re: Bitten by my C/Java experience Irmen de Jong <irmen.NOSPAM@xs4all.nl> - 2015-05-04 19:32 +0200
    Re: Bitten by my C/Java experience Chris Angelico <rosuav@gmail.com> - 2015-05-05 03:40 +1000
      Re: Bitten by my C/Java experience albert@spenarnc.xs4all.nl (Albert van der Horst) - 2015-05-08 10:47 +0000
        Re: Bitten by my C/Java experience Chris Angelico <rosuav@gmail.com> - 2015-05-08 21:32 +1000
  Re: Bitten by my C/Java experience Ian Kelly <ian.g.kelly@gmail.com> - 2015-05-04 11:43 -0600
    Re: Bitten by my C/Java experience Andrew Cooper <amc96@cam.ac.uk> - 2015-05-04 21:57 +0100
      Re: Bitten by my C/Java experience random832@fastmail.us - 2015-05-04 17:16 -0400
      Re: Bitten by my C/Java experience Tim Chase <python.list@tim.thechases.com> - 2015-05-04 16:26 -0500
  Re: Bitten by my C/Java experience Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-05-04 18:59 +0100
  Re: Bitten by my C/Java experience Ian Kelly <ian.g.kelly@gmail.com> - 2015-05-04 13:39 -0600
    Re: Bitten by my C/Java experience Cecil Westerhof <Cecil@decebal.nl> - 2015-05-04 22:28 +0200
      Re: Bitten by my C/Java experience Dave Angel <davea@davea.name> - 2015-05-04 19:17 -0400
  Re: Bitten by my C/Java experience Terry Reedy <tjreedy@udel.edu> - 2015-05-04 16:06 -0400
  Re: Bitten by my C/Java experience BartC <bc@freeuk.com> - 2015-05-04 23:02 +0100
    Re: Bitten by my C/Java experience Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-05-05 18:19 +1000
      Re: Bitten by my C/Java experience BartC <bc@freeuk.com> - 2015-05-05 14:20 +0100
        Re: Bitten by my C/Java experience Rustom Mody <rustompmody@gmail.com> - 2015-05-05 09:24 -0700
          Re: Bitten by my C/Java experience Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2015-05-06 14:38 +0200
            Re: Bitten by my C/Java experience Rustom Mody <rustompmody@gmail.com> - 2015-05-06 06:03 -0700
        Re: Bitten by my C/Java experience Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2015-05-06 23:19 +1200
          Re: Bitten by my C/Java experience BartC <bc@freeuk.com> - 2015-05-06 13:40 +0100
            Re: Bitten by my C/Java experience Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-05-07 02:03 +1000
              Re: Bitten by my C/Java experience Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-05-06 21:13 +0100
      Re: Bitten by my C/Java experience Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2015-05-06 23:15 +1200
    Re: Bitten by my C/Java experience random832@fastmail.us - 2015-05-06 09:11 -0400
    Re: Bitten by my C/Java experience Chris Angelico <rosuav@gmail.com> - 2015-05-06 23:16 +1000

csiph-web