negative integer literals

Discussion in 'C++' started by Ivan Novick, Dec 10, 2006.

  1. Ivan Novick

    Ivan Novick Guest

    Hi,

    Is it possible to have negative integer literal or only positive?

    As far as I understand, the code below would be a positive integer
    literal and the unary negative operator.

    x = -3.2;

    Thanks,
    Ivan
    http://www.0x4849.net
    Ivan Novick, Dec 10, 2006
    #1
    1. Advertising

  2. Ivan Novick

    red floyd Guest

    Ivan Novick wrote:
    > Hi,
    >
    > Is it possible to have negative integer literal or only positive?
    >
    > As far as I understand, the code below would be a positive integer
    > literal and the unary negative operator.
    >
    > x = -3.2;
    >
    > Thanks,
    > Ivan
    > http://www.0x4849.net
    >


    Actually, it's a negative *double* literal.
    red floyd, Dec 10, 2006
    #2
    1. Advertising

  3. Ivan Novick

    red floyd Guest

    Ivan Novick wrote:
    > Hi,
    >
    > Is it possible to have negative integer literal or only positive?
    >
    > As far as I understand, the code below would be a positive integer
    > literal and the unary negative operator.
    >
    > x = -3.2;
    >


    Pardon me. It's not a literal at all. It's an expression (of undefined
    type, since you don't define x).

    -3.2 is a negative double literal.
    red floyd, Dec 10, 2006
    #3
  4. red floyd wrote:
    ....
    >>

    >
    > Pardon me. It's not a literal at all. It's an expression (of undefined
    > type, since you don't define x).
    >
    > -3.2 is a negative double literal.


    I thought -3.2 was a double float literal.
    Gianni Mariani, Dec 10, 2006
    #4
  5. Ivan Novick

    Ivan Novick Guest

    red floyd wrote:
    > Ivan Novick wrote:
    > > Hi,
    > >
    > > Is it possible to have negative integer literal or only positive?
    > >
    > > As far as I understand, the code below would be a positive integer
    > > literal and the unary negative operator.
    > >
    > > x = -3.2;
    > >

    >
    > Pardon me. It's not a literal at all. It's an expression (of undefined
    > type, since you don't define x).
    >
    > -3.2 is a negative double literal.


    Yes, clearly its a double not integer, that was a typo. and obvioussly
    x must be delcared. The point is, is it a negative literal or a
    positive literal combined with a unary negative operator? In the C++
    standard i see no description at all regarding negative literals.

    Ivan
    http://www.0x4849.net
    Ivan Novick, Dec 10, 2006
    #5
  6. Ivan Novick

    John Carson Guest

    "Ivan Novick" <> wrote in message
    news:
    > red floyd wrote:
    >> Ivan Novick wrote:
    >>> Hi,
    >>>
    >>> Is it possible to have negative integer literal or only positive?
    >>>
    >>> As far as I understand, the code below would be a positive integer
    >>> literal and the unary negative operator.
    >>>
    >>> x = -3.2;
    >>>

    >>
    >> Pardon me. It's not a literal at all. It's an expression (of
    >> undefined type, since you don't define x).
    >>
    >> -3.2 is a negative double literal.

    >
    > Yes, clearly its a double not integer, that was a typo. and obvioussly
    > x must be delcared. The point is, is it a negative literal or a
    > positive literal combined with a unary negative operator? In the C++
    > standard i see no description at all regarding negative literals.


    I think you are right. These matters are discussed in section 2.13 of the
    Standard, and I see no reference to the possibility of a negative integer or
    floating literal.

    --
    John Carson
    John Carson, Dec 10, 2006
    #6
  7. Ivan Novick

    Gavin Deane Guest

    Gianni Mariani wrote:
    > I thought -3.2 was a double float literal.


    There's no such thing as a "double float". double and float are
    mutually exclusive.

    Gavin Deane
    Gavin Deane, Dec 10, 2006
    #7
  8. Gavin Deane wrote:
    > Gianni Mariani wrote:
    >> I thought -3.2 was a double float literal.

    >
    > There's no such thing as a "double float". double and float are
    > mutually exclusive.


    double floating point...

    PICKY !
    Gianni Mariani, Dec 10, 2006
    #8
  9. Ivan Novick

    benben Guest

    Ivan Novick wrote:
    > red floyd wrote:
    >> Ivan Novick wrote:
    >>> Hi,
    >>>
    >>> Is it possible to have negative integer literal or only positive?
    >>>
    >>> As far as I understand, the code below would be a positive integer
    >>> literal and the unary negative operator.
    >>>
    >>> x = -3.2;
    >>>

    >> Pardon me. It's not a literal at all. It's an expression (of undefined
    >> type, since you don't define x).
    >>
    >> -3.2 is a negative double literal.

    >
    > Yes, clearly its a double not integer, that was a typo. and obvioussly
    > x must be delcared. The point is, is it a negative literal or a
    > positive literal combined with a unary negative operator? In the C++
    > standard i see no description at all regarding negative literals.


    If you pull the - and 3.2 apart it still compiles fine. They are parsed
    as separate lexical tokens.

    I am confident but not completely sure that compilers treat the
    expression as a negation applied to the positive literal. However, even
    the dumbest compiler will not actually emit code the do that actual
    negation at runtime. So either case the outcome is the same. -3.2 is,
    well, just -3.2, whichever way you take it.

    >
    > Ivan
    > http://www.0x4849.net
    >


    Ben
    benben, Dec 10, 2006
    #9
  10. On 2006-12-10 08:05, Ivan Novick wrote:
    > Hi,
    >
    > Is it possible to have negative integer literal or only positive?
    >
    > As far as I understand, the code below would be a positive integer
    > literal and the unary negative operator.
    >
    > x = -3.2;


    I really hate to ask, but does it matter if it's a positive literal with
    an operator or a negative literal?

    --
    Erik Wikström
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=, Dec 10, 2006
    #10
  11. Ivan Novick

    Ron Natalie Guest

    red floyd wrote:

    >
    > Actually, it's a negative *double* literal.


    Actually, there are no such things as negative literals
    at ALL.

    The OP question (after you omit the integer / real confusion)
    is that YES it is an expression made up of a unary minus and
    an non-signed literal.

    Of course, it is still a "constant expression" according to
    the spec. For integer constant expressions, this pretty
    much requires them to be evaluated at compile time so they
    act like a literal. The only time it gets hairy is in
    the case where the number after the minus sign is 2**nbit-1
    where nbit is the number of bits in an int.
    Ron Natalie, Dec 10, 2006
    #11
  12. * Erik Wikström:
    > On 2006-12-10 08:05, Ivan Novick wrote:
    >> Hi,
    >>
    >> Is it possible to have negative integer literal or only positive?
    >>
    >> As far as I understand, the code below would be a positive integer
    >> literal and the unary negative operator.
    >>
    >> x = -3.2;

    >
    > I really hate to ask, but does it matter if it's a positive literal with
    > an operator or a negative literal?


    It matters for whether the most negative value can be expressed legally
    as a literal.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Dec 10, 2006
    #12
  13. Ivan Novick

    Pete Becker Guest

    Ivan Novick wrote:
    > Hi,
    >
    > Is it possible to have negative integer literal or only positive?
    >
    > As far as I understand, the code below would be a positive integer
    > literal and the unary negative operator.
    >


    Right: literals are non-negative values, and a leading minus sign is a
    separate token. Off the top of my head, I don't see that there's a
    significant difference from having a single token that includes the
    leading minus sign.

    --

    -- Pete
    Roundhouse Consulting, Ltd. (www.versatilecoding.com)
    Author of "The Standard C++ Library Extensions: a Tutorial and
    Reference." (www.petebecker.com/tr1book)
    Pete Becker, Dec 10, 2006
    #13
  14. Ivan Novick

    Pete Becker Guest

    Erik Wikström wrote:
    >
    > I really hate to ask, but does it matter if it's a positive literal with
    > an operator or a negative literal?
    >


    It matters when numeric types have an asymmetrical range. The most
    common place where some of us run into it is:

    #define INT_MAX 32767
    #define INT_MIN -32768

    The problem here is that the type of INT_MIN is not int, but long. (That
    example is for a 16-bit architecture, but the same problem arises for
    all sizes of asymmetric types).

    --

    -- Pete
    Roundhouse Consulting, Ltd. (www.versatilecoding.com)
    Author of "The Standard C++ Library Extensions: a Tutorial and
    Reference." (www.petebecker.com/tr1book)
    Pete Becker, Dec 10, 2006
    #14
  15. Ivan Novick

    Pete Becker Guest

    Pete Becker wrote:
    > Ivan Novick wrote:
    >> Hi,
    >>
    >> Is it possible to have negative integer literal or only positive?
    >>
    >> As far as I understand, the code below would be a positive integer
    >> literal and the unary negative operator.
    >>

    >
    > Right: literals are non-negative values, and a leading minus sign is a
    > separate token. Off the top of my head, I don't see that there's a
    > significant difference from having a single token that includes the
    > leading minus sign.
    >


    Okay, I also posted an example where it matters. But that's an odd
    corner, occupied by standard library writers.

    --

    -- Pete
    Roundhouse Consulting, Ltd. (www.versatilecoding.com)
    Author of "The Standard C++ Library Extensions: a Tutorial and
    Reference." (www.petebecker.com/tr1book)
    Pete Becker, Dec 10, 2006
    #15
  16. Ivan Novick

    Steve Pope Guest

    benben <benhonghatgmaildotcom@nospam> wrote:

    >I am confident but not completely sure that compilers treat the
    >expression as a negation applied to the positive literal. However, even
    >the dumbest compiler will not actually emit code the do that actual
    >negation at runtime.


    We had a discussion on "reducing constant expressions at runtime"
    awhile back. Not surprisingly, there was not total agreement
    on the above point.

    Steve
    Steve Pope, Dec 10, 2006
    #16
    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. prem_eda
    Replies:
    5
    Views:
    7,852
    Pieter Hulshoff
    Oct 11, 2004
  2. Purush
    Replies:
    4
    Views:
    1,649
    Purush Rudrakshala
    Apr 13, 2005
  3. John Goche
    Replies:
    8
    Views:
    16,456
  4. Bart Samwel
    Replies:
    14
    Views:
    808
    Bart Samwel
    Apr 22, 2005
  5. Integer Literals

    , Oct 18, 2006, in forum: C++
    Replies:
    5
    Views:
    442
Loading...

Share This Page