Union of fixed and floating point types
Say I have two real number types. They may be floating or fixed point. How can I construct a new type whose values are at least the union of the two with the minimal number of bits.
There are 3 cases to consider:
Fixed (Qa.x) U Fixed (Qb.y) - I think the best here is to use Qmax(a,b).max(x,y). I think this is optimal since I can't come up with anything smaller that will accurately represent the type.
Float (FaEx) U Float (FbEy) - I think the best here is to use Fmax(a,b)Emax(x,y). Again I can't think of a more optimal solution. (I don't know how floating point types are typically represented, I'm using an analogous representation where FaEb means a bits of mantissa and b bits of exponent)
The difficult case is:
Fixed (Qa.x) U Float (FbEy) - The best I can come up with is Qmax(a,n).max(x,m) where n is the minimal bits to represent the biggest number the float can be and m is the minimal number of bits to represent the smallest positive fraction the float can be. This seems extremely inefficient as it extends the floating point's most accurate precision to its entire range. Thus for any decent sized floating point type the resulting union type will be extremely large.
Here are some ASCII diagrams of the three cases (simplified), and why I think I'm wrong:
0/4 1/4 2/4 3/4 4/4 5/4 6/4 7/4 8/4 9/4 10/4 11/4 12/4 13/<PHONE_NUMBER>
| . v . | . v . | . v . | . v . | . v . | . v . | . v . | . v . | . v . | . v . | . v . | . v . | . v . | . v . | . v . | . v . |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Q1.2 |.......|.......|.......|.......|.......|.......|.......|........................................................................
U .................................................................................................................................
Q0.3 |...|...|...|...|...|...|...|....................................................................................................
= .................................................................................................................................
Q1.3 |...|...|...|...|...|...|...|...|...|...|...|...|...|...|........................................................................
0/4 1/4 2/4 3/4 4/4 5/4 6/4 7/4 8/4 9/4 10/4 11/4 12/4 13/<PHONE_NUMBER>
| . v . | . v . | . v . | . v . | . v . | . v . | . 655
v . | . v . | . v . | . v . | . v . | . v . | . v . | . v . | . v . | . v . |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
F1E2 ........|...|...|.......|.......|...............|...............|...............................|................................
U .................................................................................................................................
F2E1 ................|...|...|...|...|.......|.......|.......|........................................................................
= .................................................................................................................................
F2E2 ................................|.......|.......|.......|.......|...............|...............|...............|................
0/4 1/4 2/4 3/4 4/4 5/4 6/4 7/4 8/4 9/4 10/4 11/4 12/4 13/<PHONE_NUMBER>
| . v . | . v . | . v . | . v . | . v . | . v . | . v . | . v . | . v . | . v . | . v . | . v . | . v . | . v . | . v . | . v . |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Q0.3 |...|...|...|...|...|...|...|....................................................................................................
U .................................................................................................................................
F1E2 ........|...|...|.......|.......|...............|...............|...............................|................................
= .................................................................................................................................
Q2.3 |...|...|...|...|...|...|...|...|...|...|...|...|...|...|...|...|...|...|...|...|...|...|...|...|...|...|...|...|...|...|........
F? 268