summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorvalmat <ufabiz@gmail.com>2014-12-15 22:54:12 +0600
committervalmat <ufabiz@gmail.com>2014-12-15 22:54:12 +0600
commitcbe358c2b43961734711e1c3ee2c506b1434c8f3 (patch)
treec5b2e5cf09be40566a6fd454152d7fd9c0db6315
parentda4de6cb1097471b2659c102d55a646cbc9e0f41 (diff)
Fixed issue #149
-rw-r--r--tests/cpp/include/doubl2str.h98
1 files changed, 6 insertions, 92 deletions
diff --git a/tests/cpp/include/doubl2str.h b/tests/cpp/include/doubl2str.h
index 1a5e3fc..66f2b86 100644
--- a/tests/cpp/include/doubl2str.h
+++ b/tests/cpp/include/doubl2str.h
@@ -4,99 +4,13 @@
*
*/
-
-
-char num2char(unsigned int num)
+#include <sstream>
+#include <iomanip>
+std::string double2str(long double d)
{
- switch(num)
- {
- case 0:
- return '0';
- case 1:
- return '1';
- case 2:
- return '2';
- case 3:
- return '3';
- case 4:
- return '4';
- case 5:
- return '5';
- case 6:
- return '6';
- case 7:
- return '7';
- case 8:
- return '8';
- case 9:
- return '9';
- }
-
- //return '\0';
- return '-';
-}
-
-std::string double2str(long double D)
-{
- int sign = (D > 0) ? 1 : (D*=-1.0, -1);
- unsigned long long int Ceil = D;
-
- // if D is ceil
- if(Ceil == D)
- {
- return std::to_string( (long long)(D*sign) );
- }
-
- // size of result buffer
- const int bs = 32;
-
- // size of temporary buffer
- const int pw = 16;
- // Temporary buffer
- char buf[pw];
- // Result buffer
- char rez[bs];
-
- int i, size = 0;
- // set sign
- if(sign < 0) rez[size++] = '-';
-
- // set ceil
- std::string sceil = std::to_string(Ceil);
- const char * bceil = sceil.c_str();
- int sceillen = sceil.size();
- for(i = 0; i < sceillen; i++)
- {
- rez[size++] = bceil[i];
- }
-
- // set point
- rez[size++] = '.';
-
- unsigned long long int I = D * 10000000000000000; // D * 10**pw
- // .14159265359 -> 14159265359000000
- I -= Ceil * 10000000000000000;
-
- // Remove the tail of zeros
- // 14159265359000000 -> 14159265359
- while(0 == I % 10) I /= 10;
-
- int ind = 0;
- while(I > 0)
- {
- buf[ind++] = num2char(I%10);
- I = (I - I%10) / 10;
- }
-
- // set fraction part
- for(i = 0; i < ind; i++)
- {
- rez[size] = buf[ind-i-1];
- size++;
- }
-
- return std::string(rez, size);
- //rez[size] = '\0';
+ std::ostringstream strs;
+ strs << std::setprecision(16) << d;
+ return strs.str();
}