I’m engaged on a binary calculator app in iOS swift/SwiftUI, regular calculation are working fantastic in that calculator as
- I take enter in binary and convert binary enter to Int
- calculate its (Int) outcome
- convert outcome again to the binary and present that transformed outcome on display.
The issue occurred within the trigonometry features like sin, cos, tan, sinh, cosh, tanh,
Enter binary 101 = 5
in decimal. Now
sin(5)
result’s:
diploma is -0.95892427466
radina is 0.08715574274
These values are within the floating quantity, how one can convert these values within the binary quantity precisely in swift?
As you may see these values are in detrimental and likewise within the floating factors.
My code is true now
let parser = MathParser(enableImpliedMultiplication: true)
MathParser is the library that I’m utilizing for caculation
expression = sin(5)*sin(5)
separator = *
let elements = expression.elements(separatedBy: seprator)
if elements.depend > 1 {
// Get the 2 binary strings
var newString = ""
for element in elements {
if element.incorporates("sin") || element.incorporates("cos") || element.incorporates("tan") ||
element.incorporates("sinh") || element.incorporates("cosh") || element.incorporates("tanh") {
// I wish to do work right here
} else {
// Right here I'm performing some binary calculations like 101*101 (base 2) = 5*5 (base 10) and that is working fantastic
newString += "(binaryStringToInt(element) ?? 0) (seprator)"
}
}
newString.removeLast(seprator.depend)
let worth = parser.parse("(newString)")?.worth
return worth
}
These are the helper strategies
// Perform to transform a binary string to an integer
func binaryStringToInt(_ binaryString: String) -> Int? {
return Int(binaryString, radix: 2)
}
// Perform to transform an integer to a binary string
func intToBinaryString(_ worth: Int) -> String {
return String(worth, radix: 2)
}
Do not get confuse with the expression and separator, its the precise code in below course of. I must do these items in later, however proper now my downside is
Convert floating or Adverse values within the binary quantity.