Questions on implementing a floating point bignum package

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);
}
 
M

mike3

Hi.

(I'm xposting this to comp.lang.c++ as well just in case C++-specific
questions arise)

I've been writing thsbignumpackage 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?
<snip>

Hmm? I really would like to know this, as I'm having trouble deciding
if I should or shouldn't.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
SterlingLa
Top