real2str.inc 15 KB

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