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

Provide formatting and printing functions #89

Open
Pharap opened this issue Mar 26, 2021 · 0 comments
Open

Provide formatting and printing functions #89

Pharap opened this issue Mar 26, 2021 · 0 comments
Assignees
Labels
Feature Request This issue is a request for a particular feature Minor This change is a minor addition

Comments

@Pharap
Copy link
Owner

Pharap commented Mar 26, 2021

I'm getting a little fed up with having to put static_cast<float> everywhere when debugging so I'd like to find a means to print fixed points.

Implementing printing in fractional form should be pretty straightforward in most cases.
Something like this ought to work for most cases:

printer.print(fixed.getInteger());
printer.print(F(" "));
printer.print(fixed.getFraction());
printer.print(F("/");
printer.print(1ul << FractionSize);

Since inheriting Arduino's Printable would incur a ridiculously unnecessary overhead (2 bytes per object to provide the pointer to the virtual table) I'll most likely either use named functions (e.g. printFractional(printer, fixed)) or make use of << as an output operator in the style of C++'s std::ostream.

Being able to provide printing in a decimal format would be ideal, but at present I'm not sure how to do that beyond casting to float, so I may have to abuse that as a stop-gap. Hopefully I'll have time to research how floating point printing works to see if I can learn anything useful that could be applied to printing fixed points in a decimal format.

If I can't manage to find a way to implement decimal format printing then I'll just stick to the fractional representation because it's easy to implement, relatively cheap (it's mostly just bit shifting and integer printing) and reasonably easy to understand.

@Pharap Pharap added Feature Request This issue is a request for a particular feature Minor This change is a minor addition labels Mar 26, 2021
@Pharap Pharap self-assigned this Mar 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature Request This issue is a request for a particular feature Minor This change is a minor addition
Projects
None yet
Development

No branches or pull requests

1 participant