kirbah commited on
Commit
303a606
·
verified ·
1 Parent(s): 38cee1c

Update app.py

Browse files

fix error - InterpreterError: The variable math is not defined.

Files changed (1) hide show
  1. app.py +4 -3
app.py CHANGED
@@ -13,7 +13,7 @@ def monte_carlo_integration(func_expr: str, a: float, b: float, samples: int = 1
13
  """A tool that computes the approximate definite integral of a given function between limits a and b using Monte Carlo integration.
14
 
15
  Args:
16
- func_expr: A string representing a mathematical function of x (e.g., "x**2 + 3*x + 2").
17
  a: The lower bound of integration.
18
  b: The upper bound of integration.
19
  samples: The number of random samples to use for the approximation. Defaults to 10000.
@@ -24,9 +24,10 @@ def monte_carlo_integration(func_expr: str, a: float, b: float, samples: int = 1
24
  import random
25
  import math
26
 
27
- # Define a safe evaluation context with math functions
28
  safe_dict = {k: getattr(math, k) for k in dir(math) if not k.startswith("__")}
29
-
 
30
  # Define the function to integrate safely using eval
31
  def f(x):
32
  try:
 
13
  """A tool that computes the approximate definite integral of a given function between limits a and b using Monte Carlo integration.
14
 
15
  Args:
16
+ func_expr: A string representing a mathematical function of x (e.g., "x**2 + 3*x + 2" or "math.sin(x)").
17
  a: The lower bound of integration.
18
  b: The upper bound of integration.
19
  samples: The number of random samples to use for the approximation. Defaults to 10000.
 
24
  import random
25
  import math
26
 
27
+ # Create a safe dictionary of math functions and add the math module itself.
28
  safe_dict = {k: getattr(math, k) for k in dir(math) if not k.startswith("__")}
29
+ safe_dict["math"] = math # Allow expressions to use the 'math' prefix
30
+
31
  # Define the function to integrate safely using eval
32
  def f(x):
33
  try: