Macaulay2 Engine
Loading...
Searching...
No Matches

◆ toString() [2/2]

std::string mpfr::mpreal::toString ( int n = -1,
int b = 10,
mp_rnd_t mode = mpreal::get_default_rnd() ) const
inline

Definition at line 1799 of file mpreal.h.

1800{
1801 // TODO: Add extended format specification (f, e, rounding mode) as it done in output operator
1802 (void)b;
1803 (void)mode;
1804
1805#if (MPFR_VERSION >= MPFR_VERSION_NUM(2,4,0))
1806
1807 std::ostringstream format;
1808
1809 int digits = (n >= 0) ? n : 2 + bits2digits(mpfr_get_prec(mpfr_srcptr()));
1810
1811 format << "%." << digits << "RNg";
1812
1813 return toString(format.str());
1814
1815#else
1816
1817 char *s, *ns = NULL;
1818 size_t slen, nslen;
1819 mp_exp_t exp;
1820 std::string out;
1821
1822 if(mpfr_inf_p(mp))
1823 {
1824 if(mpfr_sgn(mp)>0) return "+Inf";
1825 else return "-Inf";
1826 }
1827
1828 if(mpfr_zero_p(mp)) return "0";
1829 if(mpfr_nan_p(mp)) return "NaN";
1830
1831 s = mpfr_get_str(NULL, &exp, b, 0, mp, mode);
1832 ns = mpfr_get_str(NULL, &exp, b, (std::max)(0,n), mp, mode);
1833
1834 if(s!=NULL && ns!=NULL)
1835 {
1836 slen = strlen(s);
1837 nslen = strlen(ns);
1838 if(nslen<=slen)
1839 {
1840 mpfr_free_str(s);
1841 s = ns;
1842 slen = nslen;
1843 }
1844 else {
1845 mpfr_free_str(ns);
1846 }
1847
1848 // Make human eye-friendly formatting if possible
1849 if (exp>0 && static_cast<size_t>(exp)<slen)
1850 {
1851 if(s[0]=='-')
1852 {
1853 // Remove zeros starting from right end
1854 char* ptr = s+slen-1;
1855 while (*ptr=='0' && ptr>s+exp) ptr--;
1856
1857 if(ptr==s+exp) out = std::string(s,exp+1);
1858 else out = std::string(s,exp+1)+'.'+std::string(s+exp+1,ptr-(s+exp+1)+1);
1859
1860 //out = string(s,exp+1)+'.'+string(s+exp+1);
1861 }
1862 else
1863 {
1864 // Remove zeros starting from right end
1865 char* ptr = s+slen-1;
1866 while (*ptr=='0' && ptr>s+exp-1) ptr--;
1867
1868 if(ptr==s+exp-1) out = std::string(s,exp);
1869 else out = std::string(s,exp)+'.'+std::string(s+exp,ptr-(s+exp)+1);
1870
1871 //out = string(s,exp)+'.'+string(s+exp);
1872 }
1873
1874 }else{ // exp<0 || exp>slen
1875 if(s[0]=='-')
1876 {
1877 // Remove zeros starting from right end
1878 char* ptr = s+slen-1;
1879 while (*ptr=='0' && ptr>s+1) ptr--;
1880
1881 if(ptr==s+1) out = std::string(s,2);
1882 else out = std::string(s,2)+'.'+std::string(s+2,ptr-(s+2)+1);
1883
1884 //out = string(s,2)+'.'+string(s+2);
1885 }
1886 else
1887 {
1888 // Remove zeros starting from right end
1889 char* ptr = s+slen-1;
1890 while (*ptr=='0' && ptr>s) ptr--;
1891
1892 if(ptr==s) out = std::string(s,1);
1893 else out = std::string(s,1)+'.'+std::string(s+1,ptr-(s+1)+1);
1894
1895 //out = string(s,1)+'.'+string(s+1);
1896 }
1897
1898 // Make final string
1899 if(--exp)
1900 {
1901 if(exp>0) out += "e+"+mpfr::toString<mp_exp_t>(exp,std::dec);
1902 else out += "e"+mpfr::toString<mp_exp_t>(exp,std::dec);
1903 }
1904 }
1905
1906 mpfr_free_str(s);
1907 return out;
1908 }else{
1909 return "conversion error!";
1910 }
1911#endif
1912}
::mpfr_srcptr mpfr_srcptr() const
Definition mpreal.h:1767
friend const mpreal exp(const mpreal &v, mp_rnd_t rnd_mode)
Definition mpreal.h:2298
void size_t s
Definition m2-mem.cpp:271
std::ostringstream & toString(std::ostringstream &o, int len, int *p)
int bits2digits(mp_prec_t b)
Definition mpreal.h:1969

References mpfr::bits2digits(), exp, mp, mpfr_srcptr(), s, toString(), and toString().

Referenced by toString(), and toString().