real2str.inc 16 KB

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