real2str.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1999-2000 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. { See symdefh.inc tfloattyp }
  14. treal_type = (rt_s32real,rt_s64real,rt_s80real,rt_c64bit,rt_f16bit,rt_f32bit);
  15. { corresponding to single double extended fixed comp for i386 }
  16. const
  17. { do not use real constants else you get rouding errors }
  18. i10 : longint = 10;
  19. i2 : longint = 2;
  20. i1 : longint = 1;
  21. (*
  22. { we can use this conditional if the Inf const is defined
  23. in processor specific code PM }
  24. {$ifdef FPC_HAS_INFINITY_CONST}
  25. {$define FPC_INFINITY_FOR_REAL2STR}
  26. {$else not FPC_HAS_INFINITY_CONST}
  27. { To avoid problems with infinity just
  28. issue it in byte representation to be endianness independant PM }
  29. {$ifndef FPC_INFINITY_FOR_REAL2STR}
  30. {$ifdef SUPPORT_EXTENDED}
  31. { extended is not IEEE so its processor specific
  32. so I only allow it for i386 PM }
  33. {$ifdef i386}
  34. {$define FPC_INFINITY_FOR_REAL2STR}
  35. InfArray : {extended} array[0..9] of byte = ($0,$0,$0,$0,$0,$0,$0,$80,$ff,$7f);
  36. {$endif i386}
  37. {$endif SUPPORT_EXTENDED}
  38. {$endif not FPC_INFINITY_FOR_REAL2STR}
  39. {$ifndef FPC_INFINITY_FOR_REAL2STR}
  40. {$ifdef SUPPORT_DOUBLE}
  41. {$define FPC_INFINITY_FOR_REAL2STR}
  42. InfArray : {double} array[0..9] of byte = ($0,$0,$0,$0,$0,$0,$f0,$7f);
  43. {$endif SUPPORT_DOUBLE}
  44. {$endif not FPC_INFINITY_FOR_REAL2STR}
  45. {$ifndef FPC_INFINITY_FOR_REAL2STR}
  46. {$ifdef SUPPORT_SINGLE}
  47. {$define FPC_INFINITY_FOR_REAL2STR}
  48. InfArray : {single} array[0..3] of byte = ($0,$0,$80,$7f);
  49. {$endif SUPPORT_SINGLE}
  50. {$endif not FPC_INFINITY_FOR_REAL2STR}
  51. {$ifndef FPC_INFINITY_FOR_REAL2STR}
  52. {$warning don't know Infinity values }
  53. {$endif not FPC_INFINITY_FOR_REAL2STR}
  54. {$endif not FPC_HAS_INFINITY_CONST}
  55. *)
  56. Procedure str_real (len,f : longint; d : ValReal; real_type :treal_type; var s : string);
  57. {
  58. These numbers are for the double type...
  59. At the moment these are mapped onto a double but this may change
  60. in the future !
  61. }
  62. var maxlen : longint; { Maximal length of string for float }
  63. minlen : longint; { Minimal length of string for float }
  64. explen : longint; { Length of exponent, including E and sign.
  65. Must be strictly larger than 2 }
  66. const
  67. maxexp = 1e+35; { Maximum value for decimal expressions }
  68. minexp = 1e-35; { Minimum value for decimal expressions }
  69. zero = '0000000000000000000000000000000000000000';
  70. type
  71. {$ifdef SUPPORT_EXTENDED}
  72. TSplitExtended = packed record
  73. case byte of
  74. 0: (bytes: Array[0..9] of byte);
  75. 1: (words: Array[0..4] of word);
  76. 2: (cards: Array[0..1] of cardinal; w: word);
  77. end;
  78. {$else}
  79. {$ifdef SUPPORT_DOUBLE}
  80. TSplitDouble = packed record
  81. case byte of
  82. 0: (bytes: Array[0..7] of byte);
  83. 1: (words: Array[0..3] of word);
  84. 2: (cards: Array[0..1] of cardinal);
  85. end;
  86. {$else}
  87. {$ifdef SUPPORT_SINGLE}
  88. TSplitSingle = packed record
  89. case byte of
  90. 0: (bytes: Array[0..3] of byte);
  91. 1: (words: Array[0..1] of word);
  92. 2: (cards: Array[0..0] of cardinal);
  93. end;
  94. {$endif SUPPORT_SINGLE}
  95. {$endif SUPPORT_DOUBLE}
  96. {$endif SUPPORT_EXTENDED}
  97. var correct : longint; { Power correction }
  98. currprec : longint;
  99. il,il2,roundcorr : Valreal;
  100. temp : string;
  101. power : string[10];
  102. sign : boolean;
  103. i : integer;
  104. dot : byte;
  105. currp : pchar;
  106. mantZero, expMaximal: boolean;
  107. begin
  108. case real_type of
  109. rt_s32real :
  110. begin
  111. maxlen:=16;
  112. minlen:=8;
  113. explen:=4;
  114. end;
  115. rt_s64real :
  116. begin
  117. maxlen:=23;
  118. minlen:=9;
  119. explen:=5;
  120. end;
  121. rt_s80real :
  122. begin
  123. maxlen:=26;
  124. minlen:=10;
  125. explen:=6;
  126. end;
  127. rt_c64bit :
  128. begin
  129. maxlen:=22;
  130. minlen:=9;
  131. { according to TP (was 5) (FK) }
  132. explen:=6;
  133. end;
  134. rt_f16bit :
  135. begin
  136. maxlen:=16;
  137. minlen:=8;
  138. explen:=4;
  139. end;
  140. rt_f32bit :
  141. begin
  142. maxlen:=16;
  143. minlen:=8;
  144. explen:=4;
  145. end;
  146. end;
  147. { check parameters }
  148. { default value for length is -32767 }
  149. if len=-32767 then
  150. len:=maxlen;
  151. { determine sign. before precision, needs 2 less calls to abs() }
  152. { sign:=d<0;}
  153. {$ifndef big_endian}
  154. {$ifdef SUPPORT_EXTENDED}
  155. { extended, format (MSB): 1 Sign bit, 15 bit exponent, 64 bit mantissa }
  156. sign := (TSplitExtended(d).w and $8000) <> 0;
  157. expMaximal := (TSplitExtended(d).w and $7fff) = 32767;
  158. mantZero := (TSplitExtended(d).cards[0] = 0) and
  159. (TSplitExtended(d).cards[1] = 0);
  160. {$else SUPPORT_EXTENDED}
  161. {$ifdef SUPPORT_DOUBLE}
  162. { double, format (MSB): 1 Sign bit, 11 bit exponent, 52 bit mantissa }
  163. sign := ((TSplitDouble(d).cards[1] shr 20) and $800) <> 0;
  164. expMaximal := ((TSplitDouble(d).cards[1] shr 20) and $7ff) = 2047;
  165. mantZero := (TSplitDouble(d).cards[1] and $fffff = 0) and
  166. (TSplitDouble(d).cards[0] = 0);
  167. {$else SUPPORT_DOUBLE}
  168. {$ifdef SUPPORT_SINGLE}
  169. { single, format (MSB): 1 Sign bit, 8 bit exponent, 23 bit mantissa }
  170. sign := ((TSplitSingle(d).words[1] shr 7) and $100) <> 0;
  171. expMaximal := ((TSplitSingle(d).words[1] shr 7) and $ff) = 255;
  172. mantZero := (TSplitSingle(d).cards[0] and $7fffff = 0);
  173. {$else SUPPORT_SINGLE}
  174. {$error No floating type supported for real2str}
  175. {$endif SUPPORT_SINGLE}
  176. {$endif SUPPORT_DOUBLE}
  177. {$endif SUPPORT_EXTENDED}
  178. {$else big_endian}
  179. {$error NaN/Inf not yet supported for big endian machines in str_real}
  180. {$endif big_endian}
  181. if expMaximal then
  182. if mantZero then
  183. if sign then
  184. temp := '-Inf'
  185. else temp := 'Inf'
  186. else temp := 'NaN'
  187. else
  188. begin
  189. { the creates a cannot determine which overloaded function to call
  190. if d is extended !!!
  191. we should prefer real_to_real on real_to_longint !!
  192. corrected in compiler }
  193. { d:=abs(d); this converts d to double so we loose precision }
  194. { for the same reason I converted d:=frac(d) to d:=d-int(d); (PM) }
  195. if sign then
  196. d:=-d;
  197. (*
  198. {$ifdef FPC_INFINITY_FOR_REAL2STR}
  199. {$ifndef FPC_HAS_INFINITY_CONST}
  200. if d=ValReal(InfArray) then
  201. {$else FPC_HAS_INFINITY_CONST}
  202. if d=Inf then
  203. {$endif FPC_HAS_INFINITY_CONST}
  204. begin
  205. if sign then
  206. s:='-Inf'
  207. else
  208. s:='Inf';
  209. exit;
  210. end;
  211. {$endif FPC_INFINITY_FOR_REAL2STR}
  212. *)
  213. { determine precision : maximal precision is : }
  214. currprec:=maxlen-explen-3;
  215. { this is also the maximal number of decimals !!}
  216. if f>currprec then
  217. f:=currprec;
  218. { when doing a fixed-point, we need less characters.}
  219. if (f<0) or ( (d<>0) and ((d>maxexp) or (d<minexp))) then
  220. begin
  221. { determine maximal number of decimals }
  222. if (len>=0) and (len<minlen) then
  223. len:=minlen;
  224. if (len>0) and (len<maxlen) then
  225. currprec:=len-explen-3;
  226. end;
  227. { convert to standard form. }
  228. correct:=0;
  229. if d>=i10 then
  230. begin
  231. il:=i1;
  232. il2:=i10;
  233. repeat
  234. il:=il2;
  235. il2:=il*i10;
  236. inc(correct);
  237. until (d<il2);
  238. d:=d/il;
  239. end
  240. else
  241. if (d<1) and (d<>0) then
  242. begin
  243. while d<1 do
  244. begin
  245. d:=d*i10;
  246. dec(correct);
  247. end;
  248. end;
  249. { RoundOff }
  250. roundcorr:=extended(i1)/extended(i2);
  251. if f<0 then
  252. for i:=1 to currprec do roundcorr:=roundcorr/i10
  253. else
  254. begin
  255. if correct+f<0 then
  256. begin
  257. for i:=1 to abs(correct+f) do
  258. roundcorr:=roundcorr*i10;
  259. end
  260. else
  261. begin
  262. for i:=1 to correct+f do
  263. roundcorr:=roundcorr/i10;
  264. end;
  265. end;
  266. d:=d+roundcorr;
  267. { 0.99 + 0.05 > 1.0 ! Fix this by dividing the results >=10 first (PV) }
  268. while (d>=10.0) do
  269. begin
  270. d:=d/i10;
  271. inc(correct);
  272. end;
  273. { Now we have a standard expression : sign d *10^correct
  274. where 1<d<10 or d=0 ... }
  275. { get first character }
  276. currp:=pchar(@temp[1]);
  277. if sign then
  278. currp^:='-'
  279. else
  280. currp^:=' ';
  281. inc(currp);
  282. currp^:=chr(ord('0')+trunc(d));
  283. inc(currp);
  284. d:=d-int(d);
  285. { Start making the string }
  286. for i:=1 to currprec do
  287. begin
  288. d:=d*i10;
  289. currp^:=chr(ord('0')+trunc(d));
  290. inc(currp);
  291. d:=d-int(d);
  292. end;
  293. temp[0]:=chr(currp-pchar(@temp[1]));
  294. { Now we need two different schemes for the different
  295. representations. }
  296. if (f<0) or (correct>maxexp) then
  297. begin
  298. insert ('.',temp,3);
  299. str(abs(correct),power);
  300. if length(power)<explen-2 then
  301. power:=copy(zero,1,explen-2-length(power))+power;
  302. if correct<0 then
  303. power:='-'+power
  304. else
  305. power:='+'+power;
  306. temp:=temp+'E'+power;
  307. end
  308. else
  309. begin
  310. if not sign then
  311. begin
  312. delete (temp,1,1);
  313. dot:=2;
  314. end
  315. else
  316. dot:=3;
  317. { set zeroes and dot }
  318. if correct>=0 then
  319. begin
  320. if length(temp)<correct+dot+f then
  321. temp:=temp+copy(zero,1,correct+dot+f-length(temp));
  322. insert ('.',temp,correct+dot);
  323. end
  324. else
  325. begin
  326. correct:=abs(correct);
  327. insert(copy(zero,1,correct),temp,dot-1);
  328. insert ('.',temp,dot);
  329. end;
  330. { correct length to fit precision }
  331. if f>0 then
  332. temp[0]:=chr(pos('.',temp)+f)
  333. else
  334. temp[0]:=chr(pos('.',temp)-1);
  335. end;
  336. end;
  337. if length(temp)<len then
  338. s:=space(len-length(temp))+temp
  339. else
  340. s:=temp;
  341. end;
  342. {
  343. $Log$
  344. Revision 1.22 2000-02-09 16:59:31 peter
  345. * truncated log
  346. Revision 1.21 2000/02/09 12:17:51 peter
  347. * moved halt to system.inc
  348. * syslinux doesn't use direct asm anymore
  349. Revision 1.20 2000/01/17 13:00:51 jonas
  350. + support for NaN's, cleaner support for Inf
  351. Revision 1.19 2000/01/07 16:41:36 daniel
  352. * copyright 2000
  353. Revision 1.18 1999/11/28 23:57:23 pierre
  354. * Infinite loop for infinite value problem fixed
  355. Revision 1.17 1999/11/03 09:54:24 peter
  356. * another fix for precision
  357. Revision 1.16 1999/11/03 00:55:09 pierre
  358. * problem of last commit for large d values corrected
  359. Revision 1.15 1999/11/02 15:05:53 peter
  360. * better precisio by dividing only once with a calculated longint
  361. instead of multiple times by 10
  362. Revision 1.14 1999/08/03 21:58:44 peter
  363. * small speed improvements
  364. }