You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Migrating this here after a bit of discussion in Discord.
It seems like casting to unsigned integers actually just casts to signed integers, but has different behaviour in different cases.
I.e. I would expect that if I took a UInt32 value, set it to 129, cast it to UInt8 and then call int(), I would still get 129.
However, this seem to end up as 4294967169, i.e. 2**32 - 127
If I instead call UInt32() I get 1, so it overflows as 2**32 + 1? Not sure about the last one.
So despite the fact that no overflow is expected (129 < 255), it seems to overflow, i.e. act like an signed integer.
The same goes if I cast to UInt16, i.e. casting 32768 (2**16/2 + 1) to UInt16 and then calling int() I get 4294934528 i.e. 2**32 - 2**16/2
So it looks like the cast to UInt8 and UInt16 actually perform casts to Int8 and Int16, and then overflow.
Even more confusingly, this behaviour is different based on where the original UInt32 is from, but behaviour differs when initialising using var and alias (see below).
fnfill_super_minimal() -> List[UInt32]:
vartable= List[UInt32](capacity=4)
table.append(129)
# This overflows with alias, but not var
table.append(int(table[0].cast[DType.uint8]()))
# This overflows in both cases, as if uint8 overflowed.vara: UInt32 =129
table.append(int(a.cast[DType.uint8]()))
# This overflows as if uint32 overflowsvarb: UInt32 =129
table.append(UInt32(b.cast[DType.uint8]()))
return table
defmain():
varvar_table= fill_super_minimal()
aliasalias_table= fill_super_minimal()
for i inrange(4):
print(var_table[i], alias_table[i])
The expected output would be:
129 129
129 129
129 129
129 129
But I get:
129 129
129 4294967169
4294967169 4294967169
1 1
System information
- What OS did you do install Mojo on ?
Ubuntu 22.04.4 LTS
- Provide version information for Mojo by pasting the output of `mojo -v`
mojo 24.4.0 (2cb57382)
- Provide Modular CLI version by pasting the output of `modular -v`
modular 0.8.0 (39a426b5)
The text was updated successfully, but these errors were encountered:
Bug description
Migrating this here after a bit of discussion in Discord.
It seems like casting to unsigned integers actually just casts to signed integers, but has different behaviour in different cases.
I.e. I would expect that if I took a
UInt32
value, set it to129
, cast it toUInt8
and then callint()
, I would still get 129.However, this seem to end up as
4294967169
, i.e.2**32 - 127
If I instead call
UInt32()
I get1
, so it overflows as2**32 + 1
? Not sure about the last one.So despite the fact that no overflow is expected (
129 < 255
), it seems to overflow, i.e. act like an signed integer.The same goes if I cast to
UInt16
, i.e. casting32768
(2**16/2 + 1
) toUInt16
and then callingint()
I get4294934528
i.e.2**32 - 2**16/2
So it looks like the cast to
UInt8
andUInt16
actually perform casts toInt8
andInt16
, and then overflow.Even more confusingly, this behaviour is different based on where the original
UInt32
is from, but behaviour differs when initialising usingvar
andalias
(see below).Steps to reproduce
Most minimal example
Produces:
Doesn't have the same effect in the REPL, but the REPL does seem to have a clue:
A little bit less of a minimal example:
The expected output would be:
But I get:
System information
The text was updated successfully, but these errors were encountered: