real2str.inc 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1997 by Michael Van Canneyt,
  5. member of the Free Pascal development team
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. type
  13. treal_type = (rt_s64real,rt_s32real,rt_f32bit,rt_s80real,rt_s64bit);
  14. { corresponding to real single fixed extended and comp for i386 }
  15. {$ifdef i386}
  16. {$ifdef ver_above0_9_ still not ok }
  17. bestreal = extended; { still gives problems }
  18. {$else ver_above0_9_8}
  19. bestreal = double;
  20. {$endif ver_above0_9_8}
  21. {$else not i386}
  22. bestreal = single;
  23. {$endif not i386}
  24. Procedure str_real (len,f : longint; d : bestreal; real_type :treal_type; var s : string);
  25. { These numbers are for the double type...
  26. At the moment these are mapped onto a double but this may change
  27. in the future ! }
  28. var maxlen : longint; { Maximal length of string for float }
  29. minlen : longint; { Minimal length of string for float }
  30. explen : longint; { Length of exponent, including E and sign.
  31. Must be strictly larger than 2 }
  32. const
  33. maxexp = 1e+35; { Maximum value for decimal expressions }
  34. minexp = 1e-35; { Minimum value for decimal expressions }
  35. zero = '0000000000000000000000000000000000000000';
  36. var correct : longint; { Power correction }
  37. currprec : longint;
  38. roundcorr : bestreal;
  39. temp : string;
  40. power : string[10];
  41. sign : boolean;
  42. i : integer;
  43. dot : byte;
  44. begin
  45. case real_type of
  46. rt_s64real :
  47. begin
  48. maxlen:=23;
  49. minlen:=9;
  50. explen:=5;
  51. end;
  52. rt_s32real :
  53. begin
  54. maxlen:=16;
  55. minlen:=8;
  56. explen:=4;
  57. end;
  58. rt_f32bit :
  59. begin
  60. maxlen:=16;
  61. minlen:=8;
  62. explen:=4;
  63. end;
  64. rt_s80real :
  65. begin
  66. maxlen:=26;
  67. minlen:=10;
  68. explen:=6;
  69. end;
  70. rt_s64bit :
  71. begin
  72. maxlen:=24;
  73. minlen:=9;
  74. explen:=5;
  75. end;
  76. end;
  77. { check parameters }
  78. { default value for length is -32767 }
  79. {$ifdef ver_above0_9_7}
  80. if len=-32767 then len:=maxlen;
  81. {$else }
  82. if (len=-1) and (f=-1) then len:=maxlen;
  83. {$endif }
  84. { determine sign. before precision, needs 2 less calls to abs() }
  85. sign:=d<0;
  86. { the creates a cannot determine which overloaded function to call
  87. if d is extended !!!
  88. we should prefer real_to_real on real_to_longint !!
  89. corrected in compiler }
  90. { d:=abs(d); this converts d to double so we loose precision }
  91. { for the same reason I converted d:=frac(d) to d:=d-int(d); (PM) }
  92. if sign then d:=-d;
  93. { determine precision : maximal precision is : }
  94. currprec:=maxlen-explen-3;
  95. { this is also the maximal number of decimals !!}
  96. if f>currprec then f:=currprec;
  97. { when doing a fixed-point, we need less characters.}
  98. if (f<0) or ((d>maxexp) or (d<minexp)) then
  99. begin
  100. { determine maximal number of decimals }
  101. if (len>=0) and (len<minlen) then len:=minlen;
  102. if (len>0) and (len<maxlen) then
  103. currprec:=len-explen-3;
  104. end;
  105. { convert to standard form. }
  106. correct:=0;
  107. if d>=10.0 then
  108. while d>=10.0 do
  109. begin
  110. d:=d/10.0;
  111. inc(correct);
  112. end
  113. else if (d<1) and (d<>0) then
  114. while d<1 do
  115. begin
  116. d:=d*10.0;
  117. dec(correct);
  118. end;
  119. { RoundOff }
  120. roundcorr:=0.5;
  121. if f<0 then
  122. for i:=1 to currprec do roundcorr:=roundcorr/10
  123. else
  124. for i:=1 to correct+f do roundcorr:=roundcorr/10;
  125. d:=d+roundcorr;
  126. { 0.99 + 0.05 > 10.0 ! Fix this by dividing the results >=10 first (PV) }
  127. if d>=10.0 then
  128. begin
  129. d:=d/10.0;
  130. inc(correct);
  131. end;
  132. { Now we have a standard expression : sign d *10^correct
  133. where 1<d<10 or d=0 ... }
  134. { get first character }
  135. if sign then
  136. temp:='-'
  137. else
  138. temp:=' ';
  139. temp:=temp+chr(ord('0')+trunc(d));
  140. d:=d-int(d);
  141. { Start making the string }
  142. for i:=1 to currprec do
  143. begin
  144. d:=d*10.0;
  145. temp:=temp+chr(ord('0')+trunc(d));
  146. d:=d-int(d);
  147. end;
  148. { Now we need two different schemes for the different
  149. representations. }
  150. if (f<0) or (correct>maxexp) then
  151. begin
  152. insert ('.',temp,3);
  153. str(abs(correct),power);
  154. if length(power)<explen-2 then
  155. power:=copy(zero,1,explen-2-length(power))+power;
  156. if correct<0 then power:='-'+power else power:='+'+power;
  157. temp:=temp+'E'+power;
  158. end
  159. else
  160. begin
  161. if not sign then
  162. begin
  163. delete (temp,1,1);
  164. dot:=2;
  165. end
  166. else
  167. dot:=3;
  168. { set zeroes and dot }
  169. if correct>=0 then
  170. begin
  171. if length(temp)<correct+dot+f then
  172. temp:=temp+copy(zero,1,correct+dot+f-length(temp));
  173. insert ('.',temp,correct+dot);
  174. end
  175. else
  176. begin
  177. correct:=abs(correct);
  178. insert(copy(zero,1,correct),temp,dot-1);
  179. insert ('.',temp,dot);
  180. end;
  181. {correct length to fit precision.}
  182. if f>0 then
  183. temp[0]:=chr(pos('.',temp)+f)
  184. else
  185. temp[0]:=chr(pos('.',temp)-1);
  186. end;
  187. if length(temp)<len then
  188. s:=space(len-length(temp))+temp
  189. else
  190. s:=temp;
  191. end;
  192. {
  193. $Log$
  194. Revision 1.1 1998-03-25 11:18:43 root
  195. Initial revision
  196. Revision 1.7 1998/03/16 23:38:17 peter
  197. * fixed 0.997:0:2 bugs
  198. Revision 1.6 1998/01/26 11:59:47 michael
  199. + Added log at the end
  200. revision 1.5
  201. date: 1998/01/05 00:48:24; author: carl; state: Exp; lines: +2 -2
  202. + Now compatible with m68k floating point types
  203. ----------------------------
  204. revision 1.4
  205. date: 1997/12/02 17:44:45; author: pierre; state: Exp; lines: +2 -2
  206. * use of extended type in function str_real still buggy
  207. ----------------------------
  208. revision 1.3
  209. date: 1997/12/01 12:08:04; author: michael; state: Exp; lines: +12 -6
  210. + added copyright reference header.
  211. ----------------------------
  212. revision 1.2
  213. date: 1997/11/28 19:45:21; author: pierre; state: Exp; lines: +6 -3
  214. * one more bug fix with namelength
  215. + fixed math in fixed_math define (does not compile yet)
  216. ----------------------------
  217. revision 1.1
  218. date: 1997/11/27 08:33:47; author: michael; state: Exp;
  219. Initial revision
  220. ----------------------------
  221. revision 1.1.1.1
  222. date: 1997/11/27 08:33:47; author: michael; state: Exp; lines: +0 -0
  223. FPC RTL CVS start
  224. }