real2str.inc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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. maxDigits = 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. maxDigits = 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. maxDigits = 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..maxDigits+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: valreal);
  80. var
  81. intPartStack: TIntPartStack;
  82. 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 > maxDigits+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 > maxDigits + 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. if temp[spos] > '9' then
  123. begin
  124. temp[spos] := chr(ord(temp[spos])-10);
  125. roundStr(temp,spos-1);
  126. end;
  127. end;
  128. corrVal := int(intPartStack[stackPtr]) * 10.0;
  129. dec(stackPtr);
  130. if stackPtr = 0 then
  131. stackPtr := maxDigits+1;
  132. until (overflow and (stackPtr = endStackPtr)) or
  133. (not overflow and (stackPtr = maxDigits+1)) or (currPrec = 0);
  134. { round if we didn't use all available digits yet and if the }
  135. { remainder is > 5 }
  136. if overflow and
  137. (trunc(intPartStack[stackPtr]-corrVal) > 5.0 - roundCorr) then
  138. roundStr(temp,spos);
  139. end;
  140. var maxlen : longint; { Maximal length of string for float }
  141. minlen : longint; { Minimal length of string for float }
  142. explen : longint; { Length of exponent, including E and sign.
  143. Must be strictly larger than 2 }
  144. const
  145. maxexp = 1e+35; { Maximum value for decimal expressions }
  146. minexp = 1e-35; { Minimum value for decimal expressions }
  147. zero = '0000000000000000000000000000000000000000';
  148. begin
  149. case real_type of
  150. rt_s32real :
  151. begin
  152. maxlen:=16;
  153. minlen:=8;
  154. explen:=4;
  155. { correction used with comparing to avoid rounding/precision errors }
  156. roundCorr := (1/exp((16-4-3)*ln(10)));
  157. end;
  158. rt_s64real :
  159. begin
  160. { if the maximum suppported type is double, we can print out one digit }
  161. { less, because otherwise we can't round properly and 1e-400 becomes }
  162. { 0.99999999999e-400 (JM) }
  163. {$ifdef support_extended}
  164. maxlen:=23;
  165. { correction used with comparing to avoid rounding/precision errors }
  166. roundCorr := (1/exp((23-5-3)*ln(10)));
  167. {$else support_extended}
  168. {$ifdef support_double}
  169. maxlen := 22;
  170. { correction used with comparing to avoid rounding/precision errors }
  171. roundCorr := (1/exp((22-4-3)*ln(10)));
  172. {$endif support_double}
  173. {$endif support_extended}
  174. minlen:=9;
  175. explen:=5;
  176. end;
  177. rt_s80real :
  178. begin
  179. { Different in TP help, but this way the output is the same (JM) }
  180. maxlen:=25;
  181. minlen:=10;
  182. explen:=6;
  183. { correction used with comparing to avoid rounding/precision errors }
  184. roundCorr := (1/exp((25-6-3)*ln(10)));
  185. end;
  186. rt_c64bit :
  187. begin
  188. maxlen:=23;
  189. minlen:=10;
  190. { according to TP (was 5) (FK) }
  191. explen:=6;
  192. { correction used with comparing to avoid rounding/precision errors }
  193. roundCorr := (1/exp((23-6-3)*ln(10)));
  194. end;
  195. rt_f16bit :
  196. begin
  197. maxlen:=16;
  198. minlen:=8;
  199. explen:=4;
  200. { correction used with comparing to avoid rounding/precision errors }
  201. roundCorr := (1/exp((16-4-3)*ln(10)));
  202. end;
  203. rt_f32bit :
  204. begin
  205. maxlen:=16;
  206. minlen:=8;
  207. explen:=4;
  208. { correction used with comparing to avoid rounding/precision errors }
  209. roundCorr := (1/exp((16-4-3)*ln(10)));
  210. end;
  211. end;
  212. { check parameters }
  213. { default value for length is -32767 }
  214. if len=-32767 then
  215. len:=maxlen;
  216. { determine sign. before precision, needs 2 less calls to abs() }
  217. {$ifndef endian_big}
  218. {$ifdef SUPPORT_EXTENDED}
  219. { extended, format (MSB): 1 Sign bit, 15 bit exponent, 64 bit mantissa }
  220. sign := (TSplitExtended(d).w and $8000) <> 0;
  221. expMaximal := (TSplitExtended(d).w and $7fff) = 32767;
  222. mantZero := (TSplitExtended(d).cards[0] = 0) and
  223. (TSplitExtended(d).cards[1] = 0);
  224. {$else SUPPORT_EXTENDED}
  225. {$ifdef SUPPORT_DOUBLE}
  226. { double, format (MSB): 1 Sign bit, 11 bit exponent, 52 bit mantissa }
  227. sign := ((TSplitDouble(d).cards[1] shr 20) and $800) <> 0;
  228. expMaximal := ((TSplitDouble(d).cards[1] shr 20) and $7ff) = 2047;
  229. mantZero := (TSplitDouble(d).cards[1] and $fffff = 0) and
  230. (TSplitDouble(d).cards[0] = 0);
  231. {$else SUPPORT_DOUBLE}
  232. {$ifdef SUPPORT_SINGLE}
  233. { single, format (MSB): 1 Sign bit, 8 bit exponent, 23 bit mantissa }
  234. sign := ((TSplitSingle(d).words[1] shr 7) and $100) <> 0;
  235. expMaximal := ((TSplitSingle(d).words[1] shr 7) and $ff) = 255;
  236. mantZero := (TSplitSingle(d).cards[0] and $7fffff = 0);
  237. {$else SUPPORT_SINGLE}
  238. {$error No big endian floating type supported yet in real2str}
  239. {$endif SUPPORT_SINGLE}
  240. {$endif SUPPORT_DOUBLE}
  241. {$endif SUPPORT_EXTENDED}
  242. {$else endian_big}
  243. {$ifdef SUPPORT_EXTENDED}
  244. {$error sign/NaN/Inf not yet supported for big endian CPU's in str_real}
  245. {$else SUPPORT_EXTENDED}
  246. {$ifdef SUPPORT_DOUBLE}
  247. { double, format (MSB): 1 Sign bit, 11 bit exponent, 52 bit mantissa }
  248. {$error sign/NaN/Inf not yet supported for big endian CPU's in str_real}
  249. {$else SUPPORT_DOUBLE}
  250. {$ifdef SUPPORT_SINGLE}
  251. { single, format (MSB): 1 Sign bit, 8 bit exponent, 23 bit mantissa }
  252. sign := ((TSplitSingle(d).bytes[0] and $80)) <> 0;
  253. expMaximal := ((TSplitSingle(d).words[0] shr 7) and $ff) = 255;
  254. mantZero := (TSplitSingle(d).cards[0] and $7fffff = 0);
  255. {$else SUPPORT_SINGLE}
  256. {$error No big endian floating type supported yet in real2str}
  257. {$endif SUPPORT_SINGLE}
  258. {$endif SUPPORT_DOUBLE}
  259. {$endif SUPPORT_EXTENDED}
  260. {$endif endian}
  261. if expMaximal then
  262. if mantZero then
  263. if sign then
  264. temp := '-Inf'
  265. else temp := 'Inf'
  266. else temp := 'NaN'
  267. else
  268. begin
  269. { d:=abs(d); this converts d to double so we loose precision }
  270. { for the same reason I converted d:=frac(d) to d:=d-int(d); (PM) }
  271. if sign then
  272. d:=-d;
  273. { determine precision : maximal precision is : }
  274. currPrec := maxlen-explen-2;
  275. { this is also the maximal number of decimals !!}
  276. if f>currprec then
  277. f:=currprec;
  278. { when doing a fixed-point, we need less characters.}
  279. if (f<0) {or ((d<>0) and ((d>maxexp) and (d>minexp)))} then
  280. begin
  281. { determine maximal number of decimals }
  282. if (len>=0) and (len<minlen) then
  283. len:=minlen;
  284. if (len>0) and (len<maxlen) then
  285. currprec:=len-explen-2;
  286. end;
  287. { leading zero, may be necessary for things like str(9.999:0:2) to }
  288. { be able to insert an extra character at the start of the string }
  289. temp := ' 0';
  290. { position in the temporary output string }
  291. spos := 2;
  292. { get the integer part }
  293. correct := 0;
  294. GetIntPart(d);
  295. { now process the fractional part }
  296. if d > 1.0- roundCorr then
  297. d := frac(d);
  298. { if we have to round earlier than the amount of available precision, }
  299. { only calculate digits up to that point }
  300. if (f >= 0) and (currPrec > f) then
  301. currPrec := f;
  302. { if integer part was zero, go to the first significant digit of the }
  303. { fractional part }
  304. { make sure we don't get an endless loop if d = 0 }
  305. if (spos = 2) and (d <> 0.0) then
  306. begin
  307. { take rounding errors into account }
  308. while d < 0.1-roundCorr do
  309. begin
  310. d := d * 10.0;
  311. dec(correct);
  312. { adjust the precision depending on how many digits we }
  313. { already "processed" by multiplying by 10, but only if }
  314. { the amount of precision is specified }
  315. if f >= 0 then
  316. dec(currPrec);
  317. end;
  318. dec(correct);
  319. end;
  320. { current length of the output string in endPos }
  321. endPos := spos;
  322. { always calculate at least 1 fractional digit for rounding }
  323. if (currPrec >= 0) then
  324. begin
  325. corrVal := 0.5;
  326. for fracCount := 1 to currPrec do
  327. corrVal := corrVal / 10.0;
  328. if d >= corrVal then
  329. d := d + corrVal;
  330. if int(d) = 1 then
  331. begin
  332. roundStr(temp,spos);
  333. d := frac(d);
  334. end;
  335. { calculate the necessary fractional digits }
  336. for fracCount := 1 to currPrec do
  337. begin
  338. if d > 1.0- roundCorr then
  339. d := frac(d) * 10.0
  340. else d := d * 10.0;
  341. inc(spos);
  342. temp[spos] := chr(trunc(d)+ord('0'));
  343. if temp[spos] > '9' then
  344. { possible because trunc and the "*10.0" aren't exact :( }
  345. begin
  346. temp[spos] := chr(ord(temp[spos]) - 10);
  347. roundStr(temp,spos-1);
  348. end;
  349. end;
  350. { new length of string }
  351. endPos := spos;
  352. end;
  353. setLength(temp,endPos);
  354. { delete leading zero if we didn't need it while rounding at the }
  355. { string level }
  356. if temp[2] = '0' then
  357. delete(temp,2,1)
  358. { the rounding caused an overflow to the next power of 10 }
  359. else inc(correct);
  360. if sign then
  361. temp[1] := '-';
  362. if (f<0) or (correct>(round(ln(maxexp)/ln(10)))) then
  363. begin
  364. insert ('.',temp,3);
  365. str(abs(correct),power);
  366. if length(power)<explen-2 then
  367. power:=copy(zero,1,explen-2-length(power))+power;
  368. if correct<0 then
  369. power:='-'+power
  370. else
  371. power:='+'+power;
  372. temp:=temp+'E'+power;
  373. end
  374. else
  375. begin
  376. if not sign then
  377. begin
  378. delete(temp,1,1);
  379. dot := 2
  380. end
  381. else
  382. dot := 3;
  383. { set zeroes and dot }
  384. if correct>=0 then
  385. begin
  386. if length(temp)<correct+dot+f-1 then
  387. temp:=temp+copy(zero,1,correct+dot+f-length(temp));
  388. insert ('.',temp,correct+dot);
  389. end
  390. else
  391. begin
  392. correct:=abs(correct);
  393. insert(copy(zero,1,correct),temp,dot-1);
  394. insert ('.',temp,dot);
  395. end;
  396. { correct length to fit precision }
  397. if f>0 then
  398. setlength(temp,pos('.',temp)+f)
  399. else
  400. setLength(temp,pos('.',temp)-1);
  401. end;
  402. end;
  403. if length(temp)<len then
  404. s:=space(len-length(temp))+temp
  405. else s:=temp;
  406. end;
  407. {
  408. $Log$
  409. Revision 1.4 2001-06-13 18:32:05 peter
  410. * big endian updates (merged)
  411. Revision 1.3 2001/04/23 18:25:45 peter
  412. * m68k updates
  413. Revision 1.2 2000/07/13 11:33:45 michael
  414. + removed logs
  415. }