counting / maths /geometry /inverse_trig_functions.py
spagestic's picture
feat: enhance Gradio interfaces for trigonometric functions with detailed descriptions and examples
d29336a
import math
import gradio as gr
def inverse_trig_functions(value: float, function_name: str) -> str:
"""
Calculates inverse trigonometric functions (asin, acos, atan) in degrees.
Args:
value: The value to calculate the inverse trigonometric function for.
For asin and acos, must be between -1 and 1.
function_name: "asin", "acos", or "atan".
Returns:
A string representing the result in degrees, or an error message.
"""
if not isinstance(value, (int, float)):
return "Error: Input value must be a number."
func_name = function_name.lower()
result_rad = 0.0
if func_name == "asin":
if -1 <= value <= 1:
result_rad = math.asin(value)
else:
return "Error: Input for asin must be between -1 and 1."
elif func_name == "acos":
if -1 <= value <= 1:
result_rad = math.acos(value)
else:
return "Error: Input for acos must be between -1 and 1."
elif func_name == "atan":
result_rad = math.atan(value)
else:
return "Error: Invalid function name. Choose 'asin', 'acos', or 'atan'."
return f"{math.degrees(result_rad):.4f} degrees"
inverse_trig_interface = gr.Interface(
fn=inverse_trig_functions,
inputs=[
gr.Number(label="Value"),
gr.Radio(["asin", "acos", "atan"], label="Inverse Function")
],
outputs="text",
title="Inverse Trigonometry Calculator",
description="Calculate inverse trigonometric functions (asin, acos, atan) and get the result in degrees. For asin and acos, input must be between -1 and 1. Example: asin(0.5) = 30 degrees.",
examples=[
[0.5, "asin"],
[0.5, "acos"],
[1, "atan"],
[-1, "asin"]
]
)