Skip to content

Commit 3d40280

Browse files
authored
Merge pull request #3140 from sorin-gabriel/sorinm-issue-22527
fix Issue 22527 - Casting out-of-range floating point value to signed…
2 parents 4c001ba + 05cb103 commit 3d40280

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

spec/expression.dd

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1134,7 +1134,9 @@ $(GNAME CastExpression):
11341134
---
11351135

11361136
$(P Casting a floating point value to an integral type is the equivalent
1137-
of converting to an integer using truncation.)
1137+
of converting to an integer using truncation. If the floating point
1138+
value is outside the range of the integral type, the cast will produce
1139+
an invalid result (this is also the case in C, C++).)
11381140

11391141
---
11401142
void main()
@@ -1145,6 +1147,18 @@ $(GNAME CastExpression):
11451147
assert(b == 1L);
11461148
long c = cast(long) -1.5;
11471149
assert(c == -1);
1150+
1151+
// if the float overflows, the cast returns the integer value of
1152+
// 80000000_00000000H (64-bit operand) or 80000000H (32-bit operand)
1153+
long d = cast(long) float.max;
1154+
assert(d == long.min);
1155+
int e = cast(int) (1234.5 + int.max);
1156+
assert(e == int.min);
1157+
1158+
// for types represented on 16 or 8 bits, the result is the same as
1159+
// 32-bit types, but the most significant bits are ignored
1160+
short f = cast(short) float.max;
1161+
assert(f == 0);
11481162
}
11491163
---
11501164

0 commit comments

Comments
 (0)