1+1 = 10, 1+2 = 3
JavaScript ES6, 87 971 1002 1063 102 101 98 1004 93 88 86 bytes
e=>eval(e.match`[2-9]`?e:`(${e.replace(/[\d.]+/g,"('0b'+$&e14)/16384")}).toString(2)`)
Demo + explanation:
function c(e){
return eval(
e.match`[2-9]`? //check if there are numbers 2 to 9
e: //if there're, just compute the result
"("+
e.replace( //otherwise replace...
/[\d.]+/g, //any number...
"(('0b'+$&e14)/16384)" //...with itself converted to base 10
)
+").toString(2)" //compute the result and convert it to binary
)
}
document.write(
c("1.1*1.1")+"<br>"+
c("1010+10-1")+"<br>"+
c("102+10-1")+"<br>"+
c("1+2+3")+"<br>"+
c("10*10*10")+"<br>"+
c("11*11*11")+"<br>"+
c("10*11*12+1")+"<br>"+
c("10.1*10.1")+"<br>"+
c("20.2*20.2")+"<br>"+
c("10/5")+"<br>"+
c(`10
+
10
-
1`)
)
1 - forgot about floats
2 - again floats problem: parseInt floors binary so I have to multiply by 1e14 and then divide by 16384
3 - hope that's achieved the given task, now start to golf :D
4 - there was a bug with dividing
Japt, 77 72 62 60 62* 60 59 51 bytes
OvUf"[2-9]" ?U:"({Ur"[\\d.]+""º$&e14+P n2 /2pE¹"})¤
Explanation (more or less the same as for the JS answer):
Ov //eval...
Uf"[2-9]" //if input contains the digits 2 to 9
U: //then it's base 10, just compute
Ur"[\\d.]+" //otherwise replace all the numbers
"º$&e14+P n2 /2pE¹" //with their base 10 equivalents
//I.e., take every number, multiple by 10^14, convert to
//base 10 and divide by 2^14
// º and ¹ are multiple brackets
¤ //means "s2", i.e. convert the result to binary
Try it online!
* didn't divide properly
Jolf, 31 bytes, noncompeting
I added a decent amount of functions inspired by this challenge, and, as thus, it is considered noncompeting. I'm happy because I finally implemented unary functions (like (H,S,n)=>val
in ES6, but are supported in ES5!)
? hi"[2-9]"~eiB~epT mpvid|m'H2H
? hi"[2-9]" if the input contains any of 2..9
~ei evaluate i (implicitly print)
else
_mpvid map the input split into number groups
m'H2 to H as a binary float
| H (or keep H, if that doesn't work)
pT join by spaces
~e evaluate
B convert to binary (implicitly print)
Test suite, Try your own input, or manually set the input.