source: trunk/libs/newlib/src/newlib/libc/stdlib/efgcvt.c @ 577

Last change on this file since 577 was 444, checked in by satin@…, 6 years ago

add newlib,libalmos-mkh, restructure shared_syscalls.h and mini-libc

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