4
Given a double-precision float, find the closest double-precision float whose binary representation is a palindrome.
Input
A floating point number x. You may use any format you like for input, but the format you chose must be able to represent every possible IEEE 754 binary64 value, including denormals, distinct representations for +0 and -0, and +Inf, -Inf, and NaN.
Output
A floating point number y. You may use any format you like for output, with the same restrictions as the input format.
The Task
y is any value such that:
- The IEEE 754 binary64 bitstring for
yis a palindrome. That is, the first 32 bits are the reverse of the last 32 bits. abs(x-y)is minimal by thetotalOrderpredicate.
Notes
abs(x-y)must be computed with strict IEEE 754 double-precision floating point arithmetic.totalOrderputs non-numeric values and signed zeroes in this order:- NaN < -Inf < -1 < -0 < +0 < +1 < +Inf < +NaN. Otherwise it behaves like the normal<operator.- The rules for performing arithmetic on non-numeric values can be found at this website
- For an overview of how binary64 floats work, see wikipedia.
- If there is more than one value for
ythat satisfies the conditions, output any one of them. Note that this can only occur ifabs(x-y1) == abs(x-y2), or if they are bothNaN. - The input and output formats may be different if desired; both formats still need to obey all the rules.
- It may be convenient to use a raw binary format for IO; this is permitted.
- For the purposes of this challenge you may consider all
NaNvalues as equivalent, sinceNaNpayload behavior is implementation defined. - It is not sufficient to just mirror the first 32 bits. See test cases 2, 4, 5, and 7 for examples.
- Any non-
NaNdouble palindrome is valid output for x=+Infor x=-Inf, since the distance is still just+Inf. Reversing the first 32 bits would not be correct though since the resultingNaNvalue would have a distance of+NaN > +Inffrom the input. - A
NaNthough, any double palindrome would be correct.
Test Cases
Input: 0x8A2B_7C82_A27D_6D8F = -1.1173033799881615e-259
Output: 0x8A2B_7C82_413E_D451 = -1.1173031443752871e-259
Input: 0x5000_0000_0000_0001 = 2.3158417847463244e+77
Output: 0x4FFF_FFFF_FFFF_FFF2 = 2.3158417847463203e+77
Input: 0x5000_0000_0000_0002 = 2.315841784746325e+77
Output: 0x5000_0000_0000_000A = 2.315841784746329e+77
Input: 0x7FF0_0000_0000_0000 = +Inf
Output: 0x0000_0000_0000_0000 (others are possible)
Input: 0xFFF0_0000_0000_0000 = -Inf
Output: 0x0000_0000_0000_0000 (others are possible)
Input: 0x7FFC_0498_90A3_38C4 = NaN
Output: 0x0000_0000_0000_0000 (others are possible)
Input: 0x8000_0000_0000_0000 = -0
Output: 0x0000_0000_0000_0000 = +0
Input: 0x0000_0000_0000_0000 = +0
Output: 0x0000_0000_0000_0000 = +0
Input: 0x8A2B_7C82_413E_D451 = -1.1173031443752871e-259
Output: 0x8A2B_7C82_413E_D451 = -1.1173031443752871e-259
Input: 0x000F_FFFF_FFFF_FFFF = 2.225073858507201e-308
Output: 0x000F_FFFF_FFFF_F000 = 2.2250738585051777e-308
Input: 0x0000_B815_7268_FDAF = 1e-309
Output: 0x0000_B815_A81D_0000 = 1.0000044514797e-309
code-golf scoring, Standard Loopholes forbidden.
Can you please define or add a reference to IEEE 754 binary64 bitstring? – Mr. Xcoder – 2017-06-27T19:59:18.313
@Mr.Xcoder https://en.wikipedia.org/wiki/Double-precision_floating-point_format
– AJMansfield – 2017-06-27T20:00:06.490