real2str.inc 15 KB

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