RE: [Dev-C++] bit extraction problem
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Ioannis V. <iv...@em...> - 2003-08-18 00:17:10
|
One "analog" approach that I can think is to convert the numeric_limits<long>::max() and min() to strings and count the digits. Then compare the number of digits of the input with the number of digits of max or min depending if the input has a minus sign. If the number if digits is equal with the equivalent limit, compare if the first digit is larger/smaller of the corresponding limit (depending on the minus sign). And so on. Ioannis Vranos * Programming pages: http://www.noicys.freeurl.com * Alternative URL 1: http://run.to/noicys * Alternative URL 2: http://www.noicys.cjb.net > -----Original Message----- > From: Per Westermark [mailto:pw...@ia...] > Sent: Sunday, August 17, 2003 4:19 PM > To: Ioannis Vranos > Cc: 'Dev C++' > Subject: RE: [Dev-C++] bit extraction problem > > > One alternative is to count the number of digits, and take the log. > > One decimal digit consumes 1/log(2) binary digits, > or approx 3.322 bits. However, it is still only possible to > get close to > the true limit - since the log of a number needs at least as many > value-digits as the number for an exact result :-) > > Another approach - which is exact - is to perform the decimal->binary > conversion in own code, and for each added decimal digit check if the > binary number overflows. For a positive number decimal input > number, the > resulting binary should increase for each iteration. If the > input number > is negative, it is simple to check for overflow by negating the result > twice and check that the original value was returned. > > For a traditional machine with two-complement handling, a > negation at the > end will not overflow. However, I'm not sure if that is true for all > strange machines out there, that uses other ways of encoding negative > numbers. Maybe it is possible to negate a number twice and check if > original value is returned. Anyway, right now, 2-complement rulez :-) |