Hi!
I think I've found a bug when trying to assign int's smallest value to a variable:
var i = -9223372036854775808
However this works OK:
var i = low(int)
The error I'm getting from the compiler is: num.nim(1, 9) Error: number 9223372036854775808 out of valid range
I'm using Nim 0.10.2 on Linux x86_64.
Is this really a bug or a newbie error (I'm just starting playing with Nim).
Jehan,
Thank you for the explanation, so, how can I assign that number to an int var?
What's wrong with using int.low?
If you absolutely have to spell it out, use
var i = -9223372036854775807-1
or
var i = 1 shl 63
There's nothing wrong with int.low, however I wanted to know if it was possible to just write the number in an intuitive way. It feels somewhat unnatural to me having to write:
var i = -9223372036854775807 - 1
or even worst:
var i = 1 shl 63
Maybe 0x8000000000000000 is a better option in this case, but I'm surprised not being able to just using the decimal representation.
I was asking because (outside of generated code) I don't know why you'd want to write the constant explicitly in decimal notation, so I was wondering if there was any actual real-world situation where you had to write it as a literal constant rather than int.low or int64.low. Note also that this is not specific to Nim; plenty of languages do not allow this (because it makes the language spec more complicated for relatively little gain).
I'd also argue that 1 shl 63 conveys the intent more clearly than a large decimal constant where people may or may not know that it corresponds to the minimum integer or where an error can more easily occur (even with hexadecimal notation, it's easy to miscount the zeroes).