real2str.inc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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. Procedure str_real (len,f : longint; d : ValReal; real_type :treal_type; var s : string);
  17. {$ifdef SUPPORT_EXTENDED}
  18. type
  19. TSplitExtended = packed record
  20. case byte of
  21. 0: (bytes: Array[0..9] of byte);
  22. 1: (words: Array[0..4] of word);
  23. 2: (cards: Array[0..1] of cardinal; w: word);
  24. end;
  25. const
  26. maxPrec = 17;
  27. {$else}
  28. {$ifdef SUPPORT_DOUBLE}
  29. type
  30. TSplitDouble = packed record
  31. case byte of
  32. 0: (bytes: Array[0..7] of byte);
  33. 1: (words: Array[0..3] of word);
  34. 2: (cards: Array[0..1] of cardinal);
  35. end;
  36. const
  37. maxPrec = 14;
  38. {$else}
  39. {$ifdef SUPPORT_SINGLE}
  40. type
  41. TSplitSingle = packed record
  42. case byte of
  43. 0: (bytes: Array[0..3] of byte);
  44. 1: (words: Array[0..1] of word);
  45. 2: (cards: Array[0..0] of cardinal);
  46. end;
  47. const
  48. maxPrec = 9;
  49. {$endif SUPPORT_SINGLE}
  50. {$endif SUPPORT_DOUBLE}
  51. {$endif SUPPORT_EXTENDED}
  52. type
  53. { the value in the last position is used for rounding }
  54. TIntPartStack = array[1..maxPrec+1] of valReal;
  55. var
  56. roundCorr, corrVal: valReal;
  57. intPart, spos, endpos, fracCount: longint;
  58. correct, currprec: longint;
  59. temp : string;
  60. power : string[10];
  61. sign : boolean;
  62. dot : byte;
  63. mantZero, expMaximal: boolean;
  64. procedure RoundStr(var s: string; lastPos: byte);
  65. var carry: longint;
  66. begin
  67. carry := 1;
  68. repeat
  69. s[lastPos] := chr(ord(s[lastPos])+carry);
  70. carry := 0;
  71. if s[lastPos] > '9' then
  72. begin
  73. s[lastPos] := '0';
  74. carry := 1;
  75. end;
  76. dec(lastPos);
  77. until carry = 0;
  78. end;
  79. procedure getIntPart(d: extended);
  80. var
  81. intPartStack: TIntPartStack;
  82. count, stackPtr, endStackPtr, digits: longint;
  83. overflow: boolean;
  84. begin
  85. { position in the stack (gets increased before first write) }
  86. stackPtr := 0;
  87. { number of digits processed }
  88. digits := 0;
  89. { did we wrap around in the stack? Necessary to know whether we should round }
  90. overflow :=false;
  91. { generate a list consisting of d, d/10, d/100, ... until d < 1.0 }
  92. while d > 1.0-roundCorr do
  93. begin
  94. inc(stackPtr);
  95. inc(digits);
  96. if stackPtr > maxPrec+1 then
  97. begin
  98. stackPtr := 1;
  99. overflow := true;
  100. end;
  101. intPartStack[stackPtr] := d;
  102. d := d / 10.0;
  103. end;
  104. { if no integer part, exit }
  105. if digits = 0 then
  106. exit;
  107. endStackPtr := stackPtr+1;
  108. if endStackPtr > maxPrec + 1 then
  109. endStackPtr := 1;
  110. { now, all digits are calculated using trunc(d*10^(-n)-int(d*10^(-n-1))*10) }
  111. corrVal := 0.0;
  112. { the power of 10 with which the resulting string has to be "multiplied" }
  113. { if the decimal point is placed after the first significant digit }
  114. correct := digits-1;
  115. repeat
  116. if (currprec > 0) then
  117. begin
  118. intPart:= trunc(intPartStack[stackPtr]-corrVal);
  119. dec(currPrec);
  120. inc(spos);
  121. temp[spos] := chr(intPart+ord('0'));
  122. end;
  123. corrVal := int(intPartStack[stackPtr]) * 10.0;
  124. dec(stackPtr);
  125. if stackPtr = 0 then
  126. stackPtr := maxPrec+1;
  127. until (overflow and (stackPtr = endStackPtr)) or
  128. (not overflow and (stackPtr = maxPrec+1)) or (currPrec = 0);
  129. { round if we didn't use all available digits yet and if the }
  130. { remainder is > 5 }
  131. if overflow and
  132. (trunc(intPartStack[stackPtr]-corrVal) > 5.0 - roundCorr) then
  133. roundStr(temp,spos);
  134. end;
  135. var maxlen : longint; { Maximal length of string for float }
  136. minlen : longint; { Minimal length of string for float }
  137. explen : longint; { Length of exponent, including E and sign.
  138. Must be strictly larger than 2 }
  139. const
  140. maxexp = 1e+35; { Maximum value for decimal expressions }
  141. minexp = 1e-35; { Minimum value for decimal expressions }
  142. zero = '0000000000000000000000000000000000000000';
  143. begin
  144. case real_type of
  145. rt_s32real :
  146. begin
  147. maxlen:=16;
  148. minlen:=8;
  149. explen:=4;
  150. end;
  151. rt_s64real :
  152. begin
  153. { if the maximum suppported type is double, we can print out one digit }
  154. { less, because otherwise we can't round properly and 1e-400 becomes }
  155. { 0.99999999999e-400 (JM) }
  156. {$ifdef support_extended}
  157. maxlen:=23;
  158. {$else support_extended}
  159. {$ifdef support_double}
  160. maxlen := 22;
  161. {$endif support_double}
  162. {$endif support_extended}
  163. minlen:=9;
  164. explen:=5;
  165. end;
  166. rt_s80real :
  167. begin
  168. maxlen:=26;
  169. minlen:=10;
  170. explen:=6;
  171. end;
  172. rt_c64bit :
  173. begin
  174. maxlen:=22;
  175. minlen:=9;
  176. { according to TP (was 5) (FK) }
  177. explen:=6;
  178. end;
  179. rt_f16bit :
  180. begin
  181. maxlen:=16;
  182. minlen:=8;
  183. explen:=4;
  184. end;
  185. rt_f32bit :
  186. begin
  187. maxlen:=16;
  188. minlen:=8;
  189. explen:=4;
  190. end;
  191. end;
  192. { check parameters }
  193. { default value for length is -32767 }
  194. if len=-32767 then
  195. len:=maxlen;
  196. { determine sign. before precision, needs 2 less calls to abs() }
  197. {$ifndef big_endian}
  198. {$ifdef SUPPORT_EXTENDED}
  199. { extended, format (MSB): 1 Sign bit, 15 bit exponent, 64 bit mantissa }
  200. sign := (TSplitExtended(d).w and $8000) <> 0;
  201. expMaximal := (TSplitExtended(d).w and $7fff) = 32767;
  202. mantZero := (TSplitExtended(d).cards[0] = 0) and
  203. (TSplitExtended(d).cards[1] = 0);
  204. {$else SUPPORT_EXTENDED}
  205. {$ifdef SUPPORT_DOUBLE}
  206. { double, format (MSB): 1 Sign bit, 11 bit exponent, 52 bit mantissa }
  207. sign := ((TSplitDouble(d).cards[1] shr 20) and $800) <> 0;
  208. expMaximal := ((TSplitDouble(d).cards[1] shr 20) and $7ff) = 2047;
  209. mantZero := (TSplitDouble(d).cards[1] and $fffff = 0) and
  210. (TSplitDouble(d).cards[0] = 0);
  211. {$else SUPPORT_DOUBLE}
  212. {$ifdef SUPPORT_SINGLE}
  213. { single, format (MSB): 1 Sign bit, 8 bit exponent, 23 bit mantissa }
  214. sign := ((TSplitSingle(d).words[1] shr 7) and $100) <> 0;
  215. expMaximal := ((TSplitSingle(d).words[1] shr 7) and $ff) = 255;
  216. mantZero := (TSplitSingle(d).cards[0] and $7fffff = 0);
  217. {$else SUPPORT_SINGLE}
  218. {$error No big endian floating type supported yet in real2str}
  219. {$endif SUPPORT_SINGLE}
  220. {$endif SUPPORT_DOUBLE}
  221. {$endif SUPPORT_EXTENDED}
  222. {$else big_endian}
  223. {$error sign/NaN/Inf not yet supported for big endian CPU's in str_real}
  224. {$endif big_endian}
  225. if expMaximal then
  226. if mantZero then
  227. if sign then
  228. temp := '-Inf'
  229. else temp := 'Inf'
  230. else temp := 'NaN'
  231. else
  232. begin
  233. { d:=abs(d); this converts d to double so we loose precision }
  234. { for the same reason I converted d:=frac(d) to d:=d-int(d); (PM) }
  235. if sign then
  236. d:=-d;
  237. { determine precision : maximal precision is : }
  238. currprec := maxlen-explen-2;
  239. { this is also the maximal number of decimals !!}
  240. if f>currprec then
  241. f:=currprec;
  242. { when doing a fixed-point, we need less characters.}
  243. if (f<0) {or ((d<>0) and ((d>maxexp) and (d>minexp)))} then
  244. begin
  245. { determine maximal number of decimals }
  246. if (len>=0) and (len<minlen) then
  247. len:=minlen;
  248. if (len>0) and (len<maxlen) then
  249. currprec:=len-explen-2;
  250. end;
  251. { leading zero, may be necessary for things like str(9.999:0:2) to }
  252. { be able to insert an extra character at the start of the string }
  253. temp := ' 0';
  254. { correction used with comparing to avoid rounding/precision errors }
  255. roundCorr := (1/exp(maxPrec*ln(10)));
  256. { position in the temporary output string }
  257. spos := 2;
  258. { get the integer part }
  259. correct := 0;
  260. GetIntPart(d);
  261. { now process the fractional part }
  262. d := frac(d);
  263. { if integer part was zero, go to the first significant digit of the }
  264. { fractional part }
  265. { make sure we don't get an endless loop if d = 0 }
  266. if (spos = 2) and (d <> 0.0) then
  267. begin
  268. { take rounding errors into account }
  269. while d < 1.0-roundCorr do
  270. begin
  271. d := d * 10.0;
  272. dec(correct);
  273. end;
  274. { adjust the precision depending on how many digits we already }
  275. { "processed" by multiplying by 10 }
  276. { if currPrec >= abs(Correct) then
  277. currPrec := currPrec - abs(correct)+1;}
  278. end;
  279. { current length of the output string in endPos }
  280. endPos := spos;
  281. { if we have to round earlier than the amount of available precision, }
  282. { only calculate digits up to that point }
  283. if (f >= 0) and (currPrec > f) then
  284. currPrec := f;
  285. { always calculate at least 1 fractional digit for rounding }
  286. if (currPrec >= 0) then
  287. begin
  288. if (currPrec > 0) then
  289. { do some preliminary rounding (necessary, just like the }
  290. { rounding afterwards) }
  291. begin
  292. corrVal := 0.5;
  293. for fracCount := 1 to currPrec do
  294. corrVal := corrVal / 10.0;
  295. if d > corrVal then
  296. d := d + corrVal;
  297. end;
  298. { 0.0 < d < 1.0 if we didn't round of earlier, otherwise 1 < d < 10 }
  299. if d < 1.0-roundCorr then
  300. corrVal := frac(d) * 10
  301. else corrVal := d;
  302. { calculate the necessary fractional digits }
  303. for fracCount := 1 to currPrec do
  304. begin
  305. inc(spos);
  306. temp[spos] := chr(trunc(corrVal)+ord('0'));
  307. corrVal := frac(corrVal)*10.0;
  308. end;
  309. { round off. We left a zero at the start, so we can't underflow }
  310. { to the length byte }
  311. if (corrVal-5.0 > roundCorr) then
  312. roundStr(temp,spos);
  313. { new length of string }
  314. endPos := spos;
  315. end;
  316. setLength(temp,endPos);
  317. { delete leading zero if we didn't need it while rounding at the }
  318. { string level }
  319. if temp[2] = '0' then
  320. delete(temp,2,1);
  321. if sign then
  322. temp[1] := '-';
  323. if (f<0) or (correct>(round(ln(maxexp)/ln(10)))) then
  324. begin
  325. insert ('.',temp,3);
  326. str(abs(correct),power);
  327. if length(power)<explen-2 then
  328. power:=copy(zero,1,explen-2-length(power))+power;
  329. if correct<0 then
  330. power:='-'+power
  331. else
  332. power:='+'+power;
  333. temp:=temp+'E'+power;
  334. end
  335. else
  336. begin
  337. delete(temp,1,1);
  338. dot := 2;
  339. { set zeroes and dot }
  340. if correct>=0 then
  341. begin
  342. if length(temp)<correct+dot+f-1 then
  343. temp:=temp+copy(zero,1,correct+dot+f-length(temp));
  344. insert ('.',temp,correct+dot);
  345. end
  346. else
  347. begin
  348. correct:=abs(correct);
  349. insert(copy(zero,1,correct),temp,dot-1);
  350. insert ('.',temp,dot);
  351. end;
  352. { correct length to fit precision }
  353. if f>0 then
  354. setlength(temp,pos('.',temp)+f)
  355. else
  356. setLength(temp,pos('.',temp)-1);
  357. end;
  358. end;
  359. if length(temp)<len then
  360. s:=space(len-length(temp))+temp
  361. else s:=temp;
  362. end;
  363. {
  364. $Log$
  365. Revision 1.24 2000-02-26 18:53:11 jonas
  366. * fix for lost precision because sometimes the correction value was
  367. larger than the number to be corrected
  368. * incompatibility with TP's output fixed
  369. Revision 1.23 2000/02/26 15:49:40 jonas
  370. + new str_real which is completely TP compatible regarding output
  371. format and which should have no rounding errors anymore
  372. Revision 1.22 2000/02/09 16:59:31 peter
  373. * truncated log
  374. Revision 1.21 2000/02/09 12:17:51 peter
  375. * moved halt to system.inc
  376. * syslinux doesn't use direct asm anymore
  377. Revision 1.20 2000/01/17 13:00:51 jonas
  378. + support for NaN's, cleaner support for Inf
  379. Revision 1.19 2000/01/07 16:41:36 daniel
  380. * copyright 2000
  381. Revision 1.18 1999/11/28 23:57:23 pierre
  382. * Infinite loop for infinite value problem fixed
  383. Revision 1.17 1999/11/03 09:54:24 peter
  384. * another fix for precision
  385. Revision 1.16 1999/11/03 00:55:09 pierre
  386. * problem of last commit for large d values corrected
  387. Revision 1.15 1999/11/02 15:05:53 peter
  388. * better precisio by dividing only once with a calculated longint
  389. instead of multiple times by 10
  390. Revision 1.14 1999/08/03 21:58:44 peter
  391. * small speed improvements
  392. }