Commit d6f40ce
authored
[Variant] Allow lossless casting from integer to floating point (#8357)
# Which issue does this PR close?
We generally require a GitHub issue to be filed for all bug fixes and
enhancements and this helps us generate change logs for our releases.
You can link an issue to this PR using the GitHub syntax.
- Closes #NNN.
# Rationale for this change
Historically, `Variant::as_fXX` methods don't even try to cast int
values as floating point, which is counter-intuitive.
# What changes are included in this PR?
Allow lossless casting of variant integer values to variant floating
point values, by a naive determination of precision:
* Every floating point number has some number of bits of precision
* 53 (double)
* 24 (single)
* 11 (half)
* Any integer that fits entirely inside the target floating point type's
precision can be converted losslessly
* This produces an intuitive result: "too big" numbers fail to convert,
while "small enough" numbers do convert.
* This is a sufficient but _not_ a necessary condition.
* Technically, wider integer can be represented losslessly as well, as
long as they have enough trailing zeros
* It's unclear whether allowing those wider values to cast is actually
helpful in practice, because only 1 in 2**k values can cast (where k is
the number of bits of excess precision); it would certainly make input
testing more expensive.
# Are these changes tested?
New unit tests and doc tests.
# Are there any user-facing changes?
Yes. Values that failed to cast before now succeed.1 parent d74d9ba commit d6f40ce
2 files changed
+54
-13
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
144 | 144 | | |
145 | 145 | | |
146 | 146 | | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
28 | 28 | | |
29 | 29 | | |
30 | 30 | | |
31 | | - | |
| 31 | + | |
32 | 32 | | |
33 | 33 | | |
34 | 34 | | |
| |||
1082 | 1082 | | |
1083 | 1083 | | |
1084 | 1084 | | |
1085 | | - | |
1086 | | - | |
| 1085 | + | |
| 1086 | + | |
1087 | 1087 | | |
1088 | 1088 | | |
1089 | 1089 | | |
| |||
1099 | 1099 | | |
1100 | 1100 | | |
1101 | 1101 | | |
| 1102 | + | |
| 1103 | + | |
| 1104 | + | |
| 1105 | + | |
1102 | 1106 | | |
1103 | | - | |
1104 | | - | |
| 1107 | + | |
| 1108 | + | |
1105 | 1109 | | |
1106 | 1110 | | |
1107 | 1111 | | |
1108 | 1112 | | |
| 1113 | + | |
| 1114 | + | |
| 1115 | + | |
| 1116 | + | |
1109 | 1117 | | |
1110 | 1118 | | |
1111 | 1119 | | |
1112 | 1120 | | |
1113 | 1121 | | |
1114 | 1122 | | |
1115 | | - | |
1116 | | - | |
| 1123 | + | |
| 1124 | + | |
1117 | 1125 | | |
1118 | 1126 | | |
1119 | 1127 | | |
| |||
1128 | 1136 | | |
1129 | 1137 | | |
1130 | 1138 | | |
| 1139 | + | |
| 1140 | + | |
| 1141 | + | |
| 1142 | + | |
1131 | 1143 | | |
1132 | | - | |
1133 | | - | |
| 1144 | + | |
| 1145 | + | |
1134 | 1146 | | |
1135 | 1147 | | |
1136 | 1148 | | |
1137 | 1149 | | |
1138 | 1150 | | |
1139 | 1151 | | |
| 1152 | + | |
| 1153 | + | |
| 1154 | + | |
| 1155 | + | |
1140 | 1156 | | |
1141 | 1157 | | |
1142 | 1158 | | |
1143 | 1159 | | |
1144 | 1160 | | |
1145 | 1161 | | |
1146 | | - | |
1147 | | - | |
| 1162 | + | |
| 1163 | + | |
1148 | 1164 | | |
1149 | 1165 | | |
1150 | 1166 | | |
| |||
1159 | 1175 | | |
1160 | 1176 | | |
1161 | 1177 | | |
| 1178 | + | |
| 1179 | + | |
| 1180 | + | |
| 1181 | + | |
1162 | 1182 | | |
1163 | | - | |
1164 | | - | |
| 1183 | + | |
| 1184 | + | |
1165 | 1185 | | |
1166 | 1186 | | |
1167 | 1187 | | |
1168 | 1188 | | |
1169 | 1189 | | |
| 1190 | + | |
| 1191 | + | |
| 1192 | + | |
| 1193 | + | |
1170 | 1194 | | |
1171 | 1195 | | |
1172 | 1196 | | |
| |||
0 commit comments