23 ms·
From the linked discussion: > @jrose @fclc People who want NaN in their integers are welcome to do that in the privacy of their own bedrooms with new types the
by eru 3y ago
From the linked discussion:
> @jrose @fclc People who want NaN in their integers are welcome to do that in the privacy of their own bedrooms with new types they invent for this purpose.
That reminds me of OCaml's 63 bit integers. In the privacy of OCaml's own bedroom, they use one bit of their integers as a flag to distinguish pointers from integers.
See https://blog.janestreet.com/what-is-gained-and-lost-with-63-bit-integers/ https://blog.janestreet.com/what-is-gained-and-lost-with-63-...
The results are quite.. interesting. However, it does go to show that having slightly weird private integer types is actually totally workable, so the quote from the discussion might be meant as trolling, but it's less crazy than it sounds like.
- rob74 3y agoThe difference is that (a) that bit in OCaml has a purpose, while disregarding 0x80..00 hasn't; and (b) I assume that it was in OCaml from the beginning, so it wouldn't break existing software in probably very interesting ways (hence the suggestion that this should be done only "in the privacy of their own bedrooms with new types", i.e. not for already-used types)...
- JKCalhoun 3y agoYes, correct me if I'm wrong, but I thought the point was that 0x8000 is like negative zero and so of no utility, redundant. So let's make it useful.
- mbel 3y agoIt's not. 0x8000 is the smallest (-32,768) integer that can be represented with 16-bits and two's complement.
- taeric 3y agoYeah, I think it is funny that in a standard binary, it would replace negative zero and makes some sense. In two's complement, it replaces the minimum value. Fair that sentinel value is sentinel value, I suppose. But it is not like it is not a known and unique number in that encoding.
- pornel 3y agoabs() of this value also happens to exceed the maximum positive value. Which means that in C, a simple negation can cause signed integer overflow -(-32768), and therefore UB.
- gridspy 3y agoA couple of things that are special about 2s compliment - There are no redundant numbers like -0 - Adding two integers together in this format works with negative and positive numbers with no special case. -1 is actually 0xFF or 0xFFFF etc. So 0xFF + 0x01 (-1 + 1) "overflows" to 0.
- hoseja 3y ago> > @jrose @fclc People who want NaN in their integers are welcome to do that in the privacy of their own bedrooms with new types they invent for this purpose. And yet I bet they use IEEE754 floats without second thought.
- tialaramex 3y agoTypically in the unsafe languages under discussion they use the IEEE754 types without even a first thought. For example you can expect them to sort such types (you can define rules for sorting them, but if you just didn't in C too bad your program has Undefined Behaviour, while in C++ today it's even worse you've silently not written a C++ program at all and it never had any defined behaviour even before it tried to execute your invalid sort)
- gpderetta 3y agostd::sorting floats is perfectly well defined in C++ as long as you respect the precondition that there are no NaNs in your range. In other words the subset of floats without NaNs respects strict weak ordering. edit: when using the default comparison. With a custom comparator you can do whatever you want, including using std::weak_order to sort NaNs.
- tialaramex 3y agostd::sort is defined in terms of a semantic requirement Compare - a constraint on types - which forbids types which don't have Equivalence. Specifically, our type needs to have some equivalence predicate equiv(A, B) such that equiv(A, A) is true, and the provided comparisons for floating point types in C++ do not meet this requirement. I would be astonished if you're not correct at runtime in practice of course, but "It works in my environment" and "This is correct by the standard" are two different things.
- gpderetta 3y agostd::sort has a semantic requirement on the type of Compare, but Compare itself only has a constraint on the values it is comparing. From the 27.8.1.3 [1]: "comp shall induce a strict weak ordering on the values." It is not terribly clear what "values" is referring to, but form context it must be all values that are being sorted, as opposed to all possible values of T. The only explicit constrain on T is that it must be Move Assignable and Constructible. [1] https://eel.is/c++draft/alg.sorting.general#4 https://eel.is/c++draft/alg.sorting.general#4