eteran/cpp-utilities

Change type after multiple?

zrobotparking opened this issue · 2 comments

What should I do if I want to increase bits after multiple?
like this:

Fixed<8,8>  a8,b8;
Fixed<16,16>  result16;
a8=250;
b8=250;
result16 = a8 * b8;

Hmm, good question. I don't think that the library can currently do this easily, but if the math supports doing such an operation efficiently, then I'll add something to the API for it.

The difficulty is that internally in a Fixed<8,8>:

250 is represented as uint16_t(64000)
while in a Fixed<16,16>, 250 is represented as uint32_t(16384000)

So before the op, things need to be scaled. I'll look into it.

OK, I think I have a solution for you :-).

I have just checked in a version which has a scaling constructor. You can use it like this:

// these are just for simplicity
using F8  = Fixed<8,8>
using F16 = Fixed<16,16>

F8 a8 = 250;
F8 b8 = 250;

// scale the input values to a larger type so the result can fit!
F16 result16 = F16(a8) * F16(b8);