Floating Point XORFloating point addition, without floats!Format a floating point number exactly as decimalProduce an XOR tablePrimes with prime bit-countsFind the Closest Double PalindromeBitwise XOR of rational numbersXOR sort an arrayAlpha-Numerical BowtieFloating over the integersBit floating sequence
Other than good shoes and a stick, what are some ways to preserve your knees on long hikes?
What are the typical trumpet parts in classical music?
Why is it called a stateful and a stateless firewall?
Permutations in Disguise
Wouldn't Kreacher have been able to escape even without following an order?
Output a Super Mario Image
Can an infinite series be thought of as adding up "infinitely many" terms?
What's the benefit of prohibiting the use of techniques/language constructs that have not been taught?
Beauville-Laszlo for schemes
A Pixelated Sequence - Find the Continuation
geschafft or geschaffen? which one is past participle of schaffen?
What would happen if Protagoras v Euathlus were heard in court today?
Asked to Not Use Transactions and to Use A Workaround to Simulate One
In what state are satellites left in when they are left in a graveyard orbit?
How do we know that black holes are spinning?
Teleport everything in a large zone; or teleport all living things and make a lot of equipment disappear
Pronunciation of "солнце"
Did slaves have slaves?
What 68-pin connector is this on my 2.5" solid state drive?
Is my sink P-trap too low?
Impossible Scrabble Words
What does the "capacitor into resistance" symbol mean?
Floating Point XOR
Tips for remembering the order of parameters for ln?
Floating Point XOR
Floating point addition, without floats!Format a floating point number exactly as decimalProduce an XOR tablePrimes with prime bit-countsFind the Closest Double PalindromeBitwise XOR of rational numbersXOR sort an arrayAlpha-Numerical BowtieFloating over the integersBit floating sequence
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
$begingroup$
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.
code-golf bitwise floating-point
New contributor
virchau is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
$endgroup$
|
show 4 more comments
$begingroup$
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.
code-golf bitwise floating-point
New contributor
virchau is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
$endgroup$
$begingroup$
Welcome to Code Golf! Could you provide some more test cases?
$endgroup$
– Stephen
8 hours ago
$begingroup$
Does Boolean list count as a convenient method?
$endgroup$
– Adám
7 hours ago
$begingroup$
Why the lenient input format but stringent output format? How about Boolean list as output?
$endgroup$
– Adám
7 hours ago
$begingroup$
I would say no. I'll edit the question to clarify on what a "convenient method" is.
$endgroup$
– virchau
7 hours ago
$begingroup$
What if the resulting bit pattern doesn't represent a valid float?
$endgroup$
– Adám
7 hours ago
|
show 4 more comments
$begingroup$
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.
code-golf bitwise floating-point
New contributor
virchau is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
$endgroup$
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.
code-golf bitwise floating-point
code-golf bitwise floating-point
New contributor
virchau is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
virchau is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited 7 hours ago
virchau
New contributor
virchau is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked 8 hours ago
virchauvirchau
142 bronze badges
142 bronze badges
New contributor
virchau is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
virchau is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
$begingroup$
Welcome to Code Golf! Could you provide some more test cases?
$endgroup$
– Stephen
8 hours ago
$begingroup$
Does Boolean list count as a convenient method?
$endgroup$
– Adám
7 hours ago
$begingroup$
Why the lenient input format but stringent output format? How about Boolean list as output?
$endgroup$
– Adám
7 hours ago
$begingroup$
I would say no. I'll edit the question to clarify on what a "convenient method" is.
$endgroup$
– virchau
7 hours ago
$begingroup$
What if the resulting bit pattern doesn't represent a valid float?
$endgroup$
– Adám
7 hours ago
|
show 4 more comments
$begingroup$
Welcome to Code Golf! Could you provide some more test cases?
$endgroup$
– Stephen
8 hours ago
$begingroup$
Does Boolean list count as a convenient method?
$endgroup$
– Adám
7 hours ago
$begingroup$
Why the lenient input format but stringent output format? How about Boolean list as output?
$endgroup$
– Adám
7 hours ago
$begingroup$
I would say no. I'll edit the question to clarify on what a "convenient method" is.
$endgroup$
– virchau
7 hours ago
$begingroup$
What if the resulting bit pattern doesn't represent a valid float?
$endgroup$
– Adám
7 hours ago
$begingroup$
Welcome to Code Golf! Could you provide some more test cases?
$endgroup$
– Stephen
8 hours ago
$begingroup$
Welcome to Code Golf! Could you provide some more test cases?
$endgroup$
– Stephen
8 hours ago
$begingroup$
Does Boolean list count as a convenient method?
$endgroup$
– Adám
7 hours ago
$begingroup$
Does Boolean list count as a convenient method?
$endgroup$
– Adám
7 hours ago
$begingroup$
Why the lenient input format but stringent output format? How about Boolean list as output?
$endgroup$
– Adám
7 hours ago
$begingroup$
Why the lenient input format but stringent output format? How about Boolean list as output?
$endgroup$
– Adám
7 hours ago
$begingroup$
I would say no. I'll edit the question to clarify on what a "convenient method" is.
$endgroup$
– virchau
7 hours ago
$begingroup$
I would say no. I'll edit the question to clarify on what a "convenient method" is.
$endgroup$
– virchau
7 hours ago
$begingroup$
What if the resulting bit pattern doesn't represent a valid float?
$endgroup$
– Adám
7 hours ago
$begingroup$
What if the resulting bit pattern doesn't represent a valid float?
$endgroup$
– Adám
7 hours ago
|
show 4 more comments
7 Answers
7
active
oldest
votes
$begingroup$
x86-64 machine code, 4 bytes
0f 57 c1 c3
In assembly:
xorps xmm0, xmm1
ret
This is a callable function that takes two floats or doubles as arguments (in xmm0 and xmm1) and returns a float or double (in xmm0). That matches the calling convention of both the WIN64 and SysV ABI, and works for floats as well as doubles.
$endgroup$
add a comment
|
$begingroup$
C# (Visual C# Interactive Compiler), 92 bytes
x=>BitConverter.Int32BitsToSingle(x.Aggregate(0,(a,b)=>a^BitConverter.SingleToInt32Bits(b)))
Try it online!
$endgroup$
1
$begingroup$
You could use unsafe code for this, though I'm not sure if the "unsafe" keyword should affect the bytecount or not.
$endgroup$
– negative seven
3 hours ago
add a comment
|
$begingroup$
APL (Dyalog Unicode), 14 bytesSBCS
Full program. Prompts for 1-column matrix of two IEEE 754 64-bit floating-point numbers (binary64) from stdin. Prints one such number to stdout.
645⎕DR≠⌿11⎕DR⎕
Try it online!
⎕ prompt (numbers that collapse to non-floats can be forced into floats with the function ⊃⊢⎕DR⍨645,⍨⎕DR)
11⎕DR convert to 1-bit Binary (1) Data Representation (2-row, 64-column matrix)
≠⌿ vertical XOR reduction
645⎕DR convert to 64-bit float (5) Data Representation (single number)
$endgroup$
$begingroup$
I was going to come and post this! :D
$endgroup$
– Simon Rose
2 hours ago
add a comment
|
$begingroup$
C++ (gcc), 74 37 bytes
#define f(x,y)(*(int*)x^=*(int*)y,*x)
Try it online!
I haven’t previously golfed in C++ so am sure this could be shorter. A macro which takes pointers to two floats as its arguments and returns a float.
Thanks to @12Me1 for saving 2 bytes and @Arnauld for saving 4! Thanks to @Nishioka for saving another 14, @Neil a further 6 and @AZTECCO and @Nishioka another 11!
$endgroup$
1
$begingroup$
You can remove the linebreaks to save 2 chars, and this also works in C
$endgroup$
– 12Me21
5 hours ago
1
$begingroup$
You can save 4 more bytes withz=*(int*)x^*(int*)y;.
$endgroup$
– Arnauld
5 hours ago
1
$begingroup$
With gcc extensions, 54 bytes:#define f(x,y)(int z=*(int*)x^*(int*)y;*(float*)&z;)
$endgroup$
– Nishioka
3 hours ago
1
$begingroup$
Since you're using pointers, is it legal to use one of the inputs as the output? If so, you could write(*(int*)x^=*(int*)y).
$endgroup$
– Neil
3 hours ago
1
$begingroup$
Taking into consideration @Neil's suggestion it would get to 48 bytes:#define f(x,y)(*(int*)x^=*(int*)y;*(float*)x;)
$endgroup$
– Nishioka
3 hours ago
|
show 6 more comments
$begingroup$
JavaScript (Node.js), 105 bytes
Shorter Node version suggested by @Neil
Takes input as (x)(y).
x=>y=>(v=new Buffer(4),v.writeInt32LE((g=n=>v.writeFloatLE(n)&&v.readInt32LE())(x)^g(y)),v.readFloatLE())
Try it online!
JavaScript (ES6), 115 bytes
Takes input as an array of 2 floats.
a=>(v=new DataView(new ArrayBuffer(4))).getFloat32(v.setUint32([x,y]=a.map(n=>v.getUint32(v.setFloat32(0,n))),x^y))
Try it online!
$endgroup$
$begingroup$
FYI Node'sBuffersaves a few bytes:a=>(v=new Buffer(4),[x,y]=a.map(n=>v.writeFloatLE(n)&&v.readInt32LE()),v.writeInt32LE(x^y),v.readFloatLE()).
$endgroup$
– Neil
3 hours ago
$begingroup$
@Neil Thanks! (saved 2 more bytes by using a function instead ofmap)
$endgroup$
– Arnauld
14 mins ago
add a comment
|
$begingroup$
Python 3 + numpy, 75 bytes
from numpy import*
f=lambda x,y:(x.view(int32)^y.view(int32)).view(float32)
Try it online!
Defines a lambda which takes two numpy float32 arrays as its arguments and returns a numpy float32 array.
$endgroup$
add a comment
|
$begingroup$
Jelly + numpy, 89 84 bytes
“(A“,I^A“,I)vF.item()”;"F“Ȧix⁷ĿUƒ>æȥ)ị]ḳĠ©g⁺⁺ż⁼Ọ⁺⁸UḟṡɼṚCCc$ḤỴx7ạ;cĖĖƝÇḟ~»Ḳ¤¹ṣḢ}jʋƒŒV
Try it online!
Has the dubious honour of being longer than the Python 3 code it reproduces, largely because of the need to convert to/from numpy arrays and the fact that numpy isn’t loaded by Jelly so the __import__() built-in has to be used.
A monadic link taking the two floats as a list as its argument and returning a float.
$endgroup$
add a comment
|
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "200"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
virchau is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f192862%2ffloating-point-xor%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
7 Answers
7
active
oldest
votes
7 Answers
7
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
x86-64 machine code, 4 bytes
0f 57 c1 c3
In assembly:
xorps xmm0, xmm1
ret
This is a callable function that takes two floats or doubles as arguments (in xmm0 and xmm1) and returns a float or double (in xmm0). That matches the calling convention of both the WIN64 and SysV ABI, and works for floats as well as doubles.
$endgroup$
add a comment
|
$begingroup$
x86-64 machine code, 4 bytes
0f 57 c1 c3
In assembly:
xorps xmm0, xmm1
ret
This is a callable function that takes two floats or doubles as arguments (in xmm0 and xmm1) and returns a float or double (in xmm0). That matches the calling convention of both the WIN64 and SysV ABI, and works for floats as well as doubles.
$endgroup$
add a comment
|
$begingroup$
x86-64 machine code, 4 bytes
0f 57 c1 c3
In assembly:
xorps xmm0, xmm1
ret
This is a callable function that takes two floats or doubles as arguments (in xmm0 and xmm1) and returns a float or double (in xmm0). That matches the calling convention of both the WIN64 and SysV ABI, and works for floats as well as doubles.
$endgroup$
x86-64 machine code, 4 bytes
0f 57 c1 c3
In assembly:
xorps xmm0, xmm1
ret
This is a callable function that takes two floats or doubles as arguments (in xmm0 and xmm1) and returns a float or double (in xmm0). That matches the calling convention of both the WIN64 and SysV ABI, and works for floats as well as doubles.
answered 1 hour ago
haroldharold
7594 silver badges7 bronze badges
7594 silver badges7 bronze badges
add a comment
|
add a comment
|
$begingroup$
C# (Visual C# Interactive Compiler), 92 bytes
x=>BitConverter.Int32BitsToSingle(x.Aggregate(0,(a,b)=>a^BitConverter.SingleToInt32Bits(b)))
Try it online!
$endgroup$
1
$begingroup$
You could use unsafe code for this, though I'm not sure if the "unsafe" keyword should affect the bytecount or not.
$endgroup$
– negative seven
3 hours ago
add a comment
|
$begingroup$
C# (Visual C# Interactive Compiler), 92 bytes
x=>BitConverter.Int32BitsToSingle(x.Aggregate(0,(a,b)=>a^BitConverter.SingleToInt32Bits(b)))
Try it online!
$endgroup$
1
$begingroup$
You could use unsafe code for this, though I'm not sure if the "unsafe" keyword should affect the bytecount or not.
$endgroup$
– negative seven
3 hours ago
add a comment
|
$begingroup$
C# (Visual C# Interactive Compiler), 92 bytes
x=>BitConverter.Int32BitsToSingle(x.Aggregate(0,(a,b)=>a^BitConverter.SingleToInt32Bits(b)))
Try it online!
$endgroup$
C# (Visual C# Interactive Compiler), 92 bytes
x=>BitConverter.Int32BitsToSingle(x.Aggregate(0,(a,b)=>a^BitConverter.SingleToInt32Bits(b)))
Try it online!
answered 5 hours ago
Embodiment of IgnoranceEmbodiment of Ignorance
5,3662 silver badges31 bronze badges
5,3662 silver badges31 bronze badges
1
$begingroup$
You could use unsafe code for this, though I'm not sure if the "unsafe" keyword should affect the bytecount or not.
$endgroup$
– negative seven
3 hours ago
add a comment
|
1
$begingroup$
You could use unsafe code for this, though I'm not sure if the "unsafe" keyword should affect the bytecount or not.
$endgroup$
– negative seven
3 hours ago
1
1
$begingroup$
You could use unsafe code for this, though I'm not sure if the "unsafe" keyword should affect the bytecount or not.
$endgroup$
– negative seven
3 hours ago
$begingroup$
You could use unsafe code for this, though I'm not sure if the "unsafe" keyword should affect the bytecount or not.
$endgroup$
– negative seven
3 hours ago
add a comment
|
$begingroup$
APL (Dyalog Unicode), 14 bytesSBCS
Full program. Prompts for 1-column matrix of two IEEE 754 64-bit floating-point numbers (binary64) from stdin. Prints one such number to stdout.
645⎕DR≠⌿11⎕DR⎕
Try it online!
⎕ prompt (numbers that collapse to non-floats can be forced into floats with the function ⊃⊢⎕DR⍨645,⍨⎕DR)
11⎕DR convert to 1-bit Binary (1) Data Representation (2-row, 64-column matrix)
≠⌿ vertical XOR reduction
645⎕DR convert to 64-bit float (5) Data Representation (single number)
$endgroup$
$begingroup$
I was going to come and post this! :D
$endgroup$
– Simon Rose
2 hours ago
add a comment
|
$begingroup$
APL (Dyalog Unicode), 14 bytesSBCS
Full program. Prompts for 1-column matrix of two IEEE 754 64-bit floating-point numbers (binary64) from stdin. Prints one such number to stdout.
645⎕DR≠⌿11⎕DR⎕
Try it online!
⎕ prompt (numbers that collapse to non-floats can be forced into floats with the function ⊃⊢⎕DR⍨645,⍨⎕DR)
11⎕DR convert to 1-bit Binary (1) Data Representation (2-row, 64-column matrix)
≠⌿ vertical XOR reduction
645⎕DR convert to 64-bit float (5) Data Representation (single number)
$endgroup$
$begingroup$
I was going to come and post this! :D
$endgroup$
– Simon Rose
2 hours ago
add a comment
|
$begingroup$
APL (Dyalog Unicode), 14 bytesSBCS
Full program. Prompts for 1-column matrix of two IEEE 754 64-bit floating-point numbers (binary64) from stdin. Prints one such number to stdout.
645⎕DR≠⌿11⎕DR⎕
Try it online!
⎕ prompt (numbers that collapse to non-floats can be forced into floats with the function ⊃⊢⎕DR⍨645,⍨⎕DR)
11⎕DR convert to 1-bit Binary (1) Data Representation (2-row, 64-column matrix)
≠⌿ vertical XOR reduction
645⎕DR convert to 64-bit float (5) Data Representation (single number)
$endgroup$
APL (Dyalog Unicode), 14 bytesSBCS
Full program. Prompts for 1-column matrix of two IEEE 754 64-bit floating-point numbers (binary64) from stdin. Prints one such number to stdout.
645⎕DR≠⌿11⎕DR⎕
Try it online!
⎕ prompt (numbers that collapse to non-floats can be forced into floats with the function ⊃⊢⎕DR⍨645,⍨⎕DR)
11⎕DR convert to 1-bit Binary (1) Data Representation (2-row, 64-column matrix)
≠⌿ vertical XOR reduction
645⎕DR convert to 64-bit float (5) Data Representation (single number)
answered 7 hours ago
AdámAdám
29.7k2 gold badges82 silver badges214 bronze badges
29.7k2 gold badges82 silver badges214 bronze badges
$begingroup$
I was going to come and post this! :D
$endgroup$
– Simon Rose
2 hours ago
add a comment
|
$begingroup$
I was going to come and post this! :D
$endgroup$
– Simon Rose
2 hours ago
$begingroup$
I was going to come and post this! :D
$endgroup$
– Simon Rose
2 hours ago
$begingroup$
I was going to come and post this! :D
$endgroup$
– Simon Rose
2 hours ago
add a comment
|
$begingroup$
C++ (gcc), 74 37 bytes
#define f(x,y)(*(int*)x^=*(int*)y,*x)
Try it online!
I haven’t previously golfed in C++ so am sure this could be shorter. A macro which takes pointers to two floats as its arguments and returns a float.
Thanks to @12Me1 for saving 2 bytes and @Arnauld for saving 4! Thanks to @Nishioka for saving another 14, @Neil a further 6 and @AZTECCO and @Nishioka another 11!
$endgroup$
1
$begingroup$
You can remove the linebreaks to save 2 chars, and this also works in C
$endgroup$
– 12Me21
5 hours ago
1
$begingroup$
You can save 4 more bytes withz=*(int*)x^*(int*)y;.
$endgroup$
– Arnauld
5 hours ago
1
$begingroup$
With gcc extensions, 54 bytes:#define f(x,y)(int z=*(int*)x^*(int*)y;*(float*)&z;)
$endgroup$
– Nishioka
3 hours ago
1
$begingroup$
Since you're using pointers, is it legal to use one of the inputs as the output? If so, you could write(*(int*)x^=*(int*)y).
$endgroup$
– Neil
3 hours ago
1
$begingroup$
Taking into consideration @Neil's suggestion it would get to 48 bytes:#define f(x,y)(*(int*)x^=*(int*)y;*(float*)x;)
$endgroup$
– Nishioka
3 hours ago
|
show 6 more comments
$begingroup$
C++ (gcc), 74 37 bytes
#define f(x,y)(*(int*)x^=*(int*)y,*x)
Try it online!
I haven’t previously golfed in C++ so am sure this could be shorter. A macro which takes pointers to two floats as its arguments and returns a float.
Thanks to @12Me1 for saving 2 bytes and @Arnauld for saving 4! Thanks to @Nishioka for saving another 14, @Neil a further 6 and @AZTECCO and @Nishioka another 11!
$endgroup$
1
$begingroup$
You can remove the linebreaks to save 2 chars, and this also works in C
$endgroup$
– 12Me21
5 hours ago
1
$begingroup$
You can save 4 more bytes withz=*(int*)x^*(int*)y;.
$endgroup$
– Arnauld
5 hours ago
1
$begingroup$
With gcc extensions, 54 bytes:#define f(x,y)(int z=*(int*)x^*(int*)y;*(float*)&z;)
$endgroup$
– Nishioka
3 hours ago
1
$begingroup$
Since you're using pointers, is it legal to use one of the inputs as the output? If so, you could write(*(int*)x^=*(int*)y).
$endgroup$
– Neil
3 hours ago
1
$begingroup$
Taking into consideration @Neil's suggestion it would get to 48 bytes:#define f(x,y)(*(int*)x^=*(int*)y;*(float*)x;)
$endgroup$
– Nishioka
3 hours ago
|
show 6 more comments
$begingroup$
C++ (gcc), 74 37 bytes
#define f(x,y)(*(int*)x^=*(int*)y,*x)
Try it online!
I haven’t previously golfed in C++ so am sure this could be shorter. A macro which takes pointers to two floats as its arguments and returns a float.
Thanks to @12Me1 for saving 2 bytes and @Arnauld for saving 4! Thanks to @Nishioka for saving another 14, @Neil a further 6 and @AZTECCO and @Nishioka another 11!
$endgroup$
C++ (gcc), 74 37 bytes
#define f(x,y)(*(int*)x^=*(int*)y,*x)
Try it online!
I haven’t previously golfed in C++ so am sure this could be shorter. A macro which takes pointers to two floats as its arguments and returns a float.
Thanks to @12Me1 for saving 2 bytes and @Arnauld for saving 4! Thanks to @Nishioka for saving another 14, @Neil a further 6 and @AZTECCO and @Nishioka another 11!
edited 2 hours ago
answered 5 hours ago
Nick KennedyNick Kennedy
6,6251 gold badge9 silver badges15 bronze badges
6,6251 gold badge9 silver badges15 bronze badges
1
$begingroup$
You can remove the linebreaks to save 2 chars, and this also works in C
$endgroup$
– 12Me21
5 hours ago
1
$begingroup$
You can save 4 more bytes withz=*(int*)x^*(int*)y;.
$endgroup$
– Arnauld
5 hours ago
1
$begingroup$
With gcc extensions, 54 bytes:#define f(x,y)(int z=*(int*)x^*(int*)y;*(float*)&z;)
$endgroup$
– Nishioka
3 hours ago
1
$begingroup$
Since you're using pointers, is it legal to use one of the inputs as the output? If so, you could write(*(int*)x^=*(int*)y).
$endgroup$
– Neil
3 hours ago
1
$begingroup$
Taking into consideration @Neil's suggestion it would get to 48 bytes:#define f(x,y)(*(int*)x^=*(int*)y;*(float*)x;)
$endgroup$
– Nishioka
3 hours ago
|
show 6 more comments
1
$begingroup$
You can remove the linebreaks to save 2 chars, and this also works in C
$endgroup$
– 12Me21
5 hours ago
1
$begingroup$
You can save 4 more bytes withz=*(int*)x^*(int*)y;.
$endgroup$
– Arnauld
5 hours ago
1
$begingroup$
With gcc extensions, 54 bytes:#define f(x,y)(int z=*(int*)x^*(int*)y;*(float*)&z;)
$endgroup$
– Nishioka
3 hours ago
1
$begingroup$
Since you're using pointers, is it legal to use one of the inputs as the output? If so, you could write(*(int*)x^=*(int*)y).
$endgroup$
– Neil
3 hours ago
1
$begingroup$
Taking into consideration @Neil's suggestion it would get to 48 bytes:#define f(x,y)(*(int*)x^=*(int*)y;*(float*)x;)
$endgroup$
– Nishioka
3 hours ago
1
1
$begingroup$
You can remove the linebreaks to save 2 chars, and this also works in C
$endgroup$
– 12Me21
5 hours ago
$begingroup$
You can remove the linebreaks to save 2 chars, and this also works in C
$endgroup$
– 12Me21
5 hours ago
1
1
$begingroup$
You can save 4 more bytes with
z=*(int*)x^*(int*)y;.$endgroup$
– Arnauld
5 hours ago
$begingroup$
You can save 4 more bytes with
z=*(int*)x^*(int*)y;.$endgroup$
– Arnauld
5 hours ago
1
1
$begingroup$
With gcc extensions, 54 bytes:
#define f(x,y)(int z=*(int*)x^*(int*)y;*(float*)&z;)$endgroup$
– Nishioka
3 hours ago
$begingroup$
With gcc extensions, 54 bytes:
#define f(x,y)(int z=*(int*)x^*(int*)y;*(float*)&z;)$endgroup$
– Nishioka
3 hours ago
1
1
$begingroup$
Since you're using pointers, is it legal to use one of the inputs as the output? If so, you could write
(*(int*)x^=*(int*)y).$endgroup$
– Neil
3 hours ago
$begingroup$
Since you're using pointers, is it legal to use one of the inputs as the output? If so, you could write
(*(int*)x^=*(int*)y).$endgroup$
– Neil
3 hours ago
1
1
$begingroup$
Taking into consideration @Neil's suggestion it would get to 48 bytes:
#define f(x,y)(*(int*)x^=*(int*)y;*(float*)x;)$endgroup$
– Nishioka
3 hours ago
$begingroup$
Taking into consideration @Neil's suggestion it would get to 48 bytes:
#define f(x,y)(*(int*)x^=*(int*)y;*(float*)x;)$endgroup$
– Nishioka
3 hours ago
|
show 6 more comments
$begingroup$
JavaScript (Node.js), 105 bytes
Shorter Node version suggested by @Neil
Takes input as (x)(y).
x=>y=>(v=new Buffer(4),v.writeInt32LE((g=n=>v.writeFloatLE(n)&&v.readInt32LE())(x)^g(y)),v.readFloatLE())
Try it online!
JavaScript (ES6), 115 bytes
Takes input as an array of 2 floats.
a=>(v=new DataView(new ArrayBuffer(4))).getFloat32(v.setUint32([x,y]=a.map(n=>v.getUint32(v.setFloat32(0,n))),x^y))
Try it online!
$endgroup$
$begingroup$
FYI Node'sBuffersaves a few bytes:a=>(v=new Buffer(4),[x,y]=a.map(n=>v.writeFloatLE(n)&&v.readInt32LE()),v.writeInt32LE(x^y),v.readFloatLE()).
$endgroup$
– Neil
3 hours ago
$begingroup$
@Neil Thanks! (saved 2 more bytes by using a function instead ofmap)
$endgroup$
– Arnauld
14 mins ago
add a comment
|
$begingroup$
JavaScript (Node.js), 105 bytes
Shorter Node version suggested by @Neil
Takes input as (x)(y).
x=>y=>(v=new Buffer(4),v.writeInt32LE((g=n=>v.writeFloatLE(n)&&v.readInt32LE())(x)^g(y)),v.readFloatLE())
Try it online!
JavaScript (ES6), 115 bytes
Takes input as an array of 2 floats.
a=>(v=new DataView(new ArrayBuffer(4))).getFloat32(v.setUint32([x,y]=a.map(n=>v.getUint32(v.setFloat32(0,n))),x^y))
Try it online!
$endgroup$
$begingroup$
FYI Node'sBuffersaves a few bytes:a=>(v=new Buffer(4),[x,y]=a.map(n=>v.writeFloatLE(n)&&v.readInt32LE()),v.writeInt32LE(x^y),v.readFloatLE()).
$endgroup$
– Neil
3 hours ago
$begingroup$
@Neil Thanks! (saved 2 more bytes by using a function instead ofmap)
$endgroup$
– Arnauld
14 mins ago
add a comment
|
$begingroup$
JavaScript (Node.js), 105 bytes
Shorter Node version suggested by @Neil
Takes input as (x)(y).
x=>y=>(v=new Buffer(4),v.writeInt32LE((g=n=>v.writeFloatLE(n)&&v.readInt32LE())(x)^g(y)),v.readFloatLE())
Try it online!
JavaScript (ES6), 115 bytes
Takes input as an array of 2 floats.
a=>(v=new DataView(new ArrayBuffer(4))).getFloat32(v.setUint32([x,y]=a.map(n=>v.getUint32(v.setFloat32(0,n))),x^y))
Try it online!
$endgroup$
JavaScript (Node.js), 105 bytes
Shorter Node version suggested by @Neil
Takes input as (x)(y).
x=>y=>(v=new Buffer(4),v.writeInt32LE((g=n=>v.writeFloatLE(n)&&v.readInt32LE())(x)^g(y)),v.readFloatLE())
Try it online!
JavaScript (ES6), 115 bytes
Takes input as an array of 2 floats.
a=>(v=new DataView(new ArrayBuffer(4))).getFloat32(v.setUint32([x,y]=a.map(n=>v.getUint32(v.setFloat32(0,n))),x^y))
Try it online!
edited 15 mins ago
answered 8 hours ago
ArnauldArnauld
92.6k7 gold badges108 silver badges377 bronze badges
92.6k7 gold badges108 silver badges377 bronze badges
$begingroup$
FYI Node'sBuffersaves a few bytes:a=>(v=new Buffer(4),[x,y]=a.map(n=>v.writeFloatLE(n)&&v.readInt32LE()),v.writeInt32LE(x^y),v.readFloatLE()).
$endgroup$
– Neil
3 hours ago
$begingroup$
@Neil Thanks! (saved 2 more bytes by using a function instead ofmap)
$endgroup$
– Arnauld
14 mins ago
add a comment
|
$begingroup$
FYI Node'sBuffersaves a few bytes:a=>(v=new Buffer(4),[x,y]=a.map(n=>v.writeFloatLE(n)&&v.readInt32LE()),v.writeInt32LE(x^y),v.readFloatLE()).
$endgroup$
– Neil
3 hours ago
$begingroup$
@Neil Thanks! (saved 2 more bytes by using a function instead ofmap)
$endgroup$
– Arnauld
14 mins ago
$begingroup$
FYI Node's
Buffer saves a few bytes: a=>(v=new Buffer(4),[x,y]=a.map(n=>v.writeFloatLE(n)&&v.readInt32LE()),v.writeInt32LE(x^y),v.readFloatLE()).$endgroup$
– Neil
3 hours ago
$begingroup$
FYI Node's
Buffer saves a few bytes: a=>(v=new Buffer(4),[x,y]=a.map(n=>v.writeFloatLE(n)&&v.readInt32LE()),v.writeInt32LE(x^y),v.readFloatLE()).$endgroup$
– Neil
3 hours ago
$begingroup$
@Neil Thanks! (saved 2 more bytes by using a function instead of
map)$endgroup$
– Arnauld
14 mins ago
$begingroup$
@Neil Thanks! (saved 2 more bytes by using a function instead of
map)$endgroup$
– Arnauld
14 mins ago
add a comment
|
$begingroup$
Python 3 + numpy, 75 bytes
from numpy import*
f=lambda x,y:(x.view(int32)^y.view(int32)).view(float32)
Try it online!
Defines a lambda which takes two numpy float32 arrays as its arguments and returns a numpy float32 array.
$endgroup$
add a comment
|
$begingroup$
Python 3 + numpy, 75 bytes
from numpy import*
f=lambda x,y:(x.view(int32)^y.view(int32)).view(float32)
Try it online!
Defines a lambda which takes two numpy float32 arrays as its arguments and returns a numpy float32 array.
$endgroup$
add a comment
|
$begingroup$
Python 3 + numpy, 75 bytes
from numpy import*
f=lambda x,y:(x.view(int32)^y.view(int32)).view(float32)
Try it online!
Defines a lambda which takes two numpy float32 arrays as its arguments and returns a numpy float32 array.
$endgroup$
Python 3 + numpy, 75 bytes
from numpy import*
f=lambda x,y:(x.view(int32)^y.view(int32)).view(float32)
Try it online!
Defines a lambda which takes two numpy float32 arrays as its arguments and returns a numpy float32 array.
answered 4 hours ago
Nick KennedyNick Kennedy
6,6251 gold badge9 silver badges15 bronze badges
6,6251 gold badge9 silver badges15 bronze badges
add a comment
|
add a comment
|
$begingroup$
Jelly + numpy, 89 84 bytes
“(A“,I^A“,I)vF.item()”;"F“Ȧix⁷ĿUƒ>æȥ)ị]ḳĠ©g⁺⁺ż⁼Ọ⁺⁸UḟṡɼṚCCc$ḤỴx7ạ;cĖĖƝÇḟ~»Ḳ¤¹ṣḢ}jʋƒŒV
Try it online!
Has the dubious honour of being longer than the Python 3 code it reproduces, largely because of the need to convert to/from numpy arrays and the fact that numpy isn’t loaded by Jelly so the __import__() built-in has to be used.
A monadic link taking the two floats as a list as its argument and returning a float.
$endgroup$
add a comment
|
$begingroup$
Jelly + numpy, 89 84 bytes
“(A“,I^A“,I)vF.item()”;"F“Ȧix⁷ĿUƒ>æȥ)ị]ḳĠ©g⁺⁺ż⁼Ọ⁺⁸UḟṡɼṚCCc$ḤỴx7ạ;cĖĖƝÇḟ~»Ḳ¤¹ṣḢ}jʋƒŒV
Try it online!
Has the dubious honour of being longer than the Python 3 code it reproduces, largely because of the need to convert to/from numpy arrays and the fact that numpy isn’t loaded by Jelly so the __import__() built-in has to be used.
A monadic link taking the two floats as a list as its argument and returning a float.
$endgroup$
add a comment
|
$begingroup$
Jelly + numpy, 89 84 bytes
“(A“,I^A“,I)vF.item()”;"F“Ȧix⁷ĿUƒ>æȥ)ị]ḳĠ©g⁺⁺ż⁼Ọ⁺⁸UḟṡɼṚCCc$ḤỴx7ạ;cĖĖƝÇḟ~»Ḳ¤¹ṣḢ}jʋƒŒV
Try it online!
Has the dubious honour of being longer than the Python 3 code it reproduces, largely because of the need to convert to/from numpy arrays and the fact that numpy isn’t loaded by Jelly so the __import__() built-in has to be used.
A monadic link taking the two floats as a list as its argument and returning a float.
$endgroup$
Jelly + numpy, 89 84 bytes
“(A“,I^A“,I)vF.item()”;"F“Ȧix⁷ĿUƒ>æȥ)ị]ḳĠ©g⁺⁺ż⁼Ọ⁺⁸UḟṡɼṚCCc$ḤỴx7ạ;cĖĖƝÇḟ~»Ḳ¤¹ṣḢ}jʋƒŒV
Try it online!
Has the dubious honour of being longer than the Python 3 code it reproduces, largely because of the need to convert to/from numpy arrays and the fact that numpy isn’t loaded by Jelly so the __import__() built-in has to be used.
A monadic link taking the two floats as a list as its argument and returning a float.
edited 2 hours ago
answered 2 hours ago
Nick KennedyNick Kennedy
6,6251 gold badge9 silver badges15 bronze badges
6,6251 gold badge9 silver badges15 bronze badges
add a comment
|
add a comment
|
virchau is a new contributor. Be nice, and check out our Code of Conduct.
virchau is a new contributor. Be nice, and check out our Code of Conduct.
virchau is a new contributor. Be nice, and check out our Code of Conduct.
virchau is a new contributor. Be nice, and check out our Code of Conduct.
If this is an answer to a challenge…
…Be sure to follow the challenge specification. However, please refrain from exploiting obvious loopholes. Answers abusing any of the standard loopholes are considered invalid. If you think a specification is unclear or underspecified, comment on the question instead.
…Try to optimize your score. For instance, answers to code-golf challenges should attempt to be as short as possible. You can always include a readable version of the code in addition to the competitive one.
Explanations of your answer make it more interesting to read and are very much encouraged.…Include a short header which indicates the language(s) of your code and its score, as defined by the challenge.
More generally…
…Please make sure to answer the question and provide sufficient detail.
…Avoid asking for help, clarification or responding to other answers (use comments instead).
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f192862%2ffloating-point-xor%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
$begingroup$
Welcome to Code Golf! Could you provide some more test cases?
$endgroup$
– Stephen
8 hours ago
$begingroup$
Does Boolean list count as a convenient method?
$endgroup$
– Adám
7 hours ago
$begingroup$
Why the lenient input format but stringent output format? How about Boolean list as output?
$endgroup$
– Adám
7 hours ago
$begingroup$
I would say no. I'll edit the question to clarify on what a "convenient method" is.
$endgroup$
– virchau
7 hours ago
$begingroup$
What if the resulting bit pattern doesn't represent a valid float?
$endgroup$
– Adám
7 hours ago