cvt.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. Copyright 2018 Embedded Microprocessor Benchmark Consortium (EEMBC)
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. #include <math.h>
  14. #define CVTBUFSIZE 80
  15. static char CVTBUF[CVTBUFSIZE];
  16. static char *
  17. cvt(double arg, int ndigits, int *decpt, int *sign, char *buf, int eflag)
  18. {
  19. int r2;
  20. double fi, fj;
  21. char * p, *p1;
  22. if (ndigits < 0)
  23. ndigits = 0;
  24. if (ndigits >= CVTBUFSIZE - 1)
  25. ndigits = CVTBUFSIZE - 2;
  26. r2 = 0;
  27. *sign = 0;
  28. p = &buf[0];
  29. if (arg < 0)
  30. {
  31. *sign = 1;
  32. arg = -arg;
  33. }
  34. arg = modf(arg, &fi);
  35. p1 = &buf[CVTBUFSIZE];
  36. if (fi != 0)
  37. {
  38. p1 = &buf[CVTBUFSIZE];
  39. while (fi != 0)
  40. {
  41. fj = modf(fi / 10, &fi);
  42. *--p1 = (int)((fj + .03) * 10) + '0';
  43. r2++;
  44. }
  45. while (p1 < &buf[CVTBUFSIZE])
  46. *p++ = *p1++;
  47. }
  48. else if (arg > 0)
  49. {
  50. while ((fj = arg * 10) < 1)
  51. {
  52. arg = fj;
  53. r2--;
  54. }
  55. }
  56. p1 = &buf[ndigits];
  57. if (eflag == 0)
  58. p1 += r2;
  59. *decpt = r2;
  60. if (p1 < &buf[0])
  61. {
  62. buf[0] = '\0';
  63. return buf;
  64. }
  65. while (p <= p1 && p < &buf[CVTBUFSIZE])
  66. {
  67. arg *= 10;
  68. arg = modf(arg, &fj);
  69. *p++ = (int)fj + '0';
  70. }
  71. if (p1 >= &buf[CVTBUFSIZE])
  72. {
  73. buf[CVTBUFSIZE - 1] = '\0';
  74. return buf;
  75. }
  76. p = p1;
  77. *p1 += 5;
  78. while (*p1 > '9')
  79. {
  80. *p1 = '0';
  81. if (p1 > buf)
  82. ++*--p1;
  83. else
  84. {
  85. *p1 = '1';
  86. (*decpt)++;
  87. if (eflag == 0)
  88. {
  89. if (p > buf)
  90. *p = '0';
  91. p++;
  92. }
  93. }
  94. }
  95. *p = '\0';
  96. return buf;
  97. }
  98. char *
  99. ecvt(double arg, int ndigits, int *decpt, int *sign)
  100. {
  101. return cvt(arg, ndigits, decpt, sign, CVTBUF, 1);
  102. }
  103. char *
  104. ecvtbuf(double arg, int ndigits, int *decpt, int *sign, char *buf)
  105. {
  106. return cvt(arg, ndigits, decpt, sign, buf, 1);
  107. }
  108. char *
  109. fcvt(double arg, int ndigits, int *decpt, int *sign)
  110. {
  111. return cvt(arg, ndigits, decpt, sign, CVTBUF, 0);
  112. }
  113. char *
  114. fcvtbuf(double arg, int ndigits, int *decpt, int *sign, char *buf)
  115. {
  116. return cvt(arg, ndigits, decpt, sign, buf, 0);
  117. }