Changeset eb5966 in git for ntl/src/xdouble.c
- Timestamp:
- Sep 14, 2004, 2:00:40 PM (20 years ago)
- Branches:
- (u'fieker-DuVal', '117eb8c30fc9e991c4decca4832b1d19036c4c65')(u'spielwiese', 'b9f50b373314e74e83c7c060a651dd2913e1f033')
- Children:
- 3119025bf9047f84e9ab4e733bd2fdef4b6c422e
- Parents:
- e6caf814a009946e5e934ceda7f851b656e36e41
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
ntl/src/xdouble.c
re6caf81 reb5966 610 610 611 611 return x3; 612 } 613 614 615 616 617 ostream& operator<<(ostream& s, const xdouble& a) 618 { 619 if (a == 0) { 620 s << "0"; 621 return s; 622 } 623 624 long old_p = RR::precision(); 625 long temp_p = long(log(fabs(log(fabs(a))) + 1.0)/log(2.0)) + 10; 626 627 RR::SetPrecision(temp_p); 628 629 RR ln2, ln10, log_2_10; 630 ComputeLn2(ln2); 631 ComputeLn10(ln10); 632 log_2_10 = ln10/ln2; 633 ZZ log_10_a = to_ZZ( 634 (to_RR(a.e)*to_RR(2*NTL_XD_HBOUND_LOG) + log(fabs(a.x))/log(2.0))/log_2_10); 635 636 RR::SetPrecision(old_p); 637 638 xdouble b; 639 long neg; 640 641 if (a < 0) { 642 b = -a; 643 neg = 1; 644 } 645 else { 646 b = a; 647 neg = 0; 648 } 649 650 ZZ k = xdouble::OutputPrecision() - log_10_a; 651 652 xdouble c, d; 653 654 c = PowerOf10(to_ZZ(xdouble::OutputPrecision())); 655 d = PowerOf10(log_10_a); 656 657 b = b / d; 658 b = b * c; 659 660 while (b < c) { 661 b = b * 10.0; 662 k++; 663 } 664 665 while (b >= c) { 666 b = b / 10.0; 667 k--; 668 } 669 670 b = b + 0.5; 671 k = -k; 672 673 ZZ B; 674 conv(B, b); 675 676 long bp_len = xdouble::OutputPrecision()+10; 677 678 char *bp = NTL_NEW_OP char[bp_len]; 679 680 if (!bp) Error("xdouble output: out of memory"); 681 682 long len, i; 683 684 len = 0; 685 do { 686 if (len >= bp_len) Error("xdouble output: buffer overflow"); 687 bp[len] = IntValToChar(DivRem(B, B, 10)); 688 len++; 689 } while (B > 0); 690 691 for (i = 0; i < len/2; i++) { 692 char tmp; 693 tmp = bp[i]; 694 bp[i] = bp[len-1-i]; 695 bp[len-1-i] = tmp; 696 } 697 698 i = len-1; 699 while (bp[i] == '0') i--; 700 701 k += (len-1-i); 702 len = i+1; 703 704 bp[len] = '\0'; 705 706 if (k > 3 || k < -len - 3) { 707 // use scientific notation 708 709 if (neg) s << "-"; 710 s << "0." << bp << "e" << (k + len); 711 } 712 else { 713 long kk = to_long(k); 714 715 if (kk >= 0) { 716 if (neg) s << "-"; 717 s << bp; 718 for (i = 0; i < kk; i++) 719 s << "0"; 720 } 721 else if (kk <= -len) { 722 if (neg) s << "-"; 723 s << "0."; 724 for (i = 0; i < -len-kk; i++) 725 s << "0"; 726 s << bp; 727 } 728 else { 729 if (neg) s << "-"; 730 for (i = 0; i < len+kk; i++) 731 s << bp[i]; 732 733 s << "."; 734 735 for (i = len+kk; i < len; i++) 736 s << bp[i]; 737 } 738 } 739 740 delete [] bp; 741 return s; 742 } 743 744 istream& operator>>(istream& s, xdouble& x) 745 { 746 long c; 747 long cval; 748 long sign; 749 ZZ a, b; 750 751 if (!s) Error("bad xdouble input"); 752 753 c = s.peek(); 754 while (IsWhiteSpace(c)) { 755 s.get(); 756 c = s.peek(); 757 } 758 759 if (c == '-') { 760 sign = -1; 761 s.get(); 762 c = s.peek(); 763 } 764 else 765 sign = 1; 766 767 long got1 = 0; 768 long got_dot = 0; 769 long got2 = 0; 770 771 a = 0; 772 b = 1; 773 774 cval = CharToIntVal(c); 775 776 if (cval >= 0 && cval <= 9) { 777 got1 = 1; 778 779 while (cval >= 0 && cval <= 9) { 780 mul(a, a, 10); 781 add(a, a, cval); 782 s.get(); 783 c = s.peek(); 784 cval = CharToIntVal(c); 785 } 786 } 787 788 if (c == '.') { 789 got_dot = 1; 790 791 s.get(); 792 c = s.peek(); 793 cval = CharToIntVal(c); 794 795 if (cval >= 0 && cval <= 9) { 796 got2 = 1; 797 798 while (cval >= 0 && cval <= 9) { 799 mul(a, a, 10); 800 add(a, a, cval); 801 mul(b, b, 10); 802 s.get(); 803 c = s.peek(); 804 cval = CharToIntVal(c); 805 } 806 } 807 } 808 809 if (got_dot && !got1 && !got2) Error("bad xdouble input"); 810 811 ZZ e; 812 813 long got_e = 0; 814 long e_sign; 815 816 if (c == 'e' || c == 'E') { 817 got_e = 1; 818 819 s.get(); 820 c = s.peek(); 821 822 if (c == '-') { 823 e_sign = -1; 824 s.get(); 825 c = s.peek(); 826 } 827 else if (c == '+') { 828 e_sign = 1; 829 s.get(); 830 c = s.peek(); 831 } 832 else 833 e_sign = 1; 834 835 cval = CharToIntVal(c); 836 837 if (cval < 0 || cval > 9) Error("bad xdouble input"); 838 839 e = 0; 840 while (cval >= 0 && cval <= 9) { 841 mul(e, e, 10); 842 add(e, e, cval); 843 s.get(); 844 c = s.peek(); 845 cval = CharToIntVal(c); 846 } 847 } 848 849 if (!got1 && !got2 && !got_e) Error("bad xdouble input"); 850 851 xdouble t1, t2, v; 852 853 if (got1 || got2) { 854 conv(t1, a); 855 conv(t2, b); 856 v = t1/t2; 857 } 858 else 859 v = 1; 860 861 if (sign < 0) 862 v = -v; 863 864 if (got_e) { 865 if (e_sign < 0) negate(e, e); 866 t1 = PowerOf10(e); 867 v = v * t1; 868 } 869 870 x = v; 871 return s; 612 872 } 613 873
Note: See TracChangeset
for help on using the changeset viewer.