negative integer literals

I

Ivan Novick

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
 
R

red floyd

Ivan said:
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.
 
R

red floyd

Ivan said:
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.
 
G

Gianni Mariani

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.
 
I

Ivan Novick

red said:
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
 
J

John Carson

Ivan Novick said:
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.
 
G

Gavin Deane

Gianni said:
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
 
B

benben

Ivan said:
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.

Ben
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

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?
 
R

Ron Natalie

red said:
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.
 
A

Alf P. Steinbach

* Erik Wikström:
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.
 
P

Pete Becker

Ivan said:
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)
 
P

Pete Becker

Erik said:
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)
 
P

Pete Becker

Pete said:
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)
 
S

Steve Pope

benben said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top