real2str.inc 15 KB

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