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


Groups > comp.lang.python > #10013 > unrolled thread

Re: Convert '165.0' to int

Started byLeo Jay <python.leojay@gmail.com>
First post2011-07-21 17:47 +0800
Last post2011-07-25 10:19 +0200
Articles 10 on this page of 50 — 15 participants

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Convert '165.0' to int Leo Jay <python.leojay@gmail.com> - 2011-07-21 17:47 +0800
    Re: Convert '165.0' to int Frank Millman <frank@chagford.com> - 2011-07-21 04:05 -0700
      Re: Convert '165.0' to int Frank Millman <frank@chagford.com> - 2011-07-21 04:23 -0700
    Re: Convert '165.0' to int Web Dreamer <webdreamer@nospam.fr> - 2011-07-21 14:46 +0200
      Re: Convert '165.0' to int Billy Mays <81282ed9a88799d21e77957df2d84bd6514d9af6@myhashismyemail.com> - 2011-07-21 09:27 -0400
        Re: Convert '165.0' to int Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-07-22 04:40 +0200
          Re: Convert '165.0' to int Billy Mays <noway@nohow.com> - 2011-07-22 00:45 -0400
            Re: Convert '165.0' to int Grant Edwards <invalid@invalid.invalid> - 2011-07-22 14:21 +0000
              Re: Convert '165.0' to int Billy Mays <81282ed9a88799d21e77957df2d84bd6514d9af6@myhashismyemail.com> - 2011-07-22 10:49 -0400
                Re: Convert '165.0' to int Grant Edwards <invalid@invalid.invalid> - 2011-07-22 14:58 +0000
                  Re: Convert '165.0' to int Billy Mays <81282ed9a88799d21e77957df2d84bd6514d9af6@myhashismyemail.com> - 2011-07-22 11:06 -0400
                    Re: Convert '165.0' to int Grant Edwards <invalid@invalid.invalid> - 2011-07-22 15:44 +0000
            Re: Convert '165.0' to int Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-07-24 10:03 +0200
        Re: Convert '165.0' to int Web Dreamer <webdreamer@nospam.fr> - 2011-07-25 10:23 +0200
          Re: Convert '165.0' to int Web Dreamer <webdreamer@nospam.fr> - 2011-07-25 11:00 +0200
      Re: Convert '165.0' to int Grant Edwards <invalid@invalid.invalid> - 2011-07-21 14:13 +0000
        Re: Convert '165.0' to int Terry Reedy <tjreedy@udel.edu> - 2011-07-21 16:00 -0400
          Re: Convert '165.0' to int Frank Millman <frank@chagford.com> - 2011-07-21 22:55 -0700
            Re: Convert '165.0' to int Hrvoje Niksic <hniksic@xemacs.org> - 2011-07-22 14:42 +0200
              Re: Convert '165.0' to int rantingrick <rantingrick@gmail.com> - 2011-07-22 12:32 -0700
                Re: Convert '165.0' to int rantingrick <rantingrick@gmail.com> - 2011-07-22 12:34 -0700
                Re: Convert '165.0' to int Chris Angelico <rosuav@gmail.com> - 2011-07-23 06:06 +1000
            Re: Convert '165.0' to int Terry Reedy <tjreedy@udel.edu> - 2011-07-22 15:59 -0400
              Re: Convert '165.0' to int Frank Millman <frank@chagford.com> - 2011-07-22 23:53 -0700
                Re: Convert '165.0' to int Chris Angelico <rosuav@gmail.com> - 2011-07-23 17:42 +1000
                  Re: Convert '165.0' to int Frank Millman <frank@chagford.com> - 2011-07-23 02:03 -0700
                  Re: Convert '165.0' to int Billy Mays <noway@nohow.com> - 2011-07-23 11:12 -0400
                    Re: Convert '165.0' to int Chris Angelico <rosuav@gmail.com> - 2011-07-24 01:20 +1000
                    Re: Convert '165.0' to int Frank Millman <frank@chagford.com> - 2011-07-23 23:53 -0700
                      Re: Convert '165.0' to int Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-07-24 17:34 +1000
                        Re: Convert '165.0' to int Frank Millman <frank@chagford.com> - 2011-07-24 00:58 -0700
                          Re: Convert '165.0' to int Chris Angelico <rosuav@gmail.com> - 2011-07-24 18:07 +1000
                            Re: Convert '165.0' to int Frank Millman <frank@chagford.com> - 2011-07-24 01:21 -0700
                              Re: Convert '165.0' to int Chris Angelico <rosuav@gmail.com> - 2011-07-24 18:28 +1000
                Re: Convert '165.0' to int Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-07-23 18:23 +1000
                  Re: Convert '165.0' to int Frank Millman <frank@chagford.com> - 2011-07-23 02:08 -0700
                Re: Convert '165.0' to int rantingrick <rantingrick@gmail.com> - 2011-07-23 11:28 -0700
                  Re: Convert '165.0' to int Billy Mays <noway@nohow.com> - 2011-07-23 23:53 -0400
                    Re: Convert '165.0' to int Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-07-24 17:21 +1000
                      Re: Convert '165.0' to int Ben Finney <ben+python@benfinney.id.au> - 2011-07-24 17:43 +1000
                  Re: Convert '165.0' to int Frank Millman <frank@chagford.com> - 2011-07-24 01:36 -0700
                    Re: Convert '165.0' to int Ben Finney <ben+python@benfinney.id.au> - 2011-07-24 18:53 +1000
                      Re: Convert '165.0' to int Frank Millman <frank@chagford.com> - 2011-07-24 02:01 -0700
                        Re: Convert '165.0' to int Ben Finney <ben+python@benfinney.id.au> - 2011-07-24 19:25 +1000
                      Re: Convert '165.0' to int Chris Angelico <rosuav@gmail.com> - 2011-07-24 19:42 +1000
                    Re: Convert '165.0' to int Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2011-07-25 12:04 +1200
                      Re: Convert '165.0' to int Frank Millman <frank@chagford.com> - 2011-07-24 22:50 -0700
                        RE: Convert '165.0' to int "Prasad, Ramit" <ramit.prasad@jpmorgan.com> - 2011-08-01 20:42 -0400
                      RE: Convert '165.0' to int "Prasad, Ramit" <ramit.prasad@jpmorgan.com> - 2011-08-01 20:32 -0400
              Re: Convert '165.0' to int Web Dreamer <webdreamer@nospam.fr> - 2011-07-25 10:19 +0200

Page 3 of 3 — ← Prev page 1 2 [3]


#10196

FromFrank Millman <frank@chagford.com>
Date2011-07-24 01:36 -0700
Message-ID<58cdafad-e1ea-4774-984f-aa13cf2a9ef1@fq4g2000vbb.googlegroups.com>
In reply to#10176
On Jul 23, 8:28 pm, rantingrick <rantingr...@gmail.com> wrote:
> On Jul 23, 1:53 am, Frank Millman <fr...@chagford.com> wrote:
>
> >--------------------------------------------------
> > The ideal solution is the one I sketched out earlier - modify python's
> > 'int' function to accept strings such as '165.0'.
> >--------------------------------------------------
>
> NO! You create your OWN casting function for special cases.
>
> PythonZEN: "Special cases aren't special enough to break the rules."

BUT

"Although practicality beats purity".

I know I am flogging a dead horse here, but IMHO, '165', '165.',
'165.0', and '165.00' are all valid string representations of the
integer 165.[1]

Therefore, for practical purposes, it would not be wrong for python's
'int' function to accept these without complaining.

Just for fun, imagine that this had been done from python 1.x. Would
people now be clamouring for this 'wart' to be removed in python 3, or
would they say 'yeah, why not?'.

Frank

[1] Don't ask me why anyone would do this. I am dealing with a third-
party product that does exactly that.

[toc] | [prev] | [next] | [standalone]


#10197

FromBen Finney <ben+python@benfinney.id.au>
Date2011-07-24 18:53 +1000
Message-ID<874o2crrex.fsf@benfinney.id.au>
In reply to#10196
Frank Millman <frank@chagford.com> writes:

> I know I am flogging a dead horse here, but IMHO, '165', '165.',
> '165.0', and '165.00' are all valid string representations of the
> integer 165.[1]

I disagree entirely. Once you introduce a decimal point into the
representation, you're no longer representing an integer.

(They might be the same *number*, but that's not saying the same thing.)

-- 
 \        “I saw a sign: ‘Rest Area 25 Miles’. That's pretty big. Some |
  `\                      people must be really tired.” —Steven Wright |
_o__)                                                                  |
Ben Finney

[toc] | [prev] | [next] | [standalone]


#10199

FromFrank Millman <frank@chagford.com>
Date2011-07-24 02:01 -0700
Message-ID<c361baac-d14d-4fbc-ad36-b4da9e711d04@fv14g2000vbb.googlegroups.com>
In reply to#10197
On Jul 24, 10:53 am, Ben Finney <ben+pyt...@benfinney.id.au> wrote:
> Frank Millman <fr...@chagford.com> writes:
> > I know I am flogging a dead horse here, but IMHO, '165', '165.',
> > '165.0', and '165.00' are all valid string representations of the
> > integer 165.[1]
>
> I disagree entirely. Once you introduce a decimal point into the
> representation, you're no longer representing an integer.
>
> (They might be the same *number*, but that's not saying the same thing.)
>

Fair enough. I never did CS101, so I am looking at this from a
layman's perspective. I am happy to be corrected.

Frank

[toc] | [prev] | [next] | [standalone]


#10200

FromBen Finney <ben+python@benfinney.id.au>
Date2011-07-24 19:25 +1000
Message-ID<87zkk4qbd8.fsf@benfinney.id.au>
In reply to#10199
Frank Millman <frank@chagford.com> writes:

> On Jul 24, 10:53 am, Ben Finney <ben+pyt...@benfinney.id.au> wrote:
> > Frank Millman <fr...@chagford.com> writes:
> > > I know I am flogging a dead horse here, but IMHO, '165', '165.',
> > > '165.0', and '165.00' are all valid string representations of the
> > > integer 165.[1]
> >
> > I disagree entirely. Once you introduce a decimal point into the
> > representation, you're no longer representing an integer.
> >
> > (They might be the same *number*, but that's not saying the same
> > thing.)
>
> Fair enough. I never did CS101

Nor I.

> so I am looking at this from a layman's perspective. I am happy to be
> corrected.

The correction I would ask to apply is: If you're to practice
programming, you can't expect the necessary fuzziness of a layman's
understanding to be sufficient in specifying your program.

You need to know the specifics of what it is you're asking the computer
to do, and what it is you're *not* asking the computer to do, and the
difference between the two.

-- 
 \           “It ain't so much the things we don't know that get us in |
  `\    trouble. It's the things we know that ain't so.” —Artemus Ward |
_o__)                                     (1834–1867), U.S. journalist |
Ben Finney

[toc] | [prev] | [next] | [standalone]


#10202

FromChris Angelico <rosuav@gmail.com>
Date2011-07-24 19:42 +1000
Message-ID<mailman.1418.1311500574.1164.python-list@python.org>
In reply to#10197
On Sun, Jul 24, 2011 at 6:53 PM, Ben Finney <ben+python@benfinney.id.au> wrote:
> Frank Millman <frank@chagford.com> writes:
>
>> I know I am flogging a dead horse here, but IMHO, '165', '165.',
>> '165.0', and '165.00' are all valid string representations of the
>> integer 165.[1]
>
> I disagree entirely. Once you introduce a decimal point into the
> representation, you're no longer representing an integer.
>
> (They might be the same *number*, but that's not saying the same thing.)

What's more, 1.311500322e+12 is a valid representation of a date/time,
according to a ridiculously broken game server I've worked with.
That's the number of milliseconds since 1970, stored as floating
point. I'm sure this makes sense to somebody...

Not all possible representations are equally sane. I'm with Ben;
'165.00' is not a string representation of an integer - but you CAN
take that string, validate it as representing a number that can be
represented as an integer, and evaluate what integer it is.

Chris Angelico

[toc] | [prev] | [next] | [standalone]


#10233

FromGregory Ewing <greg.ewing@canterbury.ac.nz>
Date2011-07-25 12:04 +1200
Message-ID<993q91FebkU2@mid.individual.net>
In reply to#10196
Frank Millman wrote:

> I know I am flogging a dead horse here, but IMHO, '165', '165.',
> '165.0', and '165.00' are all valid string representations of the
> integer 165.[1]
> 
> Therefore, for practical purposes, it would not be wrong for python's
> 'int' function to accept these without complaining.

How far would you go with that? Would you also accept
'1.65e2' as a valid representation of the integer 165?

-- 
Greg

[toc] | [prev] | [next] | [standalone]


#10243

FromFrank Millman <frank@chagford.com>
Date2011-07-24 22:50 -0700
Message-ID<d133a93c-60aa-4160-81d7-493a689682e0@m10g2000yqd.googlegroups.com>
In reply to#10233
On Jul 25, 2:04 am, Gregory Ewing <greg.ew...@canterbury.ac.nz> wrote:
> Frank Millman wrote:
> > I know I am flogging a dead horse here, but IMHO, '165', '165.',
> > '165.0', and '165.00' are all valid string representations of the
> > integer 165.[1]
>
> > Therefore, for practical purposes, it would not be wrong for python's
> > 'int' function to accept these without complaining.
>
> How far would you go with that? Would you also accept
> '1.65e2' as a valid representation of the integer 165?
>

To be honest, I don't even know what that means! I could read up on
it, but I think this has gone far enough. Having flogged this horse to
death, I will now let it rest in peace :-)

Frank

[toc] | [prev] | [next] | [standalone]


#10690

From"Prasad, Ramit" <ramit.prasad@jpmorgan.com>
Date2011-08-01 20:42 -0400
Message-ID<mailman.1742.1312246617.1164.python-list@python.org>
In reply to#10243
-----Original Message-----
From: python-list-bounces+ramit.prasad=jpmchase.com@python.org [mailto:python-list-bounces+ramit.prasad=jpmchase.com@python.org] On Behalf Of Frank Millman
Sent: Monday, July 25, 2011 12:51 AM
To: python-list@python.org
Subject: Re: Convert '165.0' to int

On Jul 25, 2:04 am, Gregory Ewing <greg.ew...@canterbury.ac.nz> wrote:
> Frank Millman wrote:
> > I know I am flogging a dead horse here, but IMHO, '165', '165.',
> > '165.0', and '165.00' are all valid string representations of the
> > integer 165.[1]
>
> > Therefore, for practical purposes, it would not be wrong for python's
> > 'int' function to accept these without complaining.
>
> How far would you go with that? Would you also accept
> '1.65e2' as a valid representation of the integer 165?
>

To be honest, I don't even know what that means! I could read up on
it, but I think this has gone far enough. Having flogged this horse to
death, I will now let it rest in peace :-)

Frank
-- 
http://mail.python.org/mailman/listinfo/python-list


========================

'1.65e2' is a fairly standard representation of scientific notation. It basically translates to: 1.65 x 10^2

For more details, see: http://en.wikipedia.org/wiki/Scientific_notation 

Ramit


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423



This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  

[toc] | [prev] | [next] | [standalone]


#10691

From"Prasad, Ramit" <ramit.prasad@jpmorgan.com>
Date2011-08-01 20:32 -0400
Message-ID<mailman.1743.1312248487.1164.python-list@python.org>
In reply to#10233
-----Original Message-----
From: python-list-bounces+ramit.prasad=jpmchase.com@python.org [mailto:python-list-bounces+ramit.prasad=jpmchase.com@python.org] On Behalf Of Gregory Ewing
Sent: Sunday, July 24, 2011 7:05 PM
To: python-list@python.org
Subject: Re: Convert '165.0' to int

Frank Millman wrote:

> I know I am flogging a dead horse here, but IMHO, '165', '165.',
> '165.0', and '165.00' are all valid string representations of the
> integer 165.[1]
> 
> Therefore, for practical purposes, it would not be wrong for python's
> 'int' function to accept these without complaining.

How far would you go with that? Would you also accept
'1.65e2' as a valid representation of the integer 165?

-- 
Greg
-- 
================================

No, YAGNI (You Aren't Going To Need It). Do the minimum that works for your use case. If you only expect ###.# then code for that. Avoid trying to over-engineer your solution. *If* you think you are going to receive information that might be like '1.65e2' then code for it, otherwise spend your time working on other problems.

Ramit


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423



This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  

[toc] | [prev] | [next] | [standalone]


#10248

FromWeb Dreamer <webdreamer@nospam.fr>
Date2011-07-25 10:19 +0200
Message-ID<4e2d26f8$0$21340$426a74cc@news.free.fr>
In reply to#10147
Terry Reedy a écrit ce vendredi 22 juillet 2011 21:59 dans 
<mailman.1387.1311364775.1164.python-list@python.org> :

>  >>> s='123456789012345678901.0'
>  >>> int(float(s))
> 123456789012345683968
[...]
>  >>> int(s.split('.')[0])
> 123456789012345678901

Indeed, floats do some rounding and the conversion is not precise due to the 
rounding error when conversion to float...
Thanks for pointing this out.

I would consider then doing this:

>>> def strtoint(s):
...     elts = s.split('.')
...     number = int(elts[0])
...     if len(elts)>1:
...         if int(elts[1][0])>=5:
...             number += 1
...     return number

>>> s = '123456789012345678901.987654321'
>>> strtoint(s)
123456789012345678902L
>>> int(round(float(s)))
123456789012345683968L
>>>

Now this is more precise.

But I think we got beyond what The OP requested :-)

-- 
Web Dreamer

[toc] | [prev] | [standalone]


Page 3 of 3 — ← Prev page 1 2 [3]

Back to top | Article view | comp.lang.python


csiph-web