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


Groups > comp.lang.python > #94355

Re: Integers with leading zeroes

From Ben Finney <ben+python@benfinney.id.au>
Subject Re: Integers with leading zeroes
Date 2015-07-22 18:40 +1000
References (1 earlier) <mailman.823.1437499340.3674.python-list@python.org> <55aeea13$0$1669$c3e8da3$5496439d@news.astraweb.com> <mailman.845.1437527447.3674.python-list@python.org> <55aefc70$0$1656$c3e8da3$5496439d@news.astraweb.com> <201507220712.t6M7CxJO014614@fido.openend.se>
Newsgroups comp.lang.python
Message-ID <mailman.860.1437554451.3674.python-list@python.org> (permalink)

Show all headers | View raw


Laura Creighton <lac@openend.se> writes:

> The biggest use I have for decimal numbers that begin with 0 is in
> credit card numbers, account numbers and the like where the first
> check you do is 'does this thing have the correct number of digits'.

The following are examples of types from the real world that people
think of, and casually discuss, as “numbers”.

* Postal code
* Credit card number
* Telephone number
* Car registration plate number
* Personal Identification Number (PIN)

You can no doubt come up with other examples.

Despite that they are represented in text with digits, and the authority
that generates them may even use some sequence of integers, the types
should not be treated as numbers.

They are not quantities. Performing arithmetic on them (adding two
together, dividing by two, etc.) makes no sense. Representing them in a
different number base (e.g. hexadecimal) utterly changes their meaning.

And, as you've found, a leading zero makes a difference to the value. To
a computer (ignoring the legacy of octal representation), that makes
nonsense of the idea these are numbers.

So these types are not numbers and should not be treated that way in the
program.

These are, from the point of view of your computer program, opaque
tokens. They should be encoded not using a number type, but a type which
represents them as only a string of decimal digits.

Best if they're stored without spaces or punctuation; convert to and
from some format for human use if you must, but only at the input/output
borders of the program.

> So far, all the examples I've been able to find in my code -- which
> does this sort of stuff a lot -- is looking at string versions of
> decimal numbers, but I suspect there is old code out there in the
> wild which just used integers.

Maybe so, but I consider the latter kind of code to be a terrible
practice since it leads to assumptions in the code which are unfounded,
and which no human would ever make. Treat them the way a human actually
would: as an opaque tokem, a text (sequence of characters) that happen
to contain digits.

-- 
 \            “Simplicity is prerequisite for reliability.” —Edsger W. |
  `\                                                          Dijkstra |
_o__)                                                                  |
Ben Finney

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


Thread

Integers with leading zeroes Steven D'Aprano <steve@pearwood.info> - 2015-07-19 15:39 +1000
  Re: Integers with leading zeroes Ian Kelly <ian.g.kelly@gmail.com> - 2015-07-18 23:46 -0600
  Re: Integers with leading zeroes "Skybuck Flying" <skybuck2000@hotmail.com> - 2015-07-19 10:19 +0200
    Re: Integers with leading zeroes Rick Johnson <rantingrickjohnson@gmail.com> - 2015-07-19 12:58 -0700
  Re: Integers with leading zeroes Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2015-07-21 19:21 +0200
    Re: Integers with leading zeroes sohcahtoa82@gmail.com - 2015-07-21 10:58 -0700
      Re: Integers with leading zeroes Emile van Sebille <emile@fenx.com> - 2015-07-21 11:06 -0700
        Re: Integers with leading zeroes sohcahtoa82@gmail.com - 2015-07-21 11:38 -0700
          Re: Integers with leading zeroes sohcahtoa82@gmail.com - 2015-07-21 11:41 -0700
    Re: Integers with leading zeroes Steven D'Aprano <steve@pearwood.info> - 2015-07-22 10:55 +1000
      Re: Integers with leading zeroes Chris Angelico <rosuav@gmail.com> - 2015-07-22 11:10 +1000
        Re: Integers with leading zeroes Steven D'Aprano <steve@pearwood.info> - 2015-07-22 12:14 +1000
          Re: Integers with leading zeroes Chris Angelico <rosuav@gmail.com> - 2015-07-22 14:16 +1000
          Re: Integers with leading zeroes Laura Creighton <lac@openend.se> - 2015-07-22 09:12 +0200
            Re: Integers with leading zeroes alister <alister.nospam.ware@ntlworld.com> - 2015-07-22 09:09 +0000
              Re: Integers with leading zeroes Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2015-07-22 17:27 +0200
                Re: Integers with leading zeroes Grant Edwards <invalid@invalid.invalid> - 2015-07-22 15:47 +0000
              Re: Integers with leading zeroes MRAB <python@mrabarnett.plus.com> - 2015-07-22 16:38 +0100
                Re: Integers with leading zeroes Grant Edwards <invalid@invalid.invalid> - 2015-07-22 15:50 +0000
                Re: Integers with leading zeroes MRAB <python@mrabarnett.plus.com> - 2015-07-22 17:24 +0100
              Re: Integers with leading zeroes Chris Angelico <rosuav@gmail.com> - 2015-07-23 08:32 +1000
          Re: Integers with leading zeroes Laura Creighton <lac@openend.se> - 2015-07-22 09:31 +0200
          Re: Integers with leading zeroes Ben Finney <ben+python@benfinney.id.au> - 2015-07-22 18:40 +1000
            Re: Integers with leading zeroes Marko Rauhamaa <marko@pacujo.net> - 2015-07-22 12:10 +0300
              Re: Integers with leading zeroes Laura Creighton <lac@openend.se> - 2015-07-22 11:38 +0200
                Re: Integers with leading zeroes Marko Rauhamaa <marko@pacujo.net> - 2015-07-22 13:26 +0300
            Re: Integers with leading zeroes Grant Edwards <invalid@invalid.invalid> - 2015-07-22 13:51 +0000
              Re: Integers with leading zeroes Rustom Mody <rustompmody@gmail.com> - 2015-07-22 07:03 -0700
              Re: Integers with leading zeroes Steven D'Aprano <steve@pearwood.info> - 2015-07-23 01:14 +1000
                Re: Integers with leading zeroes Grant Edwards <invalid@invalid.invalid> - 2015-07-22 15:20 +0000
              Re: Integers with leading zeroes Michael Torrie <torriem@gmail.com> - 2015-07-22 20:11 -0600
                Re: Integers with leading zeroes Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2015-07-24 18:23 +1200
                Re: Integers with leading zeroes Chris Angelico <rosuav@gmail.com> - 2015-07-24 16:28 +1000
                Re: Integers with leading zeroes alister <alister.nospam.ware@ntlworld.com> - 2015-07-24 13:16 +0000

csiph-web