real2str.inc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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, orgd: 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. { double, format (MSB): 1 Sign bit, 11 bit exponent, 52 bit mantissa }
  254. sign := ((TSplitDouble(d).cards[1] shr 20) and $800) <> 0;
  255. expMaximal := ((TSplitDouble(d).cards[1] shr 20) and $7ff) = 2047;
  256. fraczero := (TSplitDouble(d).cards[1] and $fffff = 0) and
  257. (TSplitDouble(d).cards[0] = 0);
  258. {$else SUPPORT_DOUBLE}
  259. {$ifdef SUPPORT_SINGLE}
  260. { single, format (MSB): 1 Sign bit, 8 bit exponent, 23 bit mantissa }
  261. sign := ((TSplitSingle(d).words[1] shr 7) and $100) <> 0;
  262. expMaximal := ((TSplitSingle(d).words[1] shr 7) and $ff) = 255;
  263. fraczero := (TSplitSingle(d).cards[0] and $7fffff = 0);
  264. {$else SUPPORT_SINGLE}
  265. {$error No little endian floating type supported yet in real2str}
  266. {$endif SUPPORT_SINGLE}
  267. {$endif SUPPORT_DOUBLE}
  268. {$endif SUPPORT_EXTENDED}
  269. {$else endian_big}
  270. {$ifdef SUPPORT_EXTENDED}
  271. {$error sign/NaN/Inf not yet supported for big endian CPU's in str_real}
  272. {$else SUPPORT_EXTENDED}
  273. {$ifdef SUPPORT_DOUBLE}
  274. sign := ((TSplitDouble(d).cards[0] shr 20) and $800) <> 0;
  275. expMaximal := ((TSplitDouble(d).cards[0] shr 20) and $7ff) = 2047;
  276. fraczero:= (TSplitDouble(d).cards[0] and $fffff = 0) and
  277. (TSplitDouble(d).cards[1] = 0);
  278. { double, format (MSB): 1 Sign bit, 11 bit exponent, 52 bit mantissa }
  279. {$else SUPPORT_DOUBLE}
  280. {$ifdef SUPPORT_SINGLE}
  281. { single, format (MSB): 1 Sign bit, 8 bit exponent, 23 bit mantissa }
  282. sign := ((TSplitSingle(d).bytes[0] and $80)) <> 0;
  283. expMaximal := ((TSplitSingle(d).words[0] shr 7) and $ff) = 255;
  284. fraczero:= (TSplitSingle(d).cards[0] and $7fffff = 0);
  285. {$else SUPPORT_SINGLE}
  286. {$error No big endian floating type supported yet in real2str}
  287. {$endif SUPPORT_SINGLE}
  288. {$endif SUPPORT_DOUBLE}
  289. {$endif SUPPORT_EXTENDED}
  290. {$endif endian}
  291. if expMaximal then
  292. if fraczero then
  293. if sign then
  294. temp := '-Inf'
  295. else temp := '+Inf'
  296. else temp := 'Nan'
  297. else
  298. begin
  299. { d:=abs(d); this converts d to double so we loose precision }
  300. { for the same reason I converted d:=frac(d) to d:=d-int(d); (PM) }
  301. if sign then
  302. d:=-d;
  303. { determine precision : maximal precision is : }
  304. currPrec := maxlen-explen-2;
  305. { this is also the maximal number of decimals !!}
  306. if f>currprec then
  307. f:=currprec;
  308. { when doing a fixed-point, we need less characters.}
  309. if (f<0) {or ((d<>0) and ((d>maxexp) and (d>minexp)))} then
  310. begin
  311. { determine maximal number of decimals }
  312. if (len>=0) and (len<minlen) then
  313. len:=minlen;
  314. if (len>0) and (len<maxlen) then
  315. currprec:=len-explen-2;
  316. end;
  317. { leading zero, may be necessary for things like str(9.999:0:2) to }
  318. { be able to insert an extra character at the start of the string }
  319. temp := ' 0';
  320. { position in the temporary output string }
  321. spos := 2;
  322. { get the integer part }
  323. correct := 0;
  324. GetIntPart(d);
  325. { now process the fractional part }
  326. if d > 1.0- roundCorr then
  327. d := frac(d);
  328. { if we have to round earlier than the amount of available precision, }
  329. { only calculate digits up to that point }
  330. if (f >= 0) and (currPrec > f) then
  331. currPrec := f;
  332. { if integer part was zero, go to the first significant digit of the }
  333. { fractional part }
  334. { make sure we don't get an endless loop if d = 0 }
  335. if (spos = 2) and (d <> 0.0) then
  336. begin
  337. { take rounding errors into account }
  338. while d < 0.1-roundCorr do
  339. begin
  340. d := d * 10.0;
  341. dec(correct);
  342. { adjust the precision depending on how many digits we }
  343. { already "processed" by multiplying by 10, but only if }
  344. { the amount of precision is specified }
  345. if f >= 0 then
  346. dec(currPrec);
  347. end;
  348. dec(correct);
  349. end;
  350. { current length of the output string in endPos }
  351. endPos := spos;
  352. { always calculate at least 1 fractional digit for rounding }
  353. if (currPrec >= 0) then
  354. begin
  355. corrVal := 0.5;
  356. factor := 1;
  357. for fracCount := 1 to currPrec do
  358. factor := factor * 10.0;
  359. corrval := corrval / factor;
  360. if d >= corrVal then
  361. d := d + corrVal;
  362. if int(d) = 1 then
  363. begin
  364. roundStr(temp,spos);
  365. d := frac(d);
  366. end;
  367. { calculate the necessary fractional digits }
  368. for fracCount := 1 to currPrec do
  369. begin
  370. if d > 1.0- roundCorr then
  371. d := frac(d) * 10.0
  372. else d := d * 10.0;
  373. inc(spos);
  374. temp[spos] := chr(trunc(d)+ord('0'));
  375. if temp[spos] > '9' then
  376. { possible because trunc and the "*10.0" aren't exact :( }
  377. begin
  378. temp[spos] := chr(ord(temp[spos]) - 10);
  379. roundStr(temp,spos-1);
  380. end;
  381. end;
  382. { new length of string }
  383. endPos := spos;
  384. end;
  385. setLength(temp,endPos);
  386. { delete leading zero if we didn't need it while rounding at the }
  387. { string level }
  388. if temp[2] = '0' then
  389. delete(temp,2,1)
  390. { the rounding caused an overflow to the next power of 10 }
  391. else inc(correct);
  392. if sign then
  393. temp[1] := '-';
  394. if (f<0) or (correct>(round(ln(maxexp)/ln(10)))) then
  395. begin
  396. insert ('.',temp,3);
  397. str(abs(correct),power);
  398. if length(power)<explen-2 then
  399. power:=copy(zero,1,explen-2-length(power))+power;
  400. if correct<0 then
  401. power:='-'+power
  402. else
  403. power:='+'+power;
  404. temp:=temp+'E'+power;
  405. end
  406. else
  407. begin
  408. if not sign then
  409. begin
  410. delete(temp,1,1);
  411. dot := 2
  412. end
  413. else
  414. dot := 3;
  415. { set zeroes and dot }
  416. if correct>=0 then
  417. begin
  418. if length(temp)<correct+dot+f-1 then
  419. temp:=temp+copy(zero,1,correct+dot+f-length(temp));
  420. insert ('.',temp,correct+dot);
  421. end
  422. else
  423. begin
  424. correct:=abs(correct);
  425. insert(copy(zero,1,correct),temp,dot-1);
  426. insert ('.',temp,dot);
  427. end;
  428. { correct length to fit precision }
  429. if f>0 then
  430. setlength(temp,pos('.',temp)+f)
  431. else
  432. setLength(temp,pos('.',temp)-1);
  433. end;
  434. end;
  435. if length(temp)<len then
  436. s:=space(len-length(temp))+temp
  437. else s:=temp;
  438. end;
  439. {
  440. $Log$
  441. Revision 1.13 2003-12-29 19:19:21 jonas
  442. * fixed NaN/Inf detection for single/double
  443. Revision 1.12 2003/12/08 17:45:00 peter
  444. * currency support
  445. * dropped fixed16/32 support
  446. Revision 1.11 2003/10/26 16:56:44 jonas
  447. * fixed web bug 2643
  448. Revision 1.10 2003/09/06 17:06:59 florian
  449. * fixed Nan and +Inf string
  450. Revision 1.9 2003/09/06 16:48:35 florian
  451. * fixed real2str for -Inf and Inf
  452. Revision 1.8 2003/05/16 23:22:31 jonas
  453. * moved all loal variables to one block (necessary for ppc until nested
  454. procedures are properly supported)
  455. Revision 1.7 2002/10/04 16:41:17 jonas
  456. * fixed web bug 2131
  457. Revision 1.6 2002/09/07 15:07:46 peter
  458. * old logs removed and tabs fixed
  459. }