mojira.dev
MC-276879

Floats are truncated when cast to longs with /data, all other cases round down

Using `data modify storage ...`, you can cast a float or double to a byte, int, or long by appending it to an array of the corresponding type. When doing this, all cases round down, except appending a float to a long array, which truncates the value instead.

How to reproduce:

  1. Run commands to create byte, int, and long arrays:

    /data modify storage test byte set value [B;]
    /data modify storage test int set value [I;]
    /data modify storage test long set value [L;]
  2. Append a float and a double to each array. Make sure the number is negative and not an integer, so that truncating behaves differently than rounding down.

    /data modify storage test byte append value -1.5f
    /data modify storage test byte append value -1.5d
    
    /data modify storage test int append value -1.5f
    /data modify storage test int append value -1.5d
    
    /data modify storage test long append value -1.5f
    /data modify storage test long append value -1.5d
  3. Get the values of the arrays:

    /data get storage test
  4. You can see that both the byte and int arrays are [-2, -2], while the long array is [-1, -2], meaning that the float got truncated instead of rounded down when cast to a long.

I would expect that all cases would be consistent, rather than having one combination that behaves differently than all of the others.

Linked issues

Attachments

Comments 3

Can confirm in 1.21.4.

[media]

brief code analysis: FloatTag.getAsLong does not call floor in the conversion, as opposed to getAsByte, getAsShort, getAsInt, and DoubleTag.getAsLong which all do. that is the cause of the inconsistent behavior

Can confirm the same on /execute store … int <scale>

mr_cheese

(Unassigned)

Confirmed

Platform

Low

Commands

Commands

1.21.1, 24w38a

Retrieved