| | @@ -529,8 +529,154 @@ |
| 529 | 529 | until depth = 0; |
| 530 | 530 | end; |
| 531 | 531 | |
| 532 | +{ Reading a decimal real exactly: the F64 nearest to digits times 10 to |
| 533 | + an exponent, ties to the even one, which a library's Val doesn't |
| 534 | + promise. The number is a fraction of two big integers, num / den, |
| 535 | + scaled by 2^e until its whole part has 53 or 54 bits; that part and |
| 536 | + the remainder give the bits and the rounding. } |
| 537 | +type |
| 538 | + TBig = record |
| 539 | + n: longint; { words in use; 0 is zero } |
| 540 | + w: array[0..95] of longword; { least significant first } |
| 541 | + end; |
| 542 | + |
| 543 | +{ a := a * m + add } |
| 544 | +procedure BigMul(var a: TBig; m, add: longword); |
| 545 | +var i: longint; c: qword; |
| 546 | +begin |
| 547 | + c := add; |
| 548 | + for i := 0 to a.n - 1 do begin |
| 549 | + c := qword(a.w[i]) * m + c; |
| 550 | + a.w[i] := longword(c); |
| 551 | + c := c shr 32; |
| 552 | + end; |
| 553 | + if c <> 0 then begin a.w[a.n] := longword(c); inc(a.n) end; |
| 554 | +end; |
| 555 | + |
| 556 | +procedure BigShl(var a: TBig; k: longint); |
| 557 | +var i, words, bits: longint; |
| 558 | +begin |
| 559 | + if a.n = 0 then exit; |
| 560 | + words := k div 32; bits := k mod 32; |
| 561 | + a.w[a.n + words] := 0; |
| 562 | + for i := a.n - 1 downto 0 do begin |
| 563 | + if bits > 0 then a.w[i + words + 1] := a.w[i + words + 1] or (a.w[i] shr (32 - bits)); |
| 564 | + a.w[i + words] := a.w[i] shl bits; |
| 565 | + end; |
| 566 | + for i := 0 to words - 1 do a.w[i] := 0; |
| 567 | + inc(a.n, words + 1); |
| 568 | + while (a.n > 0) and (a.w[a.n - 1] = 0) do dec(a.n); |
| 569 | +end; |
| 570 | + |
| 571 | +procedure BigShr1(var a: TBig); |
| 572 | +var i: longint; |
| 573 | +begin |
| 574 | + for i := 0 to a.n - 1 do begin |
| 575 | + a.w[i] := a.w[i] shr 1; |
| 576 | + if i + 1 < a.n then a.w[i] := a.w[i] or (a.w[i + 1] shl 31); |
| 577 | + end; |
| 578 | + while (a.n > 0) and (a.w[a.n - 1] = 0) do dec(a.n); |
| 579 | +end; |
| 580 | + |
| 581 | +function BigCmp(const a, b: TBig): longint; |
| 582 | +var i: longint; |
| 583 | +begin |
| 584 | + if a.n <> b.n then exit(ord(a.n > b.n) * 2 - 1); |
| 585 | + for i := a.n - 1 downto 0 do |
| 586 | + if a.w[i] <> b.w[i] then exit(ord(a.w[i] > b.w[i]) * 2 - 1); |
| 587 | + BigCmp := 0; |
| 588 | +end; |
| 589 | + |
| 590 | +{ a := a - b, b not above a } |
| 591 | +procedure BigSub(var a: TBig; const b: TBig); |
| 592 | +var i: longint; d: int64; |
| 593 | +begin |
| 594 | + d := 0; |
| 595 | + for i := 0 to a.n - 1 do begin |
| 596 | + d := d + a.w[i]; |
| 597 | + if i < b.n then d := d - b.w[i]; |
| 598 | + a.w[i] := longword(d); |
| 599 | + d := -ord(d < 0); { a borrow } |
| 600 | + end; |
| 601 | + while (a.n > 0) and (a.w[a.n - 1] = 0) do dec(a.n); |
| 602 | +end; |
| 603 | + |
| 604 | +function BigBits(const a: TBig): longint; |
| 605 | +var t: longword; |
| 606 | +begin |
| 607 | + if a.n = 0 then exit(0); |
| 608 | + BigBits := (a.n - 1) * 32; |
| 609 | + t := a.w[a.n - 1]; |
| 610 | + while t <> 0 do begin inc(BigBits); t := t shr 1 end; |
| 611 | +end; |
| 612 | + |
| 613 | +{ s is digits, a point, digits, and maybe E, a sign and digits. False if |
| 614 | + the value is past the largest F64; one below the smallest is 0. } |
| 615 | +function ReadReal(const s: shortstring; out x: double): boolean; |
| 616 | +var |
| 617 | + num, den, d: TBig; i, digits, e, ex: longint; neg, half, sticky: boolean; |
| 618 | + q, b: qword; |
| 619 | +begin |
| 620 | + num.n := 0; den.n := 1; den.w[0] := 1; |
| 621 | + digits := 0; e := 0; i := 1; |
| 622 | + while (i <= length(s)) and (s[i] <> 'E') do begin |
| 623 | + if s[i] = '.' then e := 1 |
| 624 | + else begin |
| 625 | + BigMul(num, 10, ord(s[i]) - ord('0')); |
| 626 | + if num.n > 0 then inc(digits); { counted from the first that isn't 0 } |
| 627 | + if e > 0 then inc(e); |
| 628 | + end; |
| 629 | + inc(i); |
| 630 | + end; |
| 631 | + if e > 0 then e := 1 - e; { minus the digits after the point } |
| 632 | + ex := 0; neg := false; |
| 633 | + if i <= length(s) then begin |
| 634 | + inc(i); |
| 635 | + neg := s[i] = '-'; |
| 636 | + if s[i] in ['+', '-'] then inc(i); |
| 637 | + for i := i to length(s) do |
| 638 | + if ex < 100000 then ex := ex * 10 + ord(s[i]) - ord('0'); |
| 639 | + end; |
| 640 | + if neg then ex := -ex; |
| 641 | + inc(e, ex); |
| 642 | + x := 0.0; |
| 643 | + if num.n = 0 then exit(true); |
| 644 | + if digits + e > 310 then exit(false); { 10^310 is past every F64 } |
| 645 | + if digits + e < -330 then exit(true); { and 10^-330 below half the least } |
| 646 | + for i := 1 to e do BigMul(num, 10, 0); |
| 647 | + for i := 1 to -e do BigMul(den, 10, 0); |
| 648 | + { num / den 2^-e from 2^52 to 2^54, but not past the least exponent } |
| 649 | + e := BigBits(num) - BigBits(den) - 53; |
| 650 | + if e < -1074 then e := -1074; |
| 651 | + if e < 0 then BigShl(num, -e) else BigShl(den, e); |
| 652 | + { q, its whole part, by long division; num is left the remainder } |
| 653 | + d := den; BigShl(d, 53); |
| 654 | + q := 0; |
| 655 | + for i := 53 downto 0 do begin |
| 656 | + if BigCmp(num, d) >= 0 then begin BigSub(num, d); q := q or (qword(1) shl i) end; |
| 657 | + BigShr1(d); |
| 658 | + end; |
| 659 | + if q >= qword(1) shl 53 then begin { a bit too many: it rounds } |
| 660 | + half := odd(q); sticky := num.n > 0; |
| 661 | + q := q shr 1; inc(e); |
| 662 | + end else begin { the remainder against half of den } |
| 663 | + BigShl(num, 1); |
| 664 | + i := BigCmp(num, den); |
| 665 | + half := i >= 0; sticky := i > 0; |
| 666 | + end; |
| 667 | + if half and (sticky or odd(q)) then begin |
| 668 | + inc(q); |
| 669 | + if q = qword(1) shl 53 then begin q := q shr 1; inc(e) end; |
| 670 | + end; |
| 671 | + if e > 971 then exit(false); |
| 672 | + if q >= qword(1) shl 52 then b := qword(e + 1075) shl 52 or (q and (qword(1) shl 52 - 1)) |
| 673 | + else b := q; { below the least normal, e is -1074 } |
| 674 | + Move(b, x, 8); |
| 675 | + ReadReal := true; |
| 676 | +end; |
| 677 | + |
| 532 | 678 | procedure Number; |
| 533 | | -var start, i, d: longint; allDec: boolean; v: qword; s: shortstring; code: word; |
| 679 | +var start, i, d: longint; allDec: boolean; v: qword; s: shortstring; |
| 534 | 680 | begin |
| 535 | 681 | start := cur.pos; |
| 536 | 682 | allDec := true; |
| | @@ -560,8 +706,7 @@ |
| 560 | 706 | if cur.pos - start > 255 then LexError('a number has at most 255 characters'); |
| 561 | 707 | SetLength(s, cur.pos - start); |
| 562 | 708 | Move(cur.buf[start], s[1], cur.pos - start); |
| 563 | | - Val(s, rval, code); |
| 564 | | - if code <> 0 then LexError(Text(start, cur.pos) + ' is out of the range of F64'); |
| 709 | + if not ReadReal(s, rval) then LexError(Text(start, cur.pos) + ' is out of the range of F64'); |
| 565 | 710 | tok := sReal; |
| 566 | 711 | end else begin |
| 567 | 712 | if not allDec then LexError('a hex number ends with H'); |