Skip to content

Commit

Permalink
Merge pull request xtensor-stack#253 from wolfv/xio_fix
Browse files Browse the repository at this point in the history
fix xio erasing of zero in complex printing
  • Loading branch information
SylvainCorlay authored Apr 7, 2017
2 parents b4ac40d + db3989d commit f9c9640
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 5 deletions.
18 changes: 13 additions & 5 deletions include/xtensor/xio.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,13 @@ namespace xt
}
else
{
// 3 => sign and dot and + 1 (from calculation for exponent)
m_width = 3 + (precision_type)std::log10(std::floor(m_max)) + m_precision;
precision_type decimals = 1; // print a leading 0
if (std::floor(m_max) != 0)
{
decimals += (precision_type) std::log10(std::floor(m_max));
}
// 2 => sign and dot
m_width = 2 + decimals + m_precision;
}
if (!m_required_precision)
{
Expand Down Expand Up @@ -310,7 +315,7 @@ namespace xt
{
if (!m_scientific || !m_large_exponent)
{
int exponent = 1 + (int)std::log10(std::fabs(val));
int exponent = 1 + (int)std::log10(std::abs(val));
if (exponent <= -5 || exponent > 7)
{
m_scientific = true;
Expand Down Expand Up @@ -491,7 +496,10 @@ namespace xt
std::stringstream buf;
imag_printer.print_next(buf);
std::string s = buf.str();
s.erase(0, 1); // erase space for +/-
if (s[0] == ' ')
{
s.erase(0, 1); // erase space for +/-
}
// insert j at end of number
std::size_t idx = s.find_last_not_of(" ");
s.insert(idx + 1, "j");
Expand All @@ -503,7 +511,7 @@ namespace xt
void update(const value_type& val)
{
real_printer.update(val.real());
imag_printer.update(std::fabs(val.imag()));
imag_printer.update(std::abs(val.imag()));
m_signs.push_back(std::signbit(val.imag()));
}

Expand Down
64 changes: 64 additions & 0 deletions test/files/xio_expected_results.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,28 @@ static std::string random_strings = R"xio({{ some, random,
{ strings, in, xtensor xarray}})xio";


static std::string float_leading_zero = R"xio({{ 0.212955, -0.071529, 0.190885, 0.21915 , -0.008881, 0.280028,
-0.089076, 0.079694, -0.360049, -0.098982},
{ 0.127317, -0.175849, -0.255241, 0.194755, 0.093902, 0.131792,
-0.059743, -0.416274, 0.21233 , -0.072137},
{-0.20222 , -0.007915, 0.240296, -0.142271, -0.08279 , 0.154721,
-0.126199, -0.265487, 0.487995, 0.265996},
{ 0.277004, -0.472018, -0.326093, -0.345918, -0.422914, 0.389866,
0.250379, 0.193403, 0.011763, -0.035732},
{ 0.068431, -0.197451, -0.002691, 0.183263, 0.416699, -0.391071,
-0.004508, -0.267164, -0.063139, 0.251543},
{-0.019108, 0.297728, -0.217297, -0.066582, -0.490243, -0.159204,
0.189272, 0.369369, -0.232196, -0.043252},
{-0.231719, 0.337053, -0.229485, 0.030062, -0.324627, -0.185034,
0.391109, -0.319664, -0.005684, -0.287702},
{ 0.020877, -0.3399 , 0.419057, 0.49429 , -0.022952, -0.190116,
0.007204, -0.219207, 0.263837, -0.391458},
{ 0.011655, 0.409769, -0.281624, -0.136896, 0.354973, 0.211392,
-0.107056, -0.268699, -0.119825, 0.049162},
{ 0.056719, -0.495865, 0.138023, -0.442352, -0.456973, 0.375051,
-0.207412, 0.262768, -0.132135, 0.373502}})xio";


static std::string cut_high = R"xio({{1},
{1},
{1},
Expand Down Expand Up @@ -505,3 +527,45 @@ static std::string custom_formatter_result = R"xio({{ 0x1, 0x2, 0x
{ 0x64, 0xc8, 0x3e8, 0x989680}})xio";


static std::string complex_zero_erasing = R"xio({{ 0.212955-0.470576j, -0.071529+0.052044j, 0.190885-0.259752j,
0.21915 +0.384805j, -0.008881-0.039762j, 0.280028-0.30683j ,
-0.089076-0.206307j, 0.079694+0.317928j, -0.360049+0.059487j,
-0.098982+0.177925j},
{ 0.127317+0.309127j, -0.175849+0.368572j, -0.255241-0.082008j,
0.194755-0.441062j, 0.093902-0.021541j, 0.131792+0.021159j,
-0.059743+0.080632j, -0.416274-0.190801j, 0.21233 +0.419883j,
-0.072137+0.155348j},
{-0.20222 -0.150759j, -0.007915+0.041094j, 0.240296-0.050947j,
-0.142271-0.217679j, -0.08279 -0.204123j, 0.154721+0.063481j,
-0.126199+0.215191j, -0.265487+0.017619j, 0.487995-0.147958j,
0.265996+0.132779j},
{ 0.277004+0.373119j, -0.472018-0.332112j, -0.326093+0.487478j,
-0.345918-0.150585j, -0.422914+0.326205j, 0.389866-0.434538j,
0.250379-0.494647j, 0.193403+0.386945j, 0.011763+0.411318j,
-0.035732-0.300592j},
{ 0.068431-0.472449j, -0.197451+0.317335j, -0.002691-0.258172j,
0.183263-0.443917j, 0.416699-0.432439j, -0.391071-0.026128j,
-0.004508+0.009429j, -0.267164-0.083205j, -0.063139-0.244157j,
0.251543-0.118488j},
{-0.019108+0.307384j, 0.297728+0.311039j, -0.217297+0.186187j,
-0.066582+0.492614j, -0.490243-0.094815j, -0.159204-0.309826j,
0.189272-0.011826j, 0.369369+0.00237j , -0.232196-0.096645j,
-0.043252-0.318905j},
{-0.231719-0.45197j , 0.337053+0.104691j, -0.229485-0.234089j,
0.030062+0.381347j, -0.324627+0.084046j, -0.185034-0.02254j ,
0.391109-0.328485j, -0.319664-0.44338j , -0.005684-0.342965j,
-0.287702-0.161383j},
{ 0.020877-0.059357j, -0.3399 +0.183843j, 0.419057-0.294128j,
0.49429 +0.116434j, -0.022952+0.424472j, -0.190116-0.420372j,
0.007204-0.014603j, -0.219207+0.198564j, 0.263837-0.418432j,
-0.391458+0.369102j},
{ 0.011655-0.066894j, 0.409769-0.00567j , -0.281624-0.00426j ,
-0.136896-0.129278j, 0.354973+0.422129j, 0.211392+0.341278j,
-0.107056+0.155526j, -0.268699-0.037315j, -0.119825+0.065011j,
0.049162-0.076084j},
{ 0.056719-0.211788j, -0.495865-0.236019j, 0.138023+0.190738j,
-0.442352+0.332837j, -0.456973+0.092392j, 0.375051-0.033477j,
-0.207412+0.17366j , 0.262768-0.108521j, -0.132135+0.202104j,
0.373502+0.465698j}})xio";


40 changes: 40 additions & 0 deletions test/files/xio_expected_results/complex_zero_erasing.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{{ 0.212955-0.470576j, -0.071529+0.052044j, 0.190885-0.259752j,
0.21915 +0.384805j, -0.008881-0.039762j, 0.280028-0.30683j ,
-0.089076-0.206307j, 0.079694+0.317928j, -0.360049+0.059487j,
-0.098982+0.177925j},
{ 0.127317+0.309127j, -0.175849+0.368572j, -0.255241-0.082008j,
0.194755-0.441062j, 0.093902-0.021541j, 0.131792+0.021159j,
-0.059743+0.080632j, -0.416274-0.190801j, 0.21233 +0.419883j,
-0.072137+0.155348j},
{-0.20222 -0.150759j, -0.007915+0.041094j, 0.240296-0.050947j,
-0.142271-0.217679j, -0.08279 -0.204123j, 0.154721+0.063481j,
-0.126199+0.215191j, -0.265487+0.017619j, 0.487995-0.147958j,
0.265996+0.132779j},
{ 0.277004+0.373119j, -0.472018-0.332112j, -0.326093+0.487478j,
-0.345918-0.150585j, -0.422914+0.326205j, 0.389866-0.434538j,
0.250379-0.494647j, 0.193403+0.386945j, 0.011763+0.411318j,
-0.035732-0.300592j},
{ 0.068431-0.472449j, -0.197451+0.317335j, -0.002691-0.258172j,
0.183263-0.443917j, 0.416699-0.432439j, -0.391071-0.026128j,
-0.004508+0.009429j, -0.267164-0.083205j, -0.063139-0.244157j,
0.251543-0.118488j},
{-0.019108+0.307384j, 0.297728+0.311039j, -0.217297+0.186187j,
-0.066582+0.492614j, -0.490243-0.094815j, -0.159204-0.309826j,
0.189272-0.011826j, 0.369369+0.00237j , -0.232196-0.096645j,
-0.043252-0.318905j},
{-0.231719-0.45197j , 0.337053+0.104691j, -0.229485-0.234089j,
0.030062+0.381347j, -0.324627+0.084046j, -0.185034-0.02254j ,
0.391109-0.328485j, -0.319664-0.44338j , -0.005684-0.342965j,
-0.287702-0.161383j},
{ 0.020877-0.059357j, -0.3399 +0.183843j, 0.419057-0.294128j,
0.49429 +0.116434j, -0.022952+0.424472j, -0.190116-0.420372j,
0.007204-0.014603j, -0.219207+0.198564j, 0.263837-0.418432j,
-0.391458+0.369102j},
{ 0.011655-0.066894j, 0.409769-0.00567j , -0.281624-0.00426j ,
-0.136896-0.129278j, 0.354973+0.422129j, 0.211392+0.341278j,
-0.107056+0.155526j, -0.268699-0.037315j, -0.119825+0.065011j,
0.049162-0.076084j},
{ 0.056719-0.211788j, -0.495865-0.236019j, 0.138023+0.190738j,
-0.442352+0.332837j, -0.456973+0.092392j, 0.375051-0.033477j,
-0.207412+0.17366j , 0.262768-0.108521j, -0.132135+0.202104j,
0.373502+0.465698j}}
20 changes: 20 additions & 0 deletions test/files/xio_expected_results/float_leading_zero.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{{ 0.212955, -0.071529, 0.190885, 0.21915 , -0.008881, 0.280028,
-0.089076, 0.079694, -0.360049, -0.098982},
{ 0.127317, -0.175849, -0.255241, 0.194755, 0.093902, 0.131792,
-0.059743, -0.416274, 0.21233 , -0.072137},
{-0.20222 , -0.007915, 0.240296, -0.142271, -0.08279 , 0.154721,
-0.126199, -0.265487, 0.487995, 0.265996},
{ 0.277004, -0.472018, -0.326093, -0.345918, -0.422914, 0.389866,
0.250379, 0.193403, 0.011763, -0.035732},
{ 0.068431, -0.197451, -0.002691, 0.183263, 0.416699, -0.391071,
-0.004508, -0.267164, -0.063139, 0.251543},
{-0.019108, 0.297728, -0.217297, -0.066582, -0.490243, -0.159204,
0.189272, 0.369369, -0.232196, -0.043252},
{-0.231719, 0.337053, -0.229485, 0.030062, -0.324627, -0.185034,
0.391109, -0.319664, -0.005684, -0.287702},
{ 0.020877, -0.3399 , 0.419057, 0.49429 , -0.022952, -0.190116,
0.007204, -0.219207, 0.263837, -0.391458},
{ 0.011655, 0.409769, -0.281624, -0.136896, 0.354973, 0.211392,
-0.107056, -0.268699, -0.119825, 0.049162},
{ 0.056719, -0.495865, 0.138023, -0.442352, -0.456973, 0.375051,
-0.207412, 0.262768, -0.132135, 0.373502}}
20 changes: 20 additions & 0 deletions test/test_xio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,26 @@ namespace xt
EXPECT_EQ(complex_numbers, out.str());
}

TEST(xio, complex_zero_erasing)
{
xt::random::seed(123);
xt::xarray<double> real = xt::random::rand<double>({10, 10}) - 0.5;
xt::xarray<double> imag = xt::random::rand<double>({10, 10}) - 0.5;
xt::xarray<std::complex<double>> e = real + (imag * std::complex<double>(0, 1));

std::stringstream out;
out << e;
EXPECT_EQ(complex_zero_erasing, out.str());
}

TEST(xio, float_leading_zero)
{
xt::random::seed(123);
std::stringstream out;
out << xt::random::rand<double>({10, 10}) - 0.5;
EXPECT_EQ(float_leading_zero, out.str());
}

TEST(xio, custom_formatter)
{
xt::xarray<int> e = {{1,2,3,4}, {100, 200, 1000, 10000000}};
Expand Down

0 comments on commit f9c9640

Please sign in to comment.