real2str.inc 16 KB

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