Exponential Notation and integers

Discussion in 'Python' started by Timothy Fitz, Nov 18, 2004.

  1. Timothy Fitz

    Timothy Fitz Guest

    Why are all numberical literals in exponential notation floats? To me
    it is counter-intuitive for 1e3 to behave so fundamentally different
    from 1000.

    Timothy Fitz
    Timothy Fitz, Nov 18, 2004
    #1
    1. Advertising

  2. Timothy Fitz wrote:

    > Why are all numberical literals in exponential notation floats? To me
    > it is counter-intuitive for 1e3 to behave so fundamentally different
    > from 1000.


    Probably primarily by convention. Most language use computerized
    scientific notation to represent floating point numbers, not integers.
    It makes sense, after all: should 1.344e3 be an int, but 1.344e2 be a
    float?

    --
    Erik Max Francis && && http://www.alcyone.com/max/
    San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
    I'll have to get over this sooner or later. Why not now?
    -- Louis Wu
    Erik Max Francis, Nov 18, 2004
    #2
    1. Advertising

  3. Timothy Fitz

    Peter Hansen Guest

    Erik Max Francis wrote:
    > Timothy Fitz wrote:
    >> Why are all numberical literals in exponential notation floats? To me
    >> it is counter-intuitive for 1e3 to behave so fundamentally different
    >> from 1000.

    >
    > Probably primarily by convention. Most language use computerized
    > scientific notation to represent floating point numbers, not integers.
    > It makes sense, after all: should 1.344e3 be an int, but 1.344e2 be a
    > float?


    No, naturally 1.344e2 is the integer 134. <wink>


    Maybe something like this would help the OP:

    >>> def number(x):

    .... return int(float(x))
    ....
    >>> MYCONSTANT = number('1.344e3')
    >>> print MYCONSTANT

    1344

    -Peter
    Peter Hansen, Nov 19, 2004
    #3
  4. Timothy Fitz

    Timothy Fitz Guest

    My comment is more of a linguistic complaint. The differences between
    an integer (type int or long) and a float in practice are large,
    shouldn't 1e100 be of an integer type, since it -is- an integer? The
    default practice here of making it a float just seems wrong.

    [Peter Hansen]
    > Maybe something like this would help the OP:
    >
    > >>> def number(x):

    > ... return int(float(x))
    > ...
    > >>> MYCONSTANT = number('1.344e3')
    > >>> print MYCONSTANT

    > 1344


    Yes, this would work in some cases, but not where it matters. Floats
    have a significant amount of error when you are talking about very
    large numbers, which is the only time you would want integer
    exponential literals anyway, examples of errors:

    >>> long(1e100)

    10000000000000000159028911097599180468360808563945281389781327557747838772170381060813469985856815104L
    >>> 1e20 == 10 ** 20 + 8192

    True
    >>> long(1e23) == 10 ** 23

    False

    The worst part is that because python will implicitly convert between
    floats and integers, this can all happen silently and come back and
    bite you where you least expect it.
    Timothy Fitz, Nov 19, 2004
    #4
  5. Em Qui, 2004-11-18 às 15:49 -0800, Erik Max Francis escreveu:
    > Timothy Fitz wrote:
    >
    > > Why are all numberical literals in exponential notation floats? To me
    > > it is counter-intuitive for 1e3 to behave so fundamentally different
    > > from 1000.

    >
    > Probably primarily by convention. Most language use computerized
    > scientific notation to represent floating point numbers, not integers.
    > It makes sense, after all: should 1.344e3 be an int, but 1.344e2 be a
    > float?
    >

    As far as readbility is concerned, some languages provide the 1_000_000
    syntax (1000000 is just too hard to read). Is there any plans to add
    this to python?

    Thanks,
    Fabio

    > --
    > Erik Max Francis && && http://www.alcyone.com/max/
    > San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
    > I'll have to get over this sooner or later. Why not now?
    > -- Louis Wu
    =?ISO-8859-1?Q?F=E1bio?= Mendes, Nov 19, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Grey Squirrel

    Hungarian Notation Vs. Pascal Notation?

    Grey Squirrel, Mar 19, 2007, in forum: ASP .Net
    Replies:
    6
    Views:
    1,300
    Steve C. Orr [MCSD, MVP, CSM, ASP Insider]
    Mar 21, 2007
  2. Tameem
    Replies:
    454
    Views:
    11,866
  3. Replies:
    4
    Views:
    208
  4. Robert Mark Bram

    Dot notation V Bracket notation

    Robert Mark Bram, Jul 4, 2003, in forum: Javascript
    Replies:
    3
    Views:
    467
    Robert Mark Bram
    Jul 5, 2003
  5. J.Sperlhofer
    Replies:
    9
    Views:
    289
    Dr John Stockton
    Apr 28, 2005
Loading...

Share This Page