Ok, so I have a working ftoa and the needed ulltoa (unsigned long long). I use unsigned long longs to get 12 digit precision decimals. It uses a lot of looping to brute-force calculate the result. No floating point math is used, I simply read the data from the float then expand it into whole numbers. Here it is:
Code:
void ulltoa(unsigned long long number, char *buffer, int base)
{
  char *b = (char*)malloc(15);
  *b = 0;
  while(number)
  {
    int x = number % 10;
    b++;
    *b = '0' + x;
    number /= 10;
  }
  while(*b)
    *(buffer++) = *(b--);
  *buffer = 0;
  free(b);
}
char*  floatToString(float num)
{
  unsigned long long fraction = 1000000000000LL;
  unsigned long long f = 0;
  unsigned long long d = 0;
  unsigned int data = *((int*)(&num));
  signed char e = data >> 23;
 
 
  e &= 0xff;
  e -= 127;
 
  bool sign = data & 0b10000000000000000000000000000000;
 
  unsigned int sig = data & 0b00000000011111111111111111111111;
  sig |= 0b00000000100000000000000000000000;
  sig <<= 8;
 
  for(int i = 23; i; i--)
  {
    bool a = sig & 0b10000000000000000000000000000000;
   
    if(a)
    {
      unsigned long long ft = fraction, dt = 0;
     
      for(int ei = e; ei != 0; ei -= (ei >= 0 ? 1 : -1))
      {
   if(ei > 0)
   {
     ft <<= 1;
     dt <<= 1;
   }
   else
   {
     ft >>= 1;
     dt >>= 1;
   }
   
   while(ft >= 1000000000000LL)
   {
     ft -= 1000000000000LL;
     dt ++;
   }
      }
      d += dt;
      f += ft;
     
      while(f >= 1000000000000LL)
      {
   f -= 1000000000000LL;
   d ++;
      }
    }
   
    fraction >>= 1;
    sig <<= 1;
  }
 
  char *buffer = (char*)malloc(30);
  char dbuffer[15],fbuffer[15];
 
  ulltoa(d,dbuffer,10);
  ulltoa(f,fbuffer,10);
 
  if(sign)
  {
    *buffer = '-';
    *(buffer+1) = 0;
  }
  else
    *buffer = 0;
 
  strcat(buffer, dbuffer);
  strcat(buffer, ".");
 
  int zeros = 12 - strlen(fbuffer);
  while(zeros--)
    *(buffer++) = '0';
 
  strcat(buffer, fbuffer);
 
  return buffer;
}


I can comment all of the steps in here if anybody wants to figure out what I am doing. But the function right now works. Doing cout << ftoa(-M_PI) << endl << -M_PI << endl; Gives

Code:
-3.141592502588
-3.141592
I can add in a rounding feature, but for the case that you really do want a tiny number, this function will print it.
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement