real2str.inc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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 (trunc(intPartStack[stackPtr]-corrVal) > 5.0 - roundCorr) 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/exp((16-4-3)*ln(10)));
  181. end;
  182. rt_s64real :
  183. begin
  184. { if the maximum supported type is double, we can print out one digit }
  185. { less, because otherwise we can't round properly and 1e-400 becomes }
  186. { 0.99999999999e-400 (JM) }
  187. {$ifdef support_extended}
  188. maxlen:=23;
  189. { correction used with comparing to avoid rounding/precision errors }
  190. roundCorr := (1/exp((23-5-3)*ln(10)));
  191. {$else support_extended}
  192. {$ifdef support_double}
  193. maxlen := 22;
  194. { correction used with comparing to avoid rounding/precision errors }
  195. roundCorr := (1/exp((22-4-3)*ln(10)));
  196. {$endif support_double}
  197. {$endif support_extended}
  198. minlen:=9;
  199. explen:=5;
  200. end;
  201. rt_s80real :
  202. begin
  203. { Different in TP help, but this way the output is the same (JM) }
  204. maxlen:=25;
  205. minlen:=10;
  206. explen:=6;
  207. { correction used with comparing to avoid rounding/precision errors }
  208. roundCorr := (1/exp((25-6-3)*ln(10)));
  209. end;
  210. rt_c64bit :
  211. begin
  212. maxlen:=23;
  213. minlen:=10;
  214. { according to TP (was 5) (FK) }
  215. explen:=6;
  216. { correction used with comparing to avoid rounding/precision errors }
  217. roundCorr := (1/exp((23-6-3)*ln(10)));
  218. end;
  219. rt_currency :
  220. begin
  221. { Different in TP help, but this way the output is the same (JM) }
  222. maxlen:=25;
  223. minlen:=10;
  224. explen:=0;
  225. { correction used with comparing to avoid rounding/precision errors }
  226. roundCorr := (1/exp((25-6-3)*ln(10)));
  227. end;
  228. rt_s128real :
  229. begin
  230. { Different in TP help, but this way the output is the same (JM) }
  231. maxlen:=25;
  232. minlen:=10;
  233. explen:=6;
  234. { correction used with comparing to avoid rounding/precision errors }
  235. roundCorr := (1/exp((25-6-3)*ln(10)));
  236. end;
  237. end;
  238. { check parameters }
  239. { default value for length is -32767 }
  240. if len=-32767 then
  241. len:=maxlen;
  242. { determine sign. before precision, needs 2 less calls to abs() }
  243. {$ifndef endian_big}
  244. {$ifdef SUPPORT_EXTENDED}
  245. { extended, format (MSB): 1 Sign bit, 15 bit exponent, 64 bit mantissa }
  246. sign := (TSplitExtended(d).w and $8000) <> 0;
  247. expMaximal := (TSplitExtended(d).w and $7fff) = 32767;
  248. fraczero := (TSplitExtended(d).cards[0] = 0) and
  249. ((TSplitExtended(d).cards[1] and $7fffffff) = 0);
  250. {$else SUPPORT_EXTENDED}
  251. {$ifdef SUPPORT_DOUBLE}
  252. {$ifdef FPC_DOUBLE_HILO_SWAPPED}
  253. { double, format (MSB): 1 Sign bit, 11 bit exponent, 52 bit mantissa }
  254. { high and low dword are swapped when using the arm fpa }
  255. sign := ((TSplitDouble(d).cards[0] shr 20) and $800) <> 0;
  256. expMaximal := ((TSplitDouble(d).cards[0] shr 20) and $7ff) = 2047;
  257. fraczero:= (TSplitDouble(d).cards[0] and $fffff = 0) and
  258. (TSplitDouble(d).cards[1] = 0);
  259. {$else FPC_DOUBLE_HILO_SWAPPED}
  260. { double, format (MSB): 1 Sign bit, 11 bit exponent, 52 bit mantissa }
  261. sign := ((TSplitDouble(d).cards[1] shr 20) and $800) <> 0;
  262. expMaximal := ((TSplitDouble(d).cards[1] shr 20) and $7ff) = 2047;
  263. fraczero := (TSplitDouble(d).cards[1] and $fffff = 0) and
  264. (TSplitDouble(d).cards[0] = 0);
  265. {$endif FPC_DOUBLE_HILO_SWAPPED}
  266. {$else SUPPORT_DOUBLE}
  267. {$ifdef SUPPORT_SINGLE}
  268. { single, format (MSB): 1 Sign bit, 8 bit exponent, 23 bit mantissa }
  269. sign := ((TSplitSingle(d).words[1] shr 7) and $100) <> 0;
  270. expMaximal := ((TSplitSingle(d).words[1] shr 7) and $ff) = 255;
  271. fraczero := (TSplitSingle(d).cards[0] and $7fffff = 0);
  272. {$else SUPPORT_SINGLE}
  273. {$error No little endian floating type supported yet in real2str}
  274. {$endif SUPPORT_SINGLE}
  275. {$endif SUPPORT_DOUBLE}
  276. {$endif SUPPORT_EXTENDED}
  277. {$else endian_big}
  278. {$ifdef SUPPORT_EXTENDED}
  279. {$error sign/NaN/Inf not yet supported for big endian CPU's in str_real}
  280. {$else SUPPORT_EXTENDED}
  281. {$ifdef SUPPORT_DOUBLE}
  282. { double, format (MSB): 1 Sign bit, 11 bit exponent, 52 bit mantissa }
  283. sign := ((TSplitDouble(d).cards[0] shr 20) and $800) <> 0;
  284. expMaximal := ((TSplitDouble(d).cards[0] shr 20) and $7ff) = 2047;
  285. fraczero:= (TSplitDouble(d).cards[0] and $fffff = 0) and
  286. (TSplitDouble(d).cards[1] = 0);
  287. {$else SUPPORT_DOUBLE}
  288. {$ifdef SUPPORT_SINGLE}
  289. { single, format (MSB): 1 Sign bit, 8 bit exponent, 23 bit mantissa }
  290. sign := ((TSplitSingle(d).bytes[0] and $80)) <> 0;
  291. expMaximal := ((TSplitSingle(d).words[0] shr 7) and $ff) = 255;
  292. fraczero:= (TSplitSingle(d).cards[0] and $7fffff = 0);
  293. {$else SUPPORT_SINGLE}
  294. {$error No big endian floating type supported yet in real2str}
  295. {$endif SUPPORT_SINGLE}
  296. {$endif SUPPORT_DOUBLE}
  297. {$endif SUPPORT_EXTENDED}
  298. {$endif endian}
  299. if expMaximal then
  300. if fraczero then
  301. if sign then
  302. temp := '-Inf'
  303. else temp := '+Inf'
  304. else temp := 'Nan'
  305. else
  306. begin
  307. { d:=abs(d); this converts d to double so we loose precision }
  308. { for the same reason I converted d:=frac(d) to d:=d-int(d); (PM) }
  309. if sign then
  310. d:=-d;
  311. { determine precision : maximal precision is : }
  312. currPrec := maxlen-explen-2;
  313. { this is also the maximal number of decimals !!}
  314. if f>currprec then
  315. f:=currprec;
  316. { when doing a fixed-point, we need less characters.}
  317. if (f<0) {or ((d<>0) and ((d>maxexp) and (d>minexp)))} then
  318. begin
  319. { determine maximal number of decimals }
  320. if (len>=0) and (len<minlen) then
  321. len:=minlen;
  322. if (len>0) and (len<maxlen) then
  323. currprec:=len-explen-2;
  324. end;
  325. { leading zero, may be necessary for things like str(9.999:0:2) to }
  326. { be able to insert an extra character at the start of the string }
  327. temp := ' 0';
  328. { position in the temporary output string }
  329. spos := 2;
  330. { get the integer part }
  331. correct := 0;
  332. GetIntPart(d);
  333. { now process the fractional part }
  334. if d > 1.0- roundCorr then
  335. d := frac(d);
  336. { if we have to round earlier than the amount of available precision, }
  337. { only calculate digits up to that point }
  338. if (f >= 0) and (currPrec > f) then
  339. currPrec := f;
  340. { if integer part was zero, go to the first significant digit of the }
  341. { fractional part }
  342. { make sure we don't get an endless loop if d = 0 }
  343. if (spos = 2) and (d <> 0.0) then
  344. begin
  345. { take rounding errors into account }
  346. while d < 0.1-roundCorr do
  347. begin
  348. d := d * 10.0;
  349. dec(correct);
  350. { adjust the precision depending on how many digits we }
  351. { already "processed" by multiplying by 10, but only if }
  352. { the amount of precision is specified }
  353. if f >= 0 then
  354. dec(currPrec);
  355. end;
  356. dec(correct);
  357. end;
  358. { current length of the output string in endPos }
  359. endPos := spos;
  360. { always calculate at least 1 fractional digit for rounding }
  361. if (currPrec >= 0) then
  362. begin
  363. corrVal := 0.5;
  364. factor := 1;
  365. for fracCount := 1 to currPrec do
  366. factor := factor * 10.0;
  367. corrval := corrval / factor;
  368. if d >= corrVal then
  369. d := d + corrVal;
  370. if int(d) = 1 then
  371. begin
  372. roundStr(temp,spos);
  373. d := frac(d);
  374. if (f < 0) then
  375. dec(currprec);
  376. end;
  377. { calculate the necessary fractional digits }
  378. for fracCount := 1 to currPrec do
  379. begin
  380. if d > 1.0- roundCorr then
  381. d := frac(d) * 10.0
  382. else d := d * 10.0;
  383. inc(spos);
  384. temp[spos] := chr(trunc(d)+ord('0'));
  385. if temp[spos] > '9' then
  386. { possible because trunc and the "*10.0" aren't exact :( }
  387. begin
  388. temp[spos] := chr(ord(temp[spos]) - 10);
  389. roundStr(temp,spos-1);
  390. end;
  391. end;
  392. { new length of string }
  393. endPos := spos;
  394. end;
  395. setLength(temp,endPos);
  396. { delete leading zero if we didn't need it while rounding at the }
  397. { string level }
  398. if temp[2] = '0' then
  399. delete(temp,2,1)
  400. { the rounding caused an overflow to the next power of 10 }
  401. else inc(correct);
  402. if sign then
  403. temp[1] := '-';
  404. if (f<0) or (correct>(round(ln(maxexp)/ln(10)))) then
  405. begin
  406. insert ('.',temp,3);
  407. str(abs(correct),power);
  408. if length(power)<explen-2 then
  409. power:=copy(zero,1,explen-2-length(power))+power;
  410. if correct<0 then
  411. power:='-'+power
  412. else
  413. power:='+'+power;
  414. temp:=temp+'E'+power;
  415. end
  416. else
  417. begin
  418. if not sign then
  419. begin
  420. delete(temp,1,1);
  421. dot := 2
  422. end
  423. else
  424. dot := 3;
  425. { set zeroes and dot }
  426. if correct>=0 then
  427. begin
  428. if length(temp)<correct+dot+f-1 then
  429. temp:=temp+copy(zero,1,correct+dot+f-length(temp));
  430. insert ('.',temp,correct+dot);
  431. end
  432. else
  433. begin
  434. correct:=abs(correct);
  435. insert(copy(zero,1,correct),temp,dot-1);
  436. insert ('.',temp,dot);
  437. end;
  438. { correct length to fit precision }
  439. if f>0 then
  440. setlength(temp,pos('.',temp)+f)
  441. else
  442. setLength(temp,pos('.',temp)-1);
  443. end;
  444. end;
  445. if length(temp)<len then
  446. s:=space(len-length(temp))+temp
  447. else s:=temp;
  448. end;