Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. /*
  2. FUNCTION
  3. <<ecvt>>, <<ecvtf>>, <<fcvt>>, <<fcvtf>>---double or float to string
  4.  
  5. INDEX
  6.         ecvt
  7. INDEX
  8.         ecvtf
  9. INDEX
  10.         fcvt
  11. INDEX
  12.         fcvtf
  13.  
  14. ANSI_SYNOPSIS
  15.         #include <stdlib.h>
  16.  
  17.         char *ecvt(double <[val]>, int <[chars]>, int *<[decpt]>, int *<[sgn]>);
  18.         char *ecvtf(float <[val]>, int <[chars]>, int *<[decpt]>, int *<[sgn]>);
  19.  
  20.         char *fcvt(double <[val]>, int <[decimals]>,
  21.                    int *<[decpt]>, int *<[sgn]>);
  22.         char *fcvtf(float <[val]>, int <[decimals]>,
  23.                     int *<[decpt]>, int *<[sgn]>);
  24.  
  25. TRAD_SYNOPSIS
  26.         #include <stdlib.h>
  27.  
  28.         char *ecvt(<[val]>, <[chars]>, <[decpt]>, <[sgn]>);
  29.         double <[val]>;
  30.         int <[chars]>;
  31.         int *<[decpt]>;
  32.         int *<[sgn]>;
  33.         char *ecvtf(<[val]>, <[chars]>, <[decpt]>, <[sgn]>);
  34.         float <[val]>;
  35.         int <[chars]>;
  36.         int *<[decpt]>;
  37.         int *<[sgn]>;
  38.  
  39.         char *fcvt(<[val]>, <[decimals]>, <[decpt]>, <[sgn]>);
  40.         double <[val]>;
  41.         int <[decimals]>;
  42.         int *<[decpt]>;
  43.         int *<[sgn]>;
  44.         char *fcvtf(<[val]>, <[decimals]>, <[decpt]>, <[sgn]>);
  45.         float <[val]>;
  46.         int <[decimals]>;
  47.         int *<[decpt]>;
  48.         int *<[sgn]>;
  49.  
  50. DESCRIPTION
  51. <<ecvt>> and <<fcvt>> produce (null-terminated) strings of digits
  52. representating the <<double>> number <[val]>.
  53. <<ecvtf>> and <<fcvtf>> produce the corresponding character
  54. representations of <<float>> numbers.
  55.  
  56. (The <<stdlib>> functions <<ecvtbuf>> and <<fcvtbuf>> are reentrant
  57. versions of <<ecvt>> and <<fcvt>>.)
  58.  
  59. The only difference between <<ecvt>> and <<fcvt>> is the
  60. interpretation of the second argument (<[chars]> or <[decimals]>).
  61. For <<ecvt>>, the second argument <[chars]> specifies the total number
  62. of characters to write (which is also the number of significant digits
  63. in the formatted string, since these two functions write only digits).
  64. For <<fcvt>>, the second argument <[decimals]> specifies the number of
  65. characters to write after the decimal point; all digits for the integer
  66. part of <[val]> are always included.
  67.  
  68. Since <<ecvt>> and <<fcvt>> write only digits in the output string,
  69. they record the location of the decimal point in <<*<[decpt]>>>, and
  70. the sign of the number in <<*<[sgn]>>>.  After formatting a number,
  71. <<*<[decpt]>>> contains the number of digits to the left of the
  72. decimal point.  <<*<[sgn]>>> contains <<0>> if the number is positive,
  73. and <<1>> if it is negative.
  74.  
  75. RETURNS
  76. All four functions return a pointer to the new string containing a
  77. character representation of <[val]>.
  78.  
  79. PORTABILITY
  80. None of these functions are ANSI C.
  81.  
  82. Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
  83. <<lseek>>, <<read>>, <<sbrk>>, <<write>>.
  84.  
  85. NEWPAGE
  86. FUNCTION
  87. <<gcvt>>, <<gcvtf>>---format double or float as string
  88.  
  89. INDEX
  90.         gcvt
  91. INDEX
  92.         gcvtf
  93.  
  94. ANSI_SYNOPSIS
  95.         #include <stdlib.h>
  96.  
  97.         char *gcvt(double <[val]>, int <[precision]>, char *<[buf]>);
  98.         char *gcvtf(float <[val]>, int <[precision]>, char *<[buf]>);
  99.  
  100. TRAD_SYNOPSIS
  101.         #include <stdlib.h>
  102.  
  103.         char *gcvt(<[val]>, <[precision]>, <[buf]>);
  104.         double <[val]>;
  105.         int <[precision]>;
  106.         char *<[buf]>;
  107.         char *gcvtf(<[val]>, <[precision]>, <[buf]>);
  108.         float <[val]>;
  109.         int <[precision]>;
  110.         char *<[buf]>;
  111.  
  112. DESCRIPTION
  113. <<gcvt>> writes a fully formatted number as a null-terminated
  114. string in the buffer <<*<[buf]>>>.  <<gcvtf>> produces corresponding
  115. character representations of <<float>> numbers.
  116.  
  117. <<gcvt>> uses the same rules as the <<printf>> format
  118. `<<%.<[precision]>g>>'---only negative values are signed (with
  119. `<<->>'), and either exponential or ordinary decimal-fraction format
  120. is chosen depending on the number of significant digits (specified by
  121. <[precision]>).
  122.  
  123. RETURNS
  124. The result is a pointer to the formatted representation of <[val]>
  125. (the same as the argument <[buf]>).
  126.  
  127. PORTABILITY
  128. Neither function is ANSI C.
  129.  
  130. Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
  131. <<lseek>>, <<read>>, <<sbrk>>, <<write>>.
  132. */
  133.  
  134. #define _XOPEN_SOURCE
  135. #define _XOPEN_SOURCE_EXTENDED
  136. #include <_ansi.h>
  137. #include <reent.h>
  138. #include <stdio.h>
  139. #include <stdlib.h>
  140. #include "local.h"
  141.  
  142. char *
  143. _DEFUN (fcvt, (d, ndigit, decpt, sign),
  144.         double d _AND
  145.         int ndigit _AND
  146.         int *decpt _AND
  147.         int *sign)
  148. {
  149.   return fcvtbuf (d, ndigit, decpt, sign, NULL);
  150. }
  151.  
  152. char *
  153. _DEFUN (fcvtf, (d, ndigit, decpt, sign),
  154.         float d _AND
  155.         int ndigit _AND
  156.         int *decpt _AND
  157.         int *sign)
  158. {
  159.   return fcvt ((float) d, ndigit, decpt, sign);
  160. }
  161.  
  162.  
  163. char *
  164. _DEFUN (gcvtf, (d, ndigit, buf),
  165.         float d _AND
  166.         int ndigit _AND
  167.         char *buf)
  168. {
  169.   double asd = d;
  170.   return gcvt (asd, ndigit, buf);
  171. }
  172.  
  173.  
  174. char *
  175. _DEFUN (ecvt, (d, ndigit, decpt, sign),
  176.         double d _AND
  177.         int ndigit _AND
  178.         int *decpt _AND
  179.         int *sign)
  180. {
  181.   return ecvtbuf (d, ndigit, decpt, sign, NULL);
  182. }
  183.  
  184. char *
  185. _DEFUN (ecvtf, (d, ndigit, decpt, sign),
  186.         float d _AND
  187.         int ndigit _AND
  188.         int *decpt _AND
  189.         int *sign)
  190. {
  191.   return ecvt ((double) d, ndigit, decpt, sign);
  192. }
  193.  
  194.  
  195. char *
  196. _DEFUN (gcvt, (d, ndigit, buf),
  197.         double d _AND
  198.         int ndigit _AND
  199.         char *buf)
  200. {
  201.   char *tbuf = buf;
  202.   if (d < 0) {
  203.     *buf = '-';
  204.     buf++;
  205.     ndigit--;
  206.   }
  207.   return (_gcvt (_REENT, d, ndigit, buf, 'g', 0) ? tbuf : 0);
  208. }
  209.