diff --git a/client/ios/DivKit/Expressions/Functions/TrigonometricFunctions.swift b/client/ios/DivKit/Expressions/Functions/TrigonometricFunctions.swift index e4527ba1d..76d64ff69 100644 --- a/client/ios/DivKit/Expressions/Functions/TrigonometricFunctions.swift +++ b/client/ios/DivKit/Expressions/Functions/TrigonometricFunctions.swift @@ -11,6 +11,7 @@ extension [String: Function] { addFunction("atan", _atanFunction) addFunction("toRadians", _toRadians) addFunction("toDegrees", _toDegrees) + addFunction("cot", _cotFunction) } } @@ -26,6 +27,14 @@ private let _tanFunction = FunctionUnary { (radians: Double) in tan(radians) } +private let _cotFunction = FunctionUnary { (radians: Double) in + let result = tan(radians) + if result.isAlmostZero() { + throw ExpressionError("Cotangent is undefined for the given value.") + } + return 1 / result +} + private let _asinFunction = FunctionUnary { (value: Double) in let result = asin(value) if result.isNaN { diff --git a/test_data/expression_test_data/function_signatures_trigonometry.json b/test_data/expression_test_data/function_signatures_trigonometry.json index 0ab478a77..3eb095d4c 100644 --- a/test_data/expression_test_data/function_signatures_trigonometry.json +++ b/test_data/expression_test_data/function_signatures_trigonometry.json @@ -137,6 +137,20 @@ "ios", "web" ] + }, + { + "function_name": "cot", + "doc": "Calculates the cotangent of the given angle in radians", + "arguments": [ + { + "type": "number", + "doc": "Angle in radians." + } + ], + "result_type": "number", + "platforms": [ + "ios" + ] } ] } diff --git a/test_data/expression_test_data/functions_trigonometry.json b/test_data/expression_test_data/functions_trigonometry.json index 366ead322..b4e62af9c 100644 --- a/test_data/expression_test_data/functions_trigonometry.json +++ b/test_data/expression_test_data/functions_trigonometry.json @@ -351,6 +351,73 @@ "ios", "web" ] + }, + { + "expression": "@{cot(0.0)}", + "expected": { + "type": "error", + "value": "Failed to evaluate [cot(0.0)]. Cotangent is undefined for the given value." + }, + "platforms": [ + "ios" + ] + }, + { + "expression": "@{cot(pi())}", + "expected": { + "type": "error", + "value": "Failed to evaluate [cot(3.141592653589793)]. Cotangent is undefined for the given value." + }, + "platforms": [ + "ios" + ] + }, + { + "expression": "@{cot(pi() / 2)}", + "expected": { + "type": "number", + "value": 0.0 + }, + "platforms": [ + "ios" + ] + }, + { + "expression": "@{cot(pi() / 4)}", + "expected": { + "type": "number", + "value": 1.0 + }, + "platforms": [ + "ios" + ] + }, + { + "expression": "@{cot(-pi() / 4)}", + "expected": { + "type": "number", + "value": -1.0 + }, + "platforms": [ + "ios" + ] + }, + { + "expression": "@{cot(var)}", + "expected": { + "type": "number", + "value": 0.57735026919 + }, + "variables": [ + { + "name": "var", + "type": "number", + "value": 1.0471975512 + } + ], + "platforms": [ + "ios" + ] } ] }