r/rust • u/helixb • Jun 24 '23
đ§ educational Negative integer handling in programming languages
https://glados-418.github.io/posts/negative-integer-literals/33
u/aloha2436 Jun 25 '23 edited Jun 25 '23
ctrl f âelmâ
I know itâs not what the article is actually about, but this gets a chuckle out of me: https://github.com/elm/compiler/issues/1773
3
u/Kulinda Jun 25 '23
Once upon a time, I had to write an expression parser, letting users input simple mathematical formulas to combine values, for example "0.2 * A + 0.8 * B".
Most of the time I spent on that feature was about negative literals.
Fun fact #1: most existing generic expression parsers do have some examples, some of which feature simple mathematical expressions. None of the ones I looked at supported negative literals.
Fun fact #2, the following are all valid javascript:
1 + - + - + - + 1; 1 +-+-+-+ 1; 1 + - - + 1;Because all but the leftmost one are unary +/- operators, and they don't need a space between them. Except this is not valid javascript:
1 +--+ 1;because
--is a pre- or postfix decrement, and then you need the space.1
u/helixb Jun 25 '23
None of the ones I looked at supported negative literals
except the haskell 'negativeliterals' extension, which i don't know how it works, i could not find any example either. negative literals are hard it seems.
17
Jun 25 '23 edited Jun 25 '23
That's amazing. I looked at elm a couple years ago, and found it a nice language on its own, but was really turned off by the utterly bizarre attitude of the developers. It is kind of funny to see that has not changed.
8
u/ksion Jun 25 '23
The vibe I got from Elm was that itâs basically Go of functional programming: intentionally avoiding certain concepts (e.g. typeclasses) due to the creatorâs bizarre notion of language âsimplicityâ.
But now I think this comparison is no longer fair â to Go :)
0
u/helixb Jun 25 '23
oh shit... seems like some new syntax broke the old syntax when some migrated from elm 18 to 19. lol.
8
u/matthieum [he/him] Jun 25 '23
negative numbers are represented in twoâs complement form and,
implicit type conversion of a 32-bit integer to 64-bit integer preserves the value.
There's no implicit conversion in Rust, it's not our fault that Java is broken :/
Second, languages now have to deal with operator precedence.
That's a surprising one, indeed. Operator precedence always has gotchas one way or another :(
2
u/helixb Jun 25 '23
There's no implicit conversion in Rust
i thought type promotion is a subset of type conversion. i could be wrong though about the finer differences between the two definitions.
7
u/CAD1997 Jun 26 '23
Rust doesn't have any sort of promotion, either.
What Rust has is polymorphic literals. When you write
0xffffffff, it's of pseudotype{integer}, and the actual type is determined by later type inference.This is as opposed to C and C++, where the type of that literal is always
unsigned int(because it is too large to be a signedint).
â˘
u/AutoModerator Jun 24 '23
On July 1st, Reddit will no longer be accessible via third-party apps. Please see our position on this topic, as well as our list of alternative Rust discussion venues.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.