Skip to content
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

Implement string conversion methods #12

Open
Cat-Ion opened this issue Jul 31, 2017 · 3 comments
Open

Implement string conversion methods #12

Cat-Ion opened this issue Jul 31, 2017 · 3 comments

Comments

@Cat-Ion
Copy link
Owner

Cat-Ion commented Jul 31, 2017

Both MultiwordInt and FixedPoint should have methods to convert them to strings, possibly with the usual floating-point modifiers for FixedPoint (i.e. precision and printing as scientific/engineering/...)

@dmitvitalii
Copy link

Hi. I want to take this issue.

@Cat-Ion
Copy link
Owner Author

Cat-Ion commented Sep 28, 2017

Awesome! I think the best way to do this here would be to have to_string() methods on both classes that each return a std::string - std::to_string doesn't allow you to have options like the precision, and using a std::stringstream feels a bit heavy.

Something like

MultiwordInteger::to_string() const;
FixedPoint::to_string(unsigned precision);

should work fine, possibly with some optional enum parameter for the FixedPoint version that lets you select scientific/engineering/etc notations, like the %e, %f, %g specifiers for printf, if you don't think that it's too much work. Maybe do it in a second pull request :)

I haven't thought about the implementation details too much, but I'd use a (for MultiwordInteger) char[size/3+2] as buffer and create and return an std::string from that.

For FixedPoint numbers, the needed string length should not be larger than integralWidth/3+fractionalWidth+2.

I'd put the code in the *Utility.h files. Don't worry too much about the style, I'm probably inconsistent myself and if I don't like it I'll just change stuff a bit before I merge the PR.

Please do let me know if anything's unclear :)

@Cat-Ion
Copy link
Owner Author

Cat-Ion commented Sep 28, 2017

... The entries in MultiwordInteger::v are little-endian, i.e. with uint8_t as backing storage type 256 would be stored as [0, 1].

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants