Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions include/xprec/ddouble.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <cstdint>
#include <iosfwd>
#include <limits>
#include <string>

#include "version.h"

Expand Down Expand Up @@ -297,6 +298,7 @@ bool isinf(DDouble x);
bool isnan(DDouble x);
bool isnormal(DDouble x);
bool iszero(DDouble x);
std::string to_string(DDouble d, size_t nDigits = 34);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is extending the to_string interface. I am not sure that is what we want to do. I think it is better to respect to use << (which can then be used together with a stringstring).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right.


/**
* Gauss-Chebyshev quadrature rule.
Expand Down
62 changes: 62 additions & 0 deletions src/io.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <array>
#include <iostream>
#include <sstream>
#include <cassert>

#ifndef XPREC_API_EXPORT
#define XPREC_API_EXPORT
Expand Down Expand Up @@ -58,4 +59,65 @@ std::ostream &operator<<(std::ostream &out, DDouble x)
return out;
}

XPREC_API_EXPORT
std::string to_string(DDouble d, size_t nDigits){
using namespace std;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do not import the complete std namespace.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why? It's confined, there is no namespace pollution.


//Get some basic cases out of the way.
if(d.hi() == 0.0)
return "0.0";

if(isnan(d.hi()))
return "NaN";

if(isinf(d.hi()))
if(d.hi() > 0.0)
return "Inf";
else
return "-Inf";


//Alright. Get a string started. Handle sign.
string s;

if(d.hi() < 0.0) s += "-";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do not do this.

There is a clang format file in the repo. You can use clang-format -i src/io.cxx to automatically format the file according to the spect.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't do what?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

one-line if's, please split them across lines.

d = abs(d);


//Get the initial tens place and reduce d by it. This way, d becomes digit.nextdigits.
auto n = (int)floor(log10(d.hi()));
if(n < 0) n++;

constexpr DDouble Ten = 10.0;
d /= pow(Ten, n);

//Loop over digits, print them.
nDigits = min(max(nDigits, (size_t)3), (size_t)34);
Comment on lines +94 to +95
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this here?


for(size_t i = 0; i != nDigits; i++){
if(i == 1) s += ".";

//Current digit to print is just the leading one.
auto m = (int)floor(d.hi());
assert(m >= 0 && m < 10);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assertion will occasionally fail due to rounding.


s += std::to_string(m);

//Remove leading digit, promote by ten to retain the digit.nextdigits form.
d = (d - DDouble(m))*Ten;

//Emptied it - nothing more to print.
if(d.hi() == 0.0)
break;

assert(d.hi() > 0.0);
}

//Assuming you care more about the mantissa... always use exponential form. n == 0 imples e0.
if(n != 0)
s += "e" + to_string(n);

return s;
}

} /* namespace xprec */