conversion from unsigned

Discussion in 'C++' started by Lars Amsel, Apr 23, 2004.

  1. Lars Amsel

    Lars Amsel Guest

    Hi,

    I am a bit confused about some C++ code

    unsigned l = 1;
    cout << -l << " " << pow(2., -l-1.) << endl;

    I expected

    $>-1 0.25

    I got

    $>4294967295 inf

    OK. It's clear that their is a underflow in the unsigned l. I expected a
    auto conversion by the compiler to int. When I change the code to

    unsigned preL = 1;
    int l = -preL;
    cout << -l << " " << pow(2., -l-1.) << endl;

    everything works fine. Is that well defined c++ behaviour or is it a bug
    in g++ (version 3.3.x)?

    Cheers

    Lars
    Lars Amsel, Apr 23, 2004
    #1
    1. Advertising

  2. Lars Amsel wrote in news:c6bg2g$9l7ne$-berlin.de in
    comp.lang.c++:

    > Hi,
    >
    > I am a bit confused about some C++ code
    >
    > unsigned l = 1;
    > cout << -l << " " << pow(2., -l-1.) << endl;
    >
    > I expected
    >
    > $>-1 0.25
    >


    Basic arithmatic in C++, dosen't do type conversion's so
    -(ubsigned)1, has type unsigned, if nessacery the compiler
    will "widen" types *bofore* doing the arithmatic:

    long l = 1; int i = 2;

    ( l + i ) will have type long and before the addition i will
    be "widend" from int to long.

    unsigned int is "wider" than int, there is no real logic to it
    but one has to be wider than the other, else we wouldn't have
    a clue whats going on.

    > I got
    >
    > $>4294967295 inf


    (unsigned)-1 is required to be UINT_MAX (4294967295U on your
    platform).

    >
    > OK. It's clear that their is a underflow in the unsigned l.


    Nope unsigned uses modulo arithmatic, values wrap around from
    0 to UINT_MAX ( math is done modulo (UINT_MAX + 1) ).

    > I expected a
    > auto conversion by the compiler to int. When I change the code to
    >
    > unsigned preL = 1;
    > int l = -preL;
    > cout << -l << " " << pow(2., -l-1.) << endl;
    >
    > everything works fine. Is that well defined c++ behaviour or is
    > it a bug in g++ (version 3.3.x)?
    >


    Its well defined (other than the actual value 4294967295 (UINT_MAX)
    is implemetation defined).

    Rob.
    --
    http://www.victim-prime.dsl.pipex.com/
    Rob Williscroft, Apr 24, 2004
    #2
    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. Timo Freiberger
    Replies:
    3
    Views:
    941
    Bob Hairgrove
    Oct 30, 2004
  2. George Marsaglia

    Assigning unsigned long to unsigned long long

    George Marsaglia, Jul 8, 2003, in forum: C Programming
    Replies:
    1
    Views:
    672
    Eric Sosman
    Jul 8, 2003
  3. Replies:
    1
    Views:
    450
    Diez B. Roggisch
    Jun 1, 2005
  4. CFAN
    Replies:
    6
    Views:
    818
    Tor Rustad
    Apr 4, 2007
  5. pozz
    Replies:
    12
    Views:
    733
    Tim Rentsch
    Mar 20, 2011
Loading...

Share This Page