Re: Integer Base Function
"Frederick Gotham" <fgothamNO@SPAM.com> wrote of my Base
function:
Robbie Hatley posted:
const long long MAX = 9223372036854775807LL;
Remove the "LL" tagged on the end, it serves no purpose.
Like many other things I do in my code, its purpose has
has more to do with reminding human readers (including myself)
of various things, than satisfying linguistic or computational
requirements.
In fact, it will only hinder you if you decide to change
the integer type you're using.
Like, to "long long long"? :-) If, some day, we get 128bit
integers available.
long long long Ago;
In_A_Galaxy(FarFar Away);
(Or maybe "infinitely_long int", a type that stretches; it's
however big it needs to be. If you assign a number to it
that has more digits than its memory can handle, your program
just allocates more memory. Now why hasn't anyone thunk of
that yet? If std::string can do it, why not have an integer
type that can, too? )
For info on integer literals, go to page 20 of the following document:
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1905.pdf
879 pages? Wow, that's enormous. I can see the stds. committee
have been busy. Thanx for the reference. I saved a copy. I agree,
the new wording on page 20 is less confusing than the struck-out
version.
MAX and MIN are widely used as macro names for other things -- expect
conflict.
Good point. "Max" and "Min" would be better.
numeric_limits<long long>::max() would be preferable
Very good point. Esp. if I were to templatize the function.
Then I could do:
template<typename T>
std::string Base(int base, int precision, T number)
{
T Max = numeric_limits<T>::max() - 5; // see below
T Min = numeric_limits<T>::min() + 5; // see below
// etc.
}
If you write:
-9223372036854775808
It's intepreted as:
-(9223372036854775808)
Hmmm... yes, I see in section 2.13.1 that you're right.
Therefore you need to write:
-9223372036854775807 -1
I'll either use -9223372036854775803 or numerical_limit<T>::min().
if (any of several error conditions are true)
{
return std::string("ERROR"); // then return "ERROR".
}
I'd consider that inefficient, and would use asserts instead.
assert() calls abort(), which crashes the calling application
and dumps big ugly core all over the user's screen. Not necessary
just because a display function such as this fails. Better is
to just return an error message which the caller can display,
react-to, or ignore. Even throwing an exception would be
overkill in this case, I think, because that would force callers
to catch() them; else, again, abort() is called.
(Though, now that I think of it, "ERROR" is a very bad choice,
because in bases over 26, it's a valid number!)
std::ostringstream repre;
if (number < 0)
{
number *= (-1);
You have a bug there.
The number system used by the machine may have asymmetrical ranges for
positive and negative integers. There's likely to be a negative value
which hasn't got a corresponding positive value.
Usually, 2's compliment is used for signed numbers, in which case
TYPE_MIN = -(TYPE_MAX + 1), so all positive integers are invertible.
(TYPE_MIN, on the other hand, is always the one integer that's NOT
invertible in 2's compliment.)
However, I see upon looking at section 3.9.1 parargraph 2 that the
std. doesn't really specify the number system for signed integers.
So maybe this will do the trick, unless the system is VERY
asymmetrical:
T Max = numeric_limits<T>::max() - 5;
T Min = numeric_limits<T>::min() + 5;
if (condition) repre << digits[value];
That's inefficient; there's no need for a "ostringstream" object.
Since the stuff I'm writing to the stringstream are just characters,
I suppose I could make repre a std::string and do:
if (condition) repre += digits[value];
Yes, that would be more efficient.
Thanks for the tips.
--
Cheers,
Robbie Hatley
Tustin, CA, USA
lonewolfintj at pacbell dot net
(put "[usenet]" in subject to bypass spam filter)
http://home.pacbell.net/earnur/