# negative integer literals

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

1. ### Ivan NovickGuest

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

2. ### red floydGuest

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

3. ### red floydGuest

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
4. ### Gianni MarianiGuest

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
5. ### Ivan NovickGuest

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
6. ### John CarsonGuest

"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
7. ### Gavin DeaneGuest

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
8. ### Gianni MarianiGuest

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
9. ### benbenGuest

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
10. ### =?ISO-8859-1?Q?Erik_Wikstr=F6m?=Guest

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
11. ### Ron NatalieGuest

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
12. ### Alf P. SteinbachGuest

* 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
13. ### Pete BeckerGuest

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

--

-- 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. ### Pete BeckerGuest

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
15. ### Pete BeckerGuest

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
>

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
16. ### Steve PopeGuest

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