PHP7 + JavaScript, 62 61 58 bytes
This was actually more challenging than I expected! I am quite surprised of how long my code is.
eval(['alert((_=prompt())*_)','echo$argv[1]**.5'][+![]]);
How does it work?
This works by selecting the code to run, from the array.
PHP and JavaScript detection is made with +![]
.
In PHP, []
(empty array) is a falsy value, while in JavaScript it is a truthy value (objects (except null
) are always truthy, even new Boolean(false)
is truthy!).
But, I need to get it to a numeric value, so, I just use a not
(!
) and convert it to integer (with the +
).
Now, PHP yields the value 1
, while JavaScript yields 0
.
Placing the code inside an array, at those indexes, will allow us to select the right code for the desired language.
This can be used as [JS,PHP][+![]]
, to get the code of the right language.
On previous polyglots, I've used '\0'=="\0"
, which is true
in JavaScript (since \0
is parsed as the NULL-byte) and false
in PHP (the '\0'
won't be parsed as the NULL-byte, comparing the literal string \0
with the NULL-byte).
I'm happy that I've managed to reduce this check to +!'0'
.
I'm even more happy about @rckd, which reduced it to the current version!
From there on, it simply eval
s the code required.
PHP
PHP will execute echo$argv[1]**.5
(equivalent to echo sqrt($argv[1]);
, square-root the number), receiving the value from the 2nd argument and displays it in the standard output.
JavaScript
JavaScript executes alert((_=prompt())*_)
, which displays the squared number in an alert
.
Thank you to @rckd for saving 1 byte, and @user59178 for saving 3 bytes!
23+1 as a "Ha!" for all those people who treat C and C++ as the same thing. – DocMax – 2017-04-11T22:19:04.923
How does C interpret this to get n^2? Does
p
get set to0
? I'm reading it like C++ would and have no idea how C interprets it differently. – CAD97 – 2017-04-12T01:28:04.90720@CAD97: In C,
auto
means "allocate on the stack". The keyword is fairly useless because that's the default anyway, so C++ repurposed it to mean something else. In C, though, it doesn't express any opinion about the type ofp
(it's a storage class, not a type), so it counts as anint
by default (this default-to-int
behaviour is discouraged nowadays, and likely only exists because some of C's predecessors didn't have data types at all, but compilers still understand it). And of course,(int)0.5
is 0. – None – 2017-04-12T01:46:20.8472This is brilliant. – Quentin – 2017-04-12T08:23:07.330
How is
sizeof('-')
interpreted differently in C and C++? Is'-'
anint
in C? – YSC – 2017-04-12T08:55:23.263@YSC It does seem like that's the case for C, for C++ it's 1 indeed
– Gizmo – 2017-04-12T09:58:55.2831
Found a Stack Overflow question about it.
– YSC – 2017-04-12T10:47:12.943Been writing C for donkey's years, never seen "auto" before. Awesome. – Wossname – 2017-04-12T13:44:55.423
9I think the explanation for this answer would be improved by editing in @ais523's comment explaining why C produces
n^2
. – Brian J – 2017-04-12T14:59:17.357@BrianJ normally I put a full explanation in my answers, but in this case I found so many unexpected differences between the languages that I think it's more interesting for people to look up what's going on for themselves and stumble across some of the others. – Dave – 2017-04-12T18:42:58.567
The repurposing of
auto
in C++ was introduced in 2011. For all C++ standards before 2011, the version usingauto
would produce the same in both C and C++. Thesizeof
version works for all C and C++ standards. – Peter – 2017-04-14T06:36:00.773What about
#include<math.h> #define f(n) pow(n,sizeof('-')*1.5-1)
? It seems to be shorter than both. Or do codegolf rules recommend using functions instead of preprocessor macros? – Sasha – 2017-04-14T14:42:01.1601@Sasha
sizeof('-')*1.5-1
wouldn't work unless the machine has a 16-bitint
: it gives 5 ifint
is 32-bit or 11 ifint
is 64-bit. As for using macros rather than functions, you'd have to check meta for confirmation, but I haven't seen it done. – Dave – 2017-04-14T15:07:54.457@Dave, you're right. Then
#include<math.h> #define f(n) pow(n,sizeof('-')>1?2:.5)
. It's anyway shorter. – Sasha – 2017-04-14T16:43:45.267sizeof
doesn't need parentheses, so the longer version could just besizeof'-'-1?...
(or indeed with>
), but that's still longer than theauto
version. – hvd – 2017-04-15T20:27:15.123