473,386 Members | 1,679 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

conversion from unsigned

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
Jul 22 '05 #1
1 1223
Lars Amsel wrote in news:c6************@ID-95365.news.uni-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/
Jul 22 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: frs | last post by:
See example below: Why does the output of 'a' work and the output of 'b' fails to compile? Is there a way to write class 'something' so that 'b' converts correctly by default? (include iostream,...
4
by: Ken Tough | last post by:
Seems like a simple thing to find out, but I'm struggling. I have googled, but everything I find is about implicit conversion, not explicit. Is this implementation-specific, or does ANSI/ISO...
16
by: TTroy | last post by:
Hello, I'm relatively new to C and have gone through more than 4 books on it. None mentioned anything about integral promotion, arithmetic conversion, value preserving and unsigned preserving. ...
14
by: junky_fellow | last post by:
Can anybody please explain this: When a value with integer type is converted to another integer type other than _Bool, if the new type is unsigned, the value is converted by repeatedly...
6
by: NM | last post by:
I am given an int and have to tell whether the bit representation of that int is a palindrome or not. Here is what I have come up with bool isIntPalindrome(int num) { unsigned int temp = num;...
31
by: Martin Jørgensen | last post by:
Hi, I've had a introductory C++ course in the spring and haven't programmed in C++ for a couple of months now (but I have been programmed in C since january). So I decided to do my conversion...
11
by: jyck91 | last post by:
// Base Conversion // Aim: This program is to convert an inputted number // from base M into base N. Display the converted // number in base N. #include <stdio.h> #include <stdlib.h>...
6
by: CFAN | last post by:
Here is a example of double-type-conversion char * __cdecl _itoa ( int val, char *buf, int radix ) { if (radix == 10 && val < 0) xtoa((unsigned long)val, buf, radix, 1); else
7
by: somenath | last post by:
Hi All, I am trying to undestand "Type Conversions" from K&R book.I am not able to understand the bellow mentioned text "Conversion rules are more complicated when unsigned operands are...
21
by: REH | last post by:
It it permissible to use the constructor style cast with primitives such as "unsigned long"? One of my compilers accepts this syntax, the other does not. The failing one chokes on the fact that the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.