M
mike3
Hi.
(I'm xposting this to comp.lang.c++ as well just in case C++-specific
questions arise)
I've been writing ths bignum package in C++, and am wondering about
this. See, it's really made only to do big floating point arithmetic,
and the way that I'm doing it now, I have routines like the one below
that assume their operands should all have the same length, routines
that manipulate bits and pieces of the bignums as needed to implement
the floating point arithmetic which have to handle variable lengths
but such handling is done, by the very nature of floating point, in a
way often different from that of ordinary integers (due, among other
things, to the mantissa of a floating point number being a fractional
expansion (in binary), not an integer, so the place-value of the bits
is different and hence the arithmetic works somewhat differently (ie.
to add 1.1101 and 1.1 (bin) you add the latter to the _upper_ digits
of the former, not the lower digits.).).
Is this a good idea? Or should I include that differing-length
handling in these lower-
level digit-manipulating routines?
Example:
/* Add two digit slices.
* Parameters:
* a: First slice.
* b: Second slice.
*
* Returns: carry.
*
* Operation: *this = a + b.
*
* Assumes: *this, a, b have same length. (The floating point routines
* are written in such a way so that's all we need so we don't include
* extra functionality for reasons of performance and wanting to avoid
* too much redundancy.) If lengths are unequal, uses the _smallest_
* length of all operands as a safety.
*/
Digit RawDigitSlice::Add(const RawDigitSlice &a, const RawDigitSlice
&b)
{
/* Set up iterators */
DigitIterator ri(start);
DigitIterator ai(a.start);
DigitIterator bi(b.start);
int length(std::min(std::min(a.length, b.length), length));
/* Add digits */
Digit carry(0);
for(int i(0);i<length;i++,ri++,ai++,bi++)
{
Digit tmp(*ai + *bi + carry);
carry = carry ? (tmp <= *ai) : (tmp < *ai);
*ri = tmp;
}
/* Done! */
return(carry);
}
(I'm xposting this to comp.lang.c++ as well just in case C++-specific
questions arise)
I've been writing ths bignum package in C++, and am wondering about
this. See, it's really made only to do big floating point arithmetic,
and the way that I'm doing it now, I have routines like the one below
that assume their operands should all have the same length, routines
that manipulate bits and pieces of the bignums as needed to implement
the floating point arithmetic which have to handle variable lengths
but such handling is done, by the very nature of floating point, in a
way often different from that of ordinary integers (due, among other
things, to the mantissa of a floating point number being a fractional
expansion (in binary), not an integer, so the place-value of the bits
is different and hence the arithmetic works somewhat differently (ie.
to add 1.1101 and 1.1 (bin) you add the latter to the _upper_ digits
of the former, not the lower digits.).).
Is this a good idea? Or should I include that differing-length
handling in these lower-
level digit-manipulating routines?
Example:
/* Add two digit slices.
* Parameters:
* a: First slice.
* b: Second slice.
*
* Returns: carry.
*
* Operation: *this = a + b.
*
* Assumes: *this, a, b have same length. (The floating point routines
* are written in such a way so that's all we need so we don't include
* extra functionality for reasons of performance and wanting to avoid
* too much redundancy.) If lengths are unequal, uses the _smallest_
* length of all operands as a safety.
*/
Digit RawDigitSlice::Add(const RawDigitSlice &a, const RawDigitSlice
&b)
{
/* Set up iterators */
DigitIterator ri(start);
DigitIterator ai(a.start);
DigitIterator bi(b.start);
int length(std::min(std::min(a.length, b.length), length));
/* Add digits */
Digit carry(0);
for(int i(0);i<length;i++,ri++,ai++,bi++)
{
Digit tmp(*ai + *bi + carry);
carry = carry ? (tmp <= *ai) : (tmp < *ai);
*ri = tmp;
}
/* Done! */
return(carry);
}