How to create math function that converts decimal number to binary format.
Let's create a mathematical function that converts decimal to binary using the coding function of the function calculator.
Copy the code below and paste it into the code window of the function calculator's factory page, click the run button to run the code, and click the save button to save the mathematical function.
def toBinary(num)
= toBinary((num−num%2)/2)*10+num%2;
toBinary(0)=0;
toBinary(1)=1;
You can go back to the calculator page and press the F2 button to use the function you just created in your calculations as shown below.
The above code is defined recursively.
The result of the calculation is a decimal number consisting of 1 and 0, which looks like a binary number.
In simple terms, the remainder of the given number divided by 2 is placed at the end of the number, and the binary quotient of the division by 2 is placed one digit higher than that.
Convert binary to decimal
This time we are going to create a math function that converts binary number to decimal number.
Similar to the above, but with the opposite logic, you can code it as follows.
def toDecimal(num)
=toDecimal((num−num%10)/10)*2+num%10;
toDecimal(0)=0;
toDecimal(1)=1;
Copy the code above and paste it into the code window of the function calculator's factory page, click the run button to run the code, and click the save button to save the mathematical function.
You can go back to the calculator page and press the F2 button to use the function you just created in your calculations as shown below.
Create functions that compute the four basic operations between binary numbers.
You can use the functions created above as auxiliary functions and use the switch statement to create a function that performs arithmetic operations on two binary numbers, as in the code below.
def binaryCalculator(bin1,bin2,operator){
var plus=1;
var minus=2;
var multiply=3;
var division=4;
var noAnswer=9;
var result;
var num1=toDecimal(bin1);
var num2=toDecimal(bin2);
switch(operator){
case(plus):
result=num1+num2;
break;
case(minus):
result=num1−num2;
break;
case(multiply):
result=num1*num2;
break;
case(division):
if(num2==0){
return noAnswer;
}
elseif(num1==0){
return 0;
}
result=num1/num2;
break;
default:
return noAnswer;
}
var lessThan0=result<0;
var absResult=floor(abs(result));
var binResult=toBinary(absResult);
if(lessThan0){
return -binResult;
}
return binResult;
}
The above function is a function that calculates the result in binary form when two binary numbers and a number representing a calculation sign are inputted. If the calculation sign is 1, it adds. If the calculation sign is 2, it subtracts. If the calculation sign is 3, it multiplies. If the calculation sign is 4, it divides. It calculates each of these and outputs the result in binary form. However, the division calculation result here is calculated only as an approximation. If there is no answer, it outputs 9. Using the above function, you can create each binary arithmetic operation function as follows.
def addBin(bin1,bin2)=binaryCalculator(bin1,bin2,1);
def subtractBin(bin1,bin2)=binaryCalculator(bin1,bin2,2);
def multiplyBin(bin1,bin2)=binaryCalculator(bin1,bin2,3);
def divideBin(bin1,bin2)=binaryCalculator(bin1,bin2,4);
Copy all the above code and paste it into the factory page of the function calculator, press the run button to run the code, and press the save button to save the functions. Go to the calculator page and press the F2 button to use it for calculations as shown in the figure below.
Create and use a variety of useful mathematical functions using the function calculator.
Comments
Post a Comment