Hello,
what numerical type should I use to represent 0xffff_ffff_ffff_ffff ?
Obviously, Int64.of_int 0xffff_ffff_ffff_ffff
doesn’t work:
Error: Integer literal exceeds the range of representable integers of type int
Hello,
what numerical type should I use to represent 0xffff_ffff_ffff_ffff ?
Obviously, Int64.of_int 0xffff_ffff_ffff_ffff
doesn’t work:
Error: Integer literal exceeds the range of representable integers of type int
This doesn’t really answer your question but Int64 literal are suffixed with n : 0xffff_ffff_ffff_ffffn = -1n
works as expected.
I believe 1n
is a nativeint, it’s 1L
for a int64
value
Indeed (and 1l
for int32), I had an erroneous native=64bit in mind .
Thank you all for the suggestions.
Maybe some context is useful here:
The question arose while implementing a language, that has a nat64
data type. The type checker had to make sure that all nat64
literals are between 0 and 0xffff_ffff_ffff_ffff.
But since the parser uses Int64.of_string_opt
he wouldn’t even be able to parse literals greater than 0xffff_ffff_ffff_ffff.
This is good so far.
However, the type checker still couldn’t differentiate between
var x nat64 = 0xffff_ffff_ffff_ffff // should typecheck
and
var x nat64 = -1 // should not typecheck
I think I’ll ignore the problem for now, but any hints how to solve it are still welcome.
Perhaps I’m missing something basic here, but why aren’t you just using infinite-precision rationals and just be done with it? This is the type-checker, not the runtime, right? So you should be perfectly safe using something less efficient than native ints, yes? Just arrange to check in all computations on constant-expressions/values, for overflow, and everything should be fine, yes?
Maybe I’m just missing something really basic. But for example, recently, perusing the Ocaml AST, I noticed that it went from using “int” in integer-constants, to using strings + a one-char indication of which type the string was a member of.
No, you are not missing anything at all.
Knowing OCaml represents integer constants as strings makes me feel better to choose a less performant implementation, too.