Is unsigned float legal?

Discussion in 'C++' started by Memana, Jul 4, 2004.

  1. Memana

    Memana Guest

    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
     
    Memana, Jul 4, 2004
    #1
    1. Advertising

  2. Memana

    Jack Klein Guest

    On 3 Jul 2004 20:32:12 -0700, (Memana) wrote in
    comp.lang.c++:

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

    > TIA,
    > Meman A


    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, Jul 4, 2004
    #2
    1. Advertising

  3. Memana wrote:
    > 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
     
    Ioannis Vranos, Jul 4, 2004
    #3
  4. "Memana" <> wrote:

    > 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
     
    Robbie Hatley, Jul 4, 2004
    #4
  5. * 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... :)

    --
    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, Jul 4, 2004
    #5
  6. Memana

    JKop Guest

    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;
    }
     
    JKop, Jul 4, 2004
    #6
  7. Memana

    Jack Klein Guest

    On Sun, 04 Jul 2004 07:27:16 GMT, (Alf P. Steinbach)
    wrote in comp.lang.c++:

    > * 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... :)


    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.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, Jul 4, 2004
    #7
    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. bd
    Replies:
    0
    Views:
    635
  2. John

    should 'float* foo = 12345' be legal?

    John, Nov 17, 2006, in forum: C Programming
    Replies:
    4
    Views:
    380
    Keith Thompson
    Nov 17, 2006
  3. Carsten Fuchs
    Replies:
    45
    Views:
    1,561
    James Kanze
    Oct 8, 2009
  4. pozz
    Replies:
    12
    Views:
    746
    Tim Rentsch
    Mar 20, 2011
  5. Dipesh Batheja
    Replies:
    1
    Views:
    579
    Bernard Kenik
    Nov 16, 2006
Loading...

Share This Page