Skip to content

Provide formatting and printing functions #89

@Pharap

Description

@Pharap

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.

Metadata

Metadata

Assignees

Labels

Feature RequestThis issue is a request for a particular featureMinorThis change is a minor addition

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions