16
Your task is pretty simple. Given two floats, bitwise xor the binary representation of them, and output that as a float.
For example,
Normal: 16.7472 ^ 123.61 = 7.13402e-37
Binary: 01000001100001011111101001000100 ^ 01000010111101110011100001010010 = 00000011011100101100001000010110
Normal: 2.2 ^ 4.4 = 1.17549e-38
Binary: 01000000000011001100110011001101 ^ 01000000100011001100110011001101 = 00000000100000000000000000000000
Normal: 7.898 ^ 3.4444 = 1.47705e-38
Binary: 01000000111111001011110001101010 ^ 01000000010111000110101001111111 = 00000000101000001101011000010101
Restrictions/clarifications:
- Input/output can be given by any convenient method.
- The program can be a full program or just a function; either is fine.
- The float type can be any size, but the minimum size is 2 bytes.
- Standard loopholes are forbidden.
- Shortest code wins.
Welcome to Code Golf! Could you provide some more test cases? – Stephen – 2019-09-15T13:45:57.923
2Does Boolean list count as a convenient method? – Adám – 2019-09-15T14:11:35.713
Why the lenient input format but stringent output format? How about Boolean list as output? – Adám – 2019-09-15T14:12:18.630
I would say no. I'll edit the question to clarify on what a "convenient method" is. – virchau13 – 2019-09-15T14:12:49.813
What if the resulting bit pattern doesn't represent a valid float? – Adám – 2019-09-15T14:13:16.287
I don't think that's possible...? I just wrote a program to bruteforce 4-byte floats from 0 to UINT_MAX, and it didn't die anywhere. NaN is a valid output, if that's what you're asking. – virchau13 – 2019-09-15T14:24:57.910
@virchau With the exactly right input, it should be possible to produce the representation of a sNaN, causing an error. – Adám – 2019-09-15T14:32:14.573
I found the inputs that work with 8-byte floats (doubles) to produce an sNaN: 1.54234e+260 1.63233e-260 It doesn't throw an FPE on my system, though. Can you try it and see? – virchau13 – 2019-09-15T14:51:42.020
That gives me 1.675075421658194e-309. – Adám – 2019-09-15T17:47:59.067
23"binary representation" of a float is extremely ambiguous. You'll need to define which representation you're using. There are an infinite number of representations, including the finite number already used by life on this planet, some being more popular than others, such as IEEE 754 – Reversed Engineer – 2019-09-16T10:07:44.310
1Do we have to handle NaN input? – Grimmy – 2019-09-16T12:42:36.893
7This question would be more interesting as "xor the values" rather than "xor the representations". The latter is of course identical to "xor two 32-bit integers" in any language that lacks a type system or admits type punning, and thus is pretty boring... – R.. GitHub STOP HELPING ICE – 2019-09-16T14:33:44.803
5Do we have to handle infinity, subnormals, or negative 0, as either input or output? – Grimmy – 2019-09-16T16:24:11.100
@R.., you're assuming 32-bit IEEE-734. XORing a pair of Microsoft Basic 40-bit floats is a bit more exciting. – Mark – 2019-09-17T00:13:21.653
@R..: Indeed -- and then of course input and output should be given as exact fractions. (It may be assumed that the input fractions are in lowest terms and the denominators are not powers of two, so the binary representations are unique). – hmakholm left over Monica – 2019-09-17T01:38:46.007
3@Mark: No, as written the question is just about xor'ing their representations, whatever those representations are. The result is dependent on the floating point format but the algorithm is always a single xor instruction on the representation, which is pretty boring. – R.. GitHub STOP HELPING ICE – 2019-09-17T01:43:53.290
@HenningMakholm: Huh? These are floating point (diadic rationals) not arbitrary rationals. I don't even know what xor would mean on the latter. – R.. GitHub STOP HELPING ICE – 2019-09-17T01:44:36.573
1@R..: Take their representations as (infinite, repeating) binary fractions, XOR them bit by bit; find out what the result represents? – hmakholm left over Monica – 2019-09-17T01:49:47.913
@HenningMakholm: That's not mathematically meaningful, unlike xor of float values which is addition of polynomials over Z/2Z. – R.. GitHub STOP HELPING ICE – 2019-09-17T01:51:30.373
@ReversedEngineer: For languages that support floating point, I think it's implied that you should use the language's native object-representation, whatever that is. (e.g. something equivalent to type-punning to integer and back). If the language doesn't support type-punning or XOR of FP data, then you have to implement it yourself using whatever FP representation your chosen implementation uses. – Peter Cordes – 2019-09-17T07:48:40.817