Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > conversion from unsigned

Reply
Thread Tools

conversion from unsigned

 
 
Lars Amsel
Guest
Posts: n/a
 
      04-23-2004
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
 
Reply With Quote
 
 
 
 
Rob Williscroft
Guest
Posts: n/a
 
      04-23-2004
Lars Amsel wrote in news:c6bg2g$9l7ne$(E-Mail Removed)-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/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
(int) -> (unsigned) -> (int) or (unsigned) -> (int) -> (unsigned):I'll loose something? pozz C Programming 12 03-20-2011 11:32 PM
what does this double conversion mean (unsigned long)(unsigned int) CFAN C Programming 6 04-04-2007 01:24 PM
unsigned long to unsigned char ashtonn@gmail.com Python 1 06-01-2005 07:00 PM
unsigned int const does not match const unsigned int Timo Freiberger C++ 3 10-30-2004 07:02 PM
Assigning unsigned long to unsigned long long George Marsaglia C Programming 1 07-08-2003 05:16 PM



Advertisments