Integrated Dynamics

Integrated Dynamics

63M Downloads

Type cast precedence for arithmetic operators

met4000 opened this issue · 3 comments

commented

Issue type:

  • ❓ Question

Question:

The Arithmetic Addition operator (and the other arithmetic operators) supports adding numbers of different types, e.g. Int, Long, and Double. .../core/evaluate/variable/ValueTypeCategoryNumber.java looks to be the relevant bit in the code. The process seems to be to cast all numerical inputs to the same type (L79-L80) as that of the type with the highest index according to the ELEMENTS list (L42-L57).

The current ELEMENTS list is {ValueTypes.INTEGER, ValueTypes.DOUBLE, ValueTypes.LONG}, which means that any arithmetic operators that involve longs will cause every other value to be cast to a long - including doubles. This leads to behaviour such as 1.6 (double) + 2 (long) = 3 (long) - truncating the double as it gets cast, and the final result being a long. The screenshot below shows the result of both 1.6 + 2 and 2 + 1.6.
image

Using {ValueTypes.INTEGER, ValueTypes.LONG, ValueTypes.DOUBLE} (i.e. double having higher precedence than long) would both avoid implicitly truncating floating point numbers, and have the result of operations with floating point numbers also be a floating point number - both of which might be expected, based on how other programming languages work. Is it possible to use this ordering over the current one?

commented

Someone will answer your question soon. In the meantime, you might be able to get help more quickly on our Discord server.

commented

Hmm, that makes sense indeed.
Are you open to making a PR for this?
If so, could you target the master-1.18 branch? I'll make sure to upmerge it to other MC versions then.

commented

Can do - I'll try sort out my dev environment tomorrow to be able to test it and make sure something else doesn't immediately blow up from that change.