-
Notifications
You must be signed in to change notification settings - Fork 3
Add to_string, prints a DDouble in the %e style. #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: mainline
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ | |
| #include <array> | ||
| #include <iostream> | ||
| #include <sstream> | ||
| #include <cassert> | ||
|
|
||
| #ifndef XPREC_API_EXPORT | ||
| #define XPREC_API_EXPORT | ||
|
|
@@ -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; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please do not import the complete
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 += "-"; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't do what?
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 */ | ||
There was a problem hiding this comment.
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_stringinterface. 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).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right.