NumPy: The dtype of a result

Apply NumPy's upcasting rules to predict the dtype of a + b.

What to build

A program that prints the result dtype for pairs of dtypes.

Requirements

  • Types, narrowest first: bool, int8, int16, int32, int64, float32, float64. The result is the wider of the two, except that an int32 or int64 with float32 gives float64 (while int8 or int16 with float32 gives float32).
  • Print A + B -> R for each pair.

Input

Line 1: n. Then n lines, each two dtypes.

Output

n lines.

Constraints

1 ≤ n ≤ 30.

Example

Input:

3
int8 int64
bool int16
int64 float64

Output:

int8 + int64 -> int64
bool + int16 -> int16
int64 + float64 -> float64

Why it works

Upcasting picks a type that can hold both operands; float32 has only 24 bits of precision, so it cannot hold every 32- or 64-bit integer and float64 is used instead.

Examples

Example 1 · Input
3
int8 int64
bool int16
int64 float64
Expected output
int8 + int64 -> int64
bool + int16 -> int16
int64 + float64 -> float64
Example 2 · Input
2
int16 float32
int32 float32
Expected output
int16 + float32 -> float32
int32 + float32 -> float64

Prompts inside input() are ignored when checking, so input("n: ") is fine.

Hints

Hint 1

Rank the types: bool, int8, int16, int32, int64, float32, float64.

Hint 2

Handle the float32 special case before taking the wider type.

Solution

Show one correct solution (try it yourself first)
ORDER = ["bool", "int8", "int16", "int32", "int64", "float32", "float64"]

def result_type(a, b):
    pair = {a, b}
    if "float32" in pair and pair & {"int32", "int64"}:
        return "float64"
    return max(a, b, key=ORDER.index)

n = int(input())
for _ in range(n):
    a, b = input().split()
    print(f"{a} + {b} -> {result_type(a, b)}")

Lesson: Data types: dtype →