Post Reply 
Improve precision of float numbers
04-19-2019, 08:31 AM
Post: #15
RE: Improve precision of float numbers
@Druzyek: Thanks for motivating me.

To save floats in an char array seems to me that it needs much memory (similar to the BigNumber library). To enter an input string and convert it to float is very convenient (I tried this with one of my first calculators - ARC). But finally I switched to interpret every keypress at once and change the X-register immediately.

Softfloat, Float64 and Math64 are working properly - but first tries needed too much memory. And on my todo list is calculating with complex numbers, statistics, matrices and at least programming ... I'm sure that it's possible to involve these capabilities with the intrinsic (7 digit precision) float format.

But I'll try one last thing (and hope this doesn't consume to much memory):
I like the DEC64 format to save mantissa and exponent in one int64_t variable. The advantage is that interchanging numbers with subroutines is very handy. And to convert mantissa and exponent to DEC64 and vice versa can be done very quickly. And the sign of numbers is handled automatically (ie substraction equals addition with negative number ... a-b = a+ (-b) ).

The following code demonstrates how this could be done - even if the code is very inefficient and was quickly done. The input and display of those numbers needed approx 3k and involving basic math (+-*/) approx extra 3k.
What I still can't estimate is how much flash will be needed to call these functions very often (ie in case of calculating exp(x) with Taylor series).

Thanks for every hint and help (and motivation) - I love this forum.

Code:

static uint64_t Nassign(int64_t c, uint8_t e) { // Assign number
  return ((c << 8) + e);
}

static int64_t Nm(int64_t x) { // Extract mantissa
  return (x >> 8);
}

static int8_t Ne(int64_t x) { // Extract exponent
  return (x & 0xff);
}

static int64_t Nexpand(int64_t x) {
  return (Nassign(Nm(x) * 10, Ne(x) - 1));
}

static int64_t Nshrink(int64_t x) {
  return (Nassign(Nm(x) / 10, Ne(x) + 1));
}

static uint64_t Nresize(int64_t x) { // Resize x to max mantissa (needed for printing)
  if (Nm(x)) { // != 0
    while (_abs(Nm(x)) > NMAX) x = Nshrink(x);
    while (_abs(Nm(x)) < NMIN) x = Nexpand(x);
  }
  else return (Nassign(0, 1 - NDIGITS));
  return (x);
}

static uint64_t Nopt(int64_t x) { // Optimize x - reduce trailing zeros
  if (Nm(x) != 0) while (Nm(x) % 10 == 0) Nshrink(x);
  return (x);
}

static int64_t Nadd(int64_t a, int64_t b) { // Add two numbers
  if (Ne(a) == Ne(b)) return (Nassign(Nm(a) + Nm(b), Ne(a))); // Business numbers (fast addition)
  if (Ne(b) > Ne(a)) { // Swap so that a has the bigger exponent
    int64_t tmp = a; a = b; b = tmp;
  }
  while (Ne(a) > Ne(b) && Nm(a) < NMIN) { // Expand a as far as necessary or possible
    a = Nassign(Nm(a) * 10, Ne(a) - 1);
  }
  while (Ne(a) > Ne(b) && Nm(b) != 0) { // Shrink b as far as necessary (costs significance)
    b = Nassign(Nm(b) / 10, Ne(b) + 1);
  }
  return (Nassign(Nm(a) + Nm(b), Ne(b))); // Addition with equal exponents
}

static int64_t Nsub(int64_t a, int64_t b) { // Substract two numbers
  b = Nassign(-Nm(b), Ne(b));
  return (Nadd(a, b));
}

static int64_t Nmult(int64_t a, int64_t b) { // Multiply two numbers
  a = Nresize(a);
  b = Nresize(b);
  int64_t res = Nassign(0, Ne(a) + Ne(b));
  while (Nm(b)) {
    res = Nassign(Nm(res) / 10 + Nm(a) / 10 * (Nm(b) % 10), Ne(res) + 1);
    b = Nassign(Nm(b) / 10, Ne(b));
  }
  return (res);
}

static int64_t Ndiv(int64_t a, int64_t b) { // Divide two numbers
  if (Nm(b) == 0) return NINF;
  a = Nopt(a); b = Nopt(b);
  int64_t res;
  int8_t sign = _sign(Nm(a)) * _sign(Nm(b));
  a = Nassign(_abs(Nm(a)), Ne(a)); b = Nassign(_abs(Nm(b)), Ne(b));
  res = Nassign(0, Ne(a) - Ne(b) + 1);
  int64_t rest = Nm(a);
  while (Nm(res) <= NMIN) {
    res->m = res->m * 10 + rest / b.m;
    rest = rest % b.m * 10;
    (res->e)--;
  }
  return (res);
}
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: Improve precision of float numbers - deetee - 04-19-2019 08:31 AM



User(s) browsing this thread: 1 Guest(s)