fork(1) download
  1. #include <stdio.h>
  2.  
  3. /*
  4. Because lcd and serial don't support printf, and its very costly, and all we need
  5. is simple formating with a certain number of digits and precision, this ftoa is enough.
  6. If you need more, see:
  7. https://g...content-available-to-author-only...b.com/charlesnicholson/nanoprintf
  8. */
  9.  
  10. //avoid needed stdlib.h by defining abs as a macro
  11. #define absq(amt) ((amt)<0?0-(amt):(amt))
  12. //avoid needing math.h by defining powers of 10 as an array
  13. long pow10[10] = {1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000};
  14.  
  15. int puts_int( //send positive integers to STDOUT via putchar(char)
  16. unsigned int num //positive integer to display
  17. ,char digits //count of digits, negative and '0' pad for trailing zeros
  18. ,char pad //character to pad with, or null
  19. ) {
  20. unsigned int i;
  21. char c;
  22. unsigned char d;
  23. d = absq(digits);
  24. while (0 < d) {
  25. i = num/pow10[--d]; //pre-increment
  26. if (d && !i) { //no digits found yet. d && for leading zero
  27. if (pad) putchar(pad); else digits--; //track digits (not) used
  28. }
  29. else { //found something
  30. c = i%10; //only want the lowest digit
  31. //Optional: Don't continue after fraction done
  32. if ('0'==pad && 0<digits && 0==c) return digits-d;
  33. putchar(c+'0'); //convert to ASCII and send
  34. }
  35. }
  36. return absq(digits)-d;
  37. }
  38.  
  39. int puts_float( //send floating point numbers to STDOUT
  40. float f //number to display
  41. , char digits //digits, negative to pad with spaces
  42. , char precision //precision, negative to keep trailing zeros
  43. ) {
  44. unsigned int a;
  45. char i=0;
  46.  
  47. if(digits>=10) {return 0;};
  48. // check for negative float
  49. if(f<0.0) { //is it negative?
  50. putchar('-');i++; //indicate
  51. f*=-1; //make it positive
  52. if (0>digits) digits++; //optional, steal digit for sign, keep length
  53. }
  54.  
  55. a=(int)f; // extract whole number
  56. i+=puts_int(a,digits,digits>0?0:' ');
  57. if (precision) {
  58. putchar('.');i++;
  59. f-=(float)a; //remove whole part
  60. f*=pow10[absq(precision)]; // promote to precision
  61. f+=0.5; // round
  62. a=(int)f; // extract whole number
  63. i+=puts_int(a,precision,'0');
  64. }
  65. return i; //count of characters
  66. }
  67.  
  68.  
  69. int main(void) {
  70. float f;
  71. //puts_int(12345,6,' ');puts("");
  72. puts_float(1.0000, -9, 4);putchar(' ');
  73. puts_float(12345678.1234, 9, 4);putchar(' '); //8 digits no precision left. 9 digits fails
  74. puts_float(100000000, 9, 4);putchar('\r'); //only 8 usable digits, but 9 digit numbers are ok.
  75. for (f=0.0001;f<0.00091;f+=0.0001) { puts_float(f, 5, -4); putchar(' '); }; puts("");
  76. for (f=0.001;f<0.0091;f+=0.001) { puts_float(f, 5, -4); putchar(' '); }; puts("");
  77. for (f=0.01;f<0.091;f+=0.01) { puts_float(f, 5, -4); putchar(' '); }; puts("");
  78. for (f=0.1;f<0.91;f+=0.1) { puts_float(f, 5, -4); putchar(' '); }; puts("");
  79. //also puts_int the return value of puts_float, which is the character used count.
  80. puts_int(puts_float(123406.789, -8, 3),4,' ');putchar(',');putchar(' ');
  81. puts_int(puts_float(123456, 8, -3),4,' ');putchar(',');putchar(' ');
  82. puts_int(puts_float(123450.789, 8, 0),4,' ');putchar('\r');
  83. puts_int(puts_float(-4.4, 8, 4),4,' ');putchar(',');putchar(' ');
  84. puts_int(puts_float(-4.4, -8, -4),4,' ');putchar('\r');
  85. return 0;
  86. }
  87.  
Success #stdin #stdout 0s 9432KB
stdin
Standard input is empty
stdout
        1.000 12345678.000 100000000.000
0.0001 0.0002 0.0003 0.0004 0.0005 0.0006 0.0007 0.0008 0.0009 
0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 
0.0100 0.0200 0.0300 0.0400 0.0500 0.0600 0.0700 0.0800 0.0900 
0.1000 0.2000 0.3000 0.4000 0.5000 0.6000 0.7000 0.8000 0.9000 
  123406.789  12, 123456.000  10, 123450   6
-4.4   5, -      4.4000  13