struct Bignum
[email protected] (Uri Guttman) Mon, 19 Nov 2001 01:45:21 -0500
| Newsgroups | perl.perl6.internals.bignum |
|---|---|
| Message-ID | <[email protected].> |
>>>>> "JG" == Jason Gloudon <[email protected]> writes: JG> On Sun, Nov 18, 2001 at 12:26:58AM -0500, Uri Guttman wrote: >> store. it can be changed by extending the number. we may have a flags >> for that so some numbers will extend forever. normal numbers will lose >> data if they have more digits than their precision allows. scaling will >> be handled by a combination of munging the int/frac struct members and >> the exponent. as i said before the exponent will always reflect how many >> 0 digits are before/after the decimal point. you effectively have to >> decimal shift the precision number of digits away from the decimal point >> to get the exponent into play. JG> Could you state the relationship between int digits, frac digits JG> and the exponent more explicitly at some point ? It's a little JG> fuzzy to me still. What about normalized representations ? sure. i will do some here and more later. a bignum has an array of words which hold bcd values. for this discussion (and future ones) i will assume 32 bit words and so 8 bcd digits in a word. 64 bits will work just as well given the right macro values. BTW all this is subject to change by you all if you think i am totally nuts. this is partly old territory since i did decimal math packages before but they were limited in precision. it is the unlimited precision that makes this harder. in a bignum with no exponent (expon = 0), then the values of int_digits and frac_digits tells you how many bcd digits are on each side of the decimal point. the DP is always between words. so a number like 123.456 with a precision of 16 would be stored: 00000123 (DP is here) 45600000 with int_digits and frac_digits both being 3. int_words and frac_words are both 1. buff_words is 2. the maximum precision of this bignum is 16. if it were multiplied by 100000000 then it would look like: 00000123 45600000 (DP is here) int_digits would be 11, frac_digits would be 0. int_words is 2 and frac_words is 0. expon is still 0. after another multiply by 100000000 then it would look like: 00000123 45600000 (DP is 8 behind here) and all the struct values are the same except for expon which is now 8. so the exponent is a way to move the digits off the decimal point without storing the zeroes. that is the whole point of precision, it limits the amount of actual stored bcd digits but allows for infinite scaling. so the int/frac part describe the actual location of the DP in the buffer and also how many digits are in use. the digits in use part is for conversions and scaling internally. we don't want to move any more data than we have to. having both int_words and int_digits allows us to finely control stuff. we can start multiplies on the correct highest order digit. now as for normalization, that is a good question. in most cases we won't need to do it. if you have a large precision number and it gets its value to 0, we may detect it by checking the words for the first one which isn't all 0 (easy) and then locate the highest order set digit in that word (harder). we need to formalize what normalization means for us and when do we do it. if a bignum has a flag that allows growth, then the exponent may never get set as the buffer will handle all the digits needed. only during conversions and internal operations would normalizing be needed and maybe even not then. we could just scan the numbers to skip zeroes on demand. so we will need routines to set precision, change it (both grow and shrink), assign bugnums to different precisions, etc. when you assign and need to truncate it, normalizing will be needed so you can lose higher order zeroes and not useful digits. so i view normalizing as more a scan to properly set the int/frac parts and not modifying the number itself in any signifigant way. uri -- Uri Guttman ------ [email protected] -------- http://www.stemsystems.com -- Stem is an Open Source Network Development Toolkit and Application Suite - ----- Stem and Perl Development, Systems Architecture, Design and Coding ---- Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org