Guest User

Untitled

a guest
Apr 24th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.43 KB | None | 0 0
  1. /*#include "lcd.h"
  2. // putchar est standard C ANSI et n'existe pas sur C18 !
  3. int putchar(char a)
  4. {
  5.     putcLcd(a);
  6.     return 0;
  7. }
  8. */
  9. // ftoa n'existe pas sur C18
  10.  
  11. // prec indique la prÈcison (nb de chiffrers aprËs la virgule) 0 pour avoir le maximum
  12. // si format ='s' -> affichage scientifique 1.6666666// ‡ la place de =*E3
  13. // si format ='f' -> affichage classique 1666.6666
  14.  
  15. unsigned char *ftoa (float x, unsigned char *str,char prec,char format)
  16. {
  17. /* converts a floating point number to an ascii string */
  18. /* x is stored into str, which should be at least 30 chars long */
  19. unsigned char *adpt;
  20. int ie, i, k, ndig, fstyle;
  21. double y;
  22. adpt=str;
  23. //if (nargs() != 7)  IEHzap("ftoa  ");
  24. ndig = ( prec<=0) ? 7 : (prec > 22 ? 23 : prec+1);
  25. if  (format == 'f' || format == 'F')
  26.  fstyle = 1;
  27. else
  28.  fstyle = 0;
  29. /* print in e format unless last arg is 'f' */
  30. ie = 0;
  31. /* if x negative, write minus and reverse */
  32. if ( x < 0)
  33.   {
  34.   *str++ = '-';
  35.   x = -x;
  36.   }
  37.  
  38. /* put x in range 1 <= x < 10 */
  39. if (x > 0.0) while (x < 1.0)
  40.   {
  41.   x *= 10.0;        // ‡ la place de =*
  42.   ie--;
  43.   }
  44. while (x >= 10.0)
  45.   {
  46.   x = x/10.0;
  47.   ie++;
  48.   }
  49.  
  50. /* in f format, number of digits is related to size */
  51. if (fstyle) ndig += ie;             // ‡ la place de =+
  52.  
  53. /* round. x is between 1 and 10 and ndig will be printed to
  54.    right of decimal point so rounding is ... */
  55. for (y = i = 1; i < ndig; i++)
  56.   y = y/10.;
  57. x += y/2.;                          // ‡ la place de =+
  58. if (x >= 10.0) {x = 1.0; ie++;} /* repair rounding disasters */
  59. /* now loop.  put out a digit (obtain by multiplying by
  60.   10, truncating, subtracting) until enough digits out */
  61. /* if fstyle, and leading zeros, they go out special */
  62. if (fstyle && ie<0)
  63.   {
  64.   *str++ = '0'; *str++ = '.';
  65.   if (ndig < 0) ie = ie-ndig; /* limit zeros if underflow */
  66.   for (i = -1; i > ie; i--)
  67.     *str++ = '0';
  68.   }
  69. for (i=0; i < ndig; i++)
  70.   {
  71.   k = x;
  72.   *str++ = k + '0';
  73.   if (i == (fstyle ? ie : 0)) /* where is decimal point */
  74.     *str++ = '.';
  75.   x -= (y=k);               // ‡ la place de =-
  76.   x *= 10.0;                // ‡ la place de =*
  77.   }
  78.  
  79. /* now, in estyle,  put out exponent if not zero */
  80. if (!fstyle && ie != 0)
  81.   {
  82.   *str++ = 'E';
  83.   if (ie < 0)
  84.     {
  85.     ie = -ie;
  86.     *str++ = '-';
  87.     }
  88.   for (k=100; k > ie; k /=10);  // ‡ la place de =/
  89.   for (; k > 0; k /=10)         // ‡ la place de =/
  90.        {
  91.        *str++ = ie/k + '0';
  92.        ie = ie%k;
  93.        }
  94.   }
  95. *str = '\0';
  96. return (adpt);
  97. }
Add Comment
Please, Sign In to add comment