Is unsigned float legal?

M

Memana

Is it legal to use "unsigned float"?
I think floating point numbers always need sign.

When i Checked with VC++ 6.0, i got following warning message only.
warning C4076: 'unsigned' : can not be used with type 'float'

Other doubt is that,
decalre an unsgined int as below:
unsigned nNumber = 0;
Is there something wrong in doing like this.

TIA,
Meman A
 
J

Jack Klein

Is it legal to use "unsigned float"?

No, there is no such type.
I think floating point numbers always need sign.

It has nothing to do with whether or not floating point numbers "need"
a sign, it has to do with the arithmetic types defined by C++.
When i Checked with VC++ 6.0, i got following warning message only.
warning C4076: 'unsigned' : can not be used with type 'float'

The keyword 'unsigned' is not a modifier. Except for char, each
integer type in C++ (short, int, long) comes in two varieties, signed
and unsigned. For historical reasons there are three types of char,
signed, unsigned, and "plain". Each of these is a distinct and
different type. You can't pass a pointer to unsigned (char, short,
int, long) to a function declared to accept a pointer to signed (char,
short, int, long).
Other doubt is that,
decalre an unsgined int as below:
unsigned nNumber = 0;
Is there something wrong in doing like this.

No, there is nothing wrong with doing this. There are certain places
in C++ syntax where the keyword 'int' may be omitted and is inferred
by the compiler from context. Also with integer types other than
char, the keyword 'signed' may be omitted as it is always implied by
the absence of the keyword 'unsigned'.

There are two varieties of the "short" data type, their official names
are "short int" and "unsigned short int". But the following shortened
notations are allowed:

- "short" is equivalent to "signed short int"
- "signed short" is equivalent to "signed short int"
- "short int" is equivalent to "signed short int"
- "unsigned short" is equivalent to "unsigned short int"

There are two varieties of the "int" data type, "int" and "unsigned
int":

- "int" is equivalent to "signed int"
- "unsigned" (without 'char', 'short', or 'long') is equivalent to
"unsigned int"

There are two varieties of the "long" data type, "long int" and
"unsigned long int". The following shortened notions are allowed:

- "long" is equivalent to "signed long int"
- "signed long" is equivalent to "signed long int"
- "long int" is equivalent to "signed long int"
- "unsigned long" is equivalent to "unsigned long int"

Note there are no shortened notations available for the three
character types. They must be spelled out in full, "char", "signed
char", "unsigned char".

So "unsigned" by itself means "unsigned int". But "signed" by itself
is an error and means nothing.
 
I

Ioannis Vranos

Memana said:
Is it legal to use "unsigned float"?
I think floating point numbers always need sign.

When i Checked with VC++ 6.0, i got following warning message only.
warning C4076: 'unsigned' : can not be used with type 'float'



The floating point types are float, double and long double.


Other doubt is that,
decalre an unsgined int as below:
unsigned nNumber = 0;
Is there something wrong in doing like this.



This is ok.






Regards,

Ioannis Vranos
 
R

Robbie Hatley

Memana said:
Is it legal to use "unsigned float"?

There's no such thing.
I think floating point numbers always need sign.

They're always signed, yes.
When i Checked with VC++ 6.0, i got following warning message only.
warning C4076: 'unsigned' : can not be used with type 'float'

Yes, because there's no such thing as "unsigned float".
You can alway drop the sign by using fabs().
unsigned nNumber = 0;
Is there something wrong in doing like this?

That is the same as:
unsigned int nNumber = 0;
There's nothing wrong with it... if an unsigned int
is what you want.


--
Cheers,
Robbie Hatley
Tustin, CA, USA
email: lonewolfintj at pacbell dot net
web: home dot pacbell dot net slant earnur slant
 
A

Alf P. Steinbach

* Jack Klein:
So "unsigned" by itself means "unsigned int". But "signed" by itself
is an error and means nothing.

The Holy Standard decrees, in table 7 in §7.1.5.2, that "signed" by
itself is a synonym for "int".

More general rule:

"int" is implied when you do not explicitly specify "char" for an
integer type. For example, "long" means "long int". Of course
it couldn't be anything else since "long char" isn't allowed... :)
 
J

JKop

Memana posted:
Is it legal to use "unsigned float"?
I think floating point numbers always need sign.

When i Checked with VC++ 6.0, i got following warning message only.
warning C4076: 'unsigned' : can not be used with type 'float'

Other doubt is that,
decalre an unsgined int as below:
unsigned nNumber = 0;
Is there something wrong in doing like this.

TIA,
Meman A


UNTEST CODE


class unsigned_float
{
private:

float data;

public:

unsigned_float& operator=(float in)
{
data = ( (in < 0) ? -in : in );

//Or maybe put in some fancy overflow stuff here.
}

//Maybe consider other operators

operator float()
{
return data;
}

};


#include <iostream>


int main()
{
unsigned_float blah;

blah = 54.242;

std::cout << blah << std::endl;

blah = -343.34

std::cout << blah;
}
 
J

Jack Klein

* Jack Klein:

The Holy Standard decrees, in table 7 in §7.1.5.2, that "signed" by
itself is a synonym for "int".

More general rule:

"int" is implied when you do not explicitly specify "char" for an
integer type. For example, "long" means "long int". Of course
it couldn't be anything else since "long char" isn't allowed... :)

Thanks, I stand corrected. I always thought it wasn't there, but it
is in the grammar. In the C standard too, 6.7.2 para 1 of C99.
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top