0

Suppose i have an algorithm by which i can compute an infinitely precise floating point number (depending from a parameter N) lets say in pseudocode:

arbitrary_precision_float f = computeValue(n); //it could be a function which compute a specific value, like PI for instance.

I guess i can implement computeValue(int) with the library mpf of the gnump library for example...

Anyway how can i split such number in sums of floating point number where each number has L Mantissa digits?

//example
f = x1 + x2 + ... + xn;
/*
for i = 1:n
  xi = 2^ei * Mi
 Mi has exactly p digits.
*/

I don't know if i'm clear but i'm looking for something "simple".

user8469759
  • 2,522
  • 6
  • 26
  • 50
  • Why not simply split the (binary) notation of `f` into groups of `p` digits? – Petr Jun 23 '15 at 12:41
  • because of the normalization stuff? – user8469759 Jun 23 '15 at 12:43
  • sorry, could you clarify more? – Petr Jun 23 '15 at 12:44
  • If the number is in fixed point i guess you can simply do what you said. In the case of floating point number what i mean is from "f" derive one or more floating point number where the accuracy is fixed to a parameter "p", which means moreover derive an exponent for each of such values. for example... assume you have a floating point "f" where the mantissa, with leading 1, is 10010001 if i apply a straight splitting i have x1 with mantissa 1001 and x2 with mantissa 0001, x2 is not normalized, and moreover i need to derive the exponent for each one. – user8469759 Jun 23 '15 at 12:54
  • The exponent can be easily derived from the positions of that split parts in the original mantissa. Afterwards, if some value starts with 0, you can just shift its value and change its exponent (making x2=1000 and decreasing the exponent by 3). Or if you want, say, minimal number of such numbers, then it's a separate question. – Petr Jun 23 '15 at 12:56
  • What do you mean with "minimal number of such numbers"? – user8469759 Jun 23 '15 at 12:58

1 Answers1

0

You can use a very simple algorithm. Assume without loss of generality that the exponent of your original number is zero; if it's not, then you just add that exponent to all the exponents of the answer.

Split your number f into groups of L digits and treat each group as a separate xi. Any such group can be represented in the form you need: the mantissa will be exactly that group, and the exponent will be negated start position of the group in the original number (that is, i*L, where i is the group number).

If any of the resulting xis starts from zero, you just shift its mantissa correcting the exponent correspondingly.

For example, for L=4

f = 10010011100
    1001     
        0011 
            100
-> x1=1.001 *2^0
   x2=0.011 *2^{-4} = 1.1*2^{-6}
   x3=1.00  *2^{-8}

Another question arises if you want to minimize the amount of numbers you get. In the example above, two numbers are sufficient: 1.001*2^0+1.11*2^{-6}. This is a separate question, and in fact is a simple problem for dynamic programming.

Petr
  • 9,812
  • 1
  • 28
  • 52
  • How to do in the case of minimize the amount of number i get? Out of curiosity, it could be useful. – user8469759 Jun 23 '15 at 13:43
  • @Lukkio, read a bit on the dynamic programming. Once you treat `f` as a sequence of 0s and 1s, it turns to a simple DP problem. – Petr Jun 23 '15 at 13:44