Questions on implementing a floating point bignum package

Discussion in 'C++' started by mike3, Dec 5, 2007.

  1. mike3

    mike3 Guest

    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);
    }
     
    mike3, Dec 5, 2007
    #1
    1. Advertising

  2. mike3

    mike3 Guest

    On Dec 4, 5:46 pm, mike3 <> wrote:
    <snip>

    Any comments? Should one implement all that stuff?
     
    mike3, Dec 10, 2007
    #2
    1. Advertising

  3. mike3

    mike3 Guest

    On Dec 4, 5:46 pm, mike3 <> wrote:
    > 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.
     
    mike3, Dec 16, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. H aka N
    Replies:
    15
    Views:
    15,666
    Ben Jones
    Mar 2, 2006
  2. Motaz Saad
    Replies:
    7
    Views:
    6,491
  3. Replies:
    4
    Views:
    1,291
    Default User
    Feb 22, 2006
  4. Saraswati lakki
    Replies:
    0
    Views:
    1,341
    Saraswati lakki
    Jan 6, 2012
  5. teeshift
    Replies:
    2
    Views:
    258
    Chris Pearl
    Dec 1, 2006
Loading...

Share This Page