Conversions with to
There are three ways to change a domain, and each has one meaning:
| Syntax | Meaning | Runtime cost |
|---|---|---|
| implicit | Widening, or a range the checker proved | none |
x as T | Assertion: x is already in T | one check |
x to T | Conversion: changes the values | the conversion itself |
The to table
A to with a domain makes one of these conversions, chosen from x’s type:
| From | To | Operation | Matches |
|---|---|---|---|
u8 | Unit | $x / 255$ | a u8 dtype, any domain |
Unit | Signed | $2x - 1$ | a float range within $[0, 1]$ |
Signed | Unit | $(x + 1) / 2$ | a float range within $[-1, 1]$ but not within $[0, 1]$ |
Mask | Binary | float cast | Mask |
Mask | Additive | 0 where true, $-\infty$ where false | Mask |
Logit over C | Prob over C | softmax over C | anything that fits Logit over C, including plain numbers |
Logit over C | LogProb over C | log-softmax over C | as above |
Anything else is E0504, which lists the table. That includes semantic and discrete domains matched against range rows by their values (Prob to Signed; assert first, p as Unit |> to Signed), and converting to the domain a value already has (“nothing to convert”).
u8 to Unit and Mask to Binary or Additive give the default float dtype; the others keep the float dtype. A to may also write a dtype, as in to Unit f16, which must be a float (E0504). A to with a domain on a Python value is E0413, because the conversion depends on a type the value doesn’t have yet. The shape in to [b C] Unit, if given, must be x’s (E0401).
Converting dtypes
A to with only a dtype changes how values are stored and keeps them:
boolto a float givesBinary, and to an integer gives $[0, 1]$;- an integer to a float keeps the range; a float to an integer truncates it;
- a float to a lower-precision float rounds the range’s ends outward;
- anything but
booltoboolis E0504: compare instead, as inx != 0.
A dtype-only to on a Python value is allowed and leaves it a Python value.