8 ms·
Unsigned integers are also, trivially, asymmetrical. Floats are symmetrical at the cost of redundant encodings. Fundamentally a binary representation of b bits
by addaon 5y ago
Unsigned integers are also, trivially, asymmetrical. Floats are symmetrical at the cost of redundant encodings. Fundamentally a binary representation of b bits can support 2^b values. 2^b is even, and any symmetric range that includes zero has an odd number of distinct values, so a binary representation must either be asymmetric, contain undefined values, or contain redundant encodings (e.g. negative zero).
- manwe150 5y agoFloats could have had NaN occupy that encoding slot to balance against zero, but chose to instead add other complication and redundant encodings.
- unholiness 5y agoFloats still have an issue for this particular piece of code though! abs(-0) would return -0
- kps 5y agoIEEE754 specifies that abs(−0) is 0 (positive). (If your machine doesn't use IEEE754, I'd love to hear more about your project.)
- jsrcout 5y agoSome coworkers had to deal with MIL-STD-1750A [0] floating point recently. None of us had ever heard of it. They were talking to some pretty old HW. [0] https://en.wikipedia.org/wiki/MIL-STD-1750A https://en.wikipedia.org/wiki/MIL-STD-1750A
- hoseja 5y agoWow, lot of famous spacecraft using that.
- Sniffnoy 5y agoThe commenter above you isn't talking about IEEE754's abs() function, but rather the code from the OP, except applied to floats rather than ints.
- LinAGKar 5y agoThat code also has the same problem for one's complement and sign+magnitude, which are allowed encodings in C.
- ameliaquining 5y agoThis is going away in the next version of the C standard. http://www.open-std.org/jtc1/sc22/wg14/www/docs/n2412.pdf http://www.open-std.org/jtc1/sc22/wg14/www/docs/n2412.pdf
- Sniffnoy 5y agoOh, nice!
- guerrilla 5y agoI've always hated this fact with passion but it's like hating gravity. You can't fight it. Personally, I would have gone with one's compliment and a modified equality but that's just my OCD speaking.
- jleahy 5y agoIt unfortunately makes all the hardware very messy, end around carry is not a pretty thing. You end up needing fiddly flagged prefix adders.
- potiuper 5y agoFurther down the rabbit hole: https://en.wikipedia.org/wiki/Gray_code#Balanced_Gray_code https://en.wikipedia.org/wiki/Gray_code#Balanced_Gray_code Have yet to discover/work out an addition algo for balanced similar to Harold Lucal's algorithm https://stackoverflow.com/questions/20923165/gray-codes-addition https://stackoverflow.com/questions/20923165/gray-codes-addi...
- CamperBob2 5y agoOr just constrain the problem to support only [-2^N-1, 2^N-1]. This gets you an extra bit of precision when multiplying two signed numbers, because you no longer need to leave room for two sign bits in the product.
- pgn674 5y agoTwo things I hate but can't fight: Tetrahedra almost tessellate but don't quite do it: https://www.pnas.org/content/pnas/103/28/10612/F1.large.jpg https://www.pnas.org/content/pnas/103/28/10612/F1.large.jpg In music, the frequency ratio of a semitone is ideally 2^(1/12), but without some tiny fudging (called tuning), you can't make harmonies as the frequencies almost but don't quite line up right. I forget exactly how this one works so I may have something off.
- Waterluvian 5y ago“12 tone equal temperament” is a good string to Google if you want to learn about that. Most pianos are intentionally out of tune just enough that they’re good enough for every key. Other coincidences that drive me wild: speed of light is almost but not quite 3.0E8m/s And the fine structure constant being almost but not exactly 1/137.
- kps 5y agoI once worked with a 16-bit processor that used two's complement with the convention — fortunately mostly just a convention — that 0x8000 was an undefined reserved value, rather than −32768. I say ‘fortunately’ because I was tasked with implementing C-style 16- and 32-bit arithmetic, where 0x8000 is perfectly reasonable as an unsigned value and perfectly reasonable as either half of a long. And ‘mostly’ because the hardware multiply instruction took two 16-bit signed operands and produced a 30 bit result.
- GolDDranks 5y agoThe most beautiful way around this I've seen is the Posit number system. It's a floating point system that contains a single NaN which is the opposite of zero. https://posithub.org/ https://posithub.org/
- addaon 5y agoI'm a huge fan of this system, but it's worth pointing out that it doesn't have a single NaN, it has a single infinity with an undefined sign, modeling the projectively extended real numbers. NaN is a bit of an abomination; projective infinity is an uncommon beast, but one that can be worked with consistently. And yes, by pairing zero with infinity on opposite sides of the number ring, you get a symmetric system with no undefined or redundant encodings.
- Aardwolf 5y agoSigned infinity is useful for limits, and also for initial values of some algorithms, so missing them seems like a disadvantage. Also, it still has no total order, so keeps another messy part of IEEE floats
- GolDDranks 5y agoThere's an easy alternative: just use the MAX and MIN values. That's what you would do with integers anyway, in case of initial values. As for limits, if you need to express limits in a datatype for expressing a small subset of rational numbers, you've got the wrong tool for the job.
- GolDDranks 5y ago> Also, it still has no total order This is not true. It is defined to have the exact same ordering, bitwise, as two's complement integers. That means it has total order: the NaN is the most negative, then comes the negative numbers, then zero, and then positive numbers.
- eurasiantiger 5y agoNaN is a feature of dynamically typed languages; otherwise they would not be able to represent all outcomes of implicit coercion.
- high_byte 5y agoI wonder if negative zero is redundant or actually has some uses.
- kzrdude 5y agoIt is carrying data now, so I'm sure many programs use it.
- kurlberg 5y agoYes, it has uses. E.g., see Kahan's "Branch Cuts for Complex Elementary Functions, or Much Ado About Nothing's Sign Bit", copy available at https://people.freebsd.org/~das/kahan86branch.pdf https://people.freebsd.org/~das/kahan86branch.pdf (he gives an example regarding complex branch cuts and fluid dynamics applications.)
- thaumasiotes 5y ago> redundant encodings (e.g. negative zero). But negative zero is not a redundant value. It behaves very differently from positive zero.
- LinAGKar 5y agoDoes it? The only thing it does I am aware of that it does differently is decide between negative or positive infinity, which doesn't exist for integers.
- thaumasiotes 5y agoNegative zero doesn't exist for integers either. (Note also that -0 is an additive identity, and +0 isn't.)
- regularfry 5y ago> Note also that -0 is an additive identity, and +0 isn't. Wait what That's really confused me. Is this purely with reference to addition operations as defined in IEEE-754, or is there an underlying mathematical reasoning that makes it make sense?
- thaumasiotes 5y agoIn the underlying mathematics, the only additive identity is zero, which is neither positive nor negative. IEEE-754 has signed infinitesimals instead, so it can't follow that. You just have to pick one to be the identity, and they picked -0. I assume the reasoning was that they would prefer values to be +0 when possible. Assigning the sum of -0 and +0 to be +0 instead of -0 means that -0 is the identity and +0 isn't.
- LinAGKar 5y agoUnless you're on a machine which uses one's complement or ign + magnitude, which C currently allows. Luckily, C is moving to make two's complement mandatory.