Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bytes.as-int #632

Closed
IngeniariusSoftware opened this issue May 14, 2022 · 17 comments
Closed

bytes.as-int #632

IngeniariusSoftware opened this issue May 14, 2022 · 17 comments
Assignees

Comments

@IngeniariusSoftware
Copy link
Member

IngeniariusSoftware commented May 14, 2022

else if (Long.class.equals(type)) {
if (ret.length == 1) {
res = (long) ret[0];
}
else {
final byte[] cpy = new byte[Long.BYTES];
cpy[0] = ret[0];
int posx = cpy.length;
int posy = ret.length;
while (posy-- > 1 && posx-- > 1) {
cpy[posx] = ret[posy];
}
res = ByteBuffer.wrap(cpy).getLong();
}
}

the current implementation of as-bytes gives us incorrect results for bytes:

1) 01-00.as-int  ->   72057594037927936 instead 256
2) FF-FF.as-int  ->  -72057594037927681 instead -1
3) 00-00-00-00-00-00-01-00.not.as-int  -> -144115188075855617 instead -257
@yegor256
Copy link
Member

@andreoss what do you think?

@andreoss
Copy link
Member

@yegor256 @IngeniariusSoftware
Looks like the expected behaviour to me.
If you want to convert to int you should use 8 bytes, or the value will get padded to 8 bytes (keeping the first byte in order to preserve the sign)

#608

@yegor256
Copy link
Member

@andreoss @IngeniariusSoftware I think 01-00.as-int should lead to an error instead of returning some number. Indeed, bytes.as-int should expect exactly eight bytes.

@IngeniariusSoftware
Copy link
Member Author

@andreoss, is this really the expected behavior? I looked at how bytes are converted to numbers in different languages, and there, expressed in our logic, bytes are placed at the end of the array without transferring the first byte to the beginning. If necessary, completing the sign:

Input EO Others
01-00 01-00-00-00-00-00-00-00 00-00-00-00-00-00-00-01
FF-FF FF-00-00-00-00-00-00-FF FF-FF-FF-FF-FF-FF-FF-FF

@yegor25, what is the advantage of using a strictly 8-byte limit? Isn't this redundant encoding? Pay attention to the 3)

00-00-00-00-00-00-01-00.not.as-int = -144115188075855617
Expected: -257

I specifically take 8 bytes, but the result is still different. Instead of storing all 8 bytes, BigInteger truncates to the required minimum, and as a result our converter gets FE-FF instead of FF-FF-FF-FF-FF-FF-FE-FF.

@yegor256
Copy link
Member

@IngeniariusSoftware the design of EO is "explicit". This means that we don't make any assumptions, which are not visible to a programmer. Instead, we "fail fast" when the input doesn't look exactly as we expect it. It must be the responsibility of a programmer: to prepare the data the way we expect it to look like. That's why I suggest as-int to fail if anything except eight bytes is provided.

@andreoss
Copy link
Member

@yegor256 Please, assign

@yegor256
Copy link
Member

@andreoss go ahead please

@yegor256
Copy link
Member

@andreoss what's up with this?

@andreoss
Copy link
Member

@yegor256 Will open a PR, sorry for the delay

andreoss added a commit to andreoss/eo that referenced this issue Aug 8, 2022
`and`, `or`, `xor` do not truncate array.
andreoss added a commit to andreoss/eo that referenced this issue Aug 8, 2022
`and`, `or`, `xor` do not truncate array.
andreoss added a commit to andreoss/eo that referenced this issue Aug 15, 2022
andreoss added a commit to andreoss/eo that referenced this issue Aug 15, 2022
@0pdd
Copy link

0pdd commented Aug 15, 2022

@IngeniariusSoftware the puzzle #1043 is still not solved.

@0pdd
Copy link

0pdd commented Aug 16, 2022

@IngeniariusSoftware the puzzle #1046 is still not solved; solved: #1043.

@0pdd
Copy link

0pdd commented Sep 3, 2022

@IngeniariusSoftware the puzzle #1142 is still not solved; solved: #1043, #1046.

@0pdd
Copy link

0pdd commented Sep 7, 2022

@IngeniariusSoftware 3 puzzles #1183, #1184, #1185 are still not solved; solved: #1043, #1046, #1142.

@0pdd
Copy link

0pdd commented Sep 8, 2022

@IngeniariusSoftware 2 puzzles #1183, #1185 are still not solved; solved: #1043, #1046, #1142, #1184.

@0pdd
Copy link

0pdd commented Sep 11, 2022

@IngeniariusSoftware 2 puzzles #1183, #1207 are still not solved; solved: #1043, #1046, #1142, #1184, #1185.

@0pdd
Copy link

0pdd commented Sep 18, 2022

@IngeniariusSoftware the puzzle #1183 is still not solved; solved: #1043, #1046, #1142, #1184, #1185, #1207.

@0pdd
Copy link

0pdd commented Jan 18, 2023

@IngeniariusSoftware all 7 puzzles are solved here: #1043, #1046, #1142, #1183, #1184, #1185, #1207.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants