diff --git a/mathgenerator/funcs/basic_math/__init__.py b/mathgenerator/funcs/basic_math/__init__.py index 8e1a708..092f7bb 100644 --- a/mathgenerator/funcs/basic_math/__init__.py +++ b/mathgenerator/funcs/basic_math/__init__.py @@ -13,6 +13,7 @@ from .fraction_multiplication import * from .is_prime import * from .multiplication import * from .percentage import * +from .percentage_error import * from .power_of_powers import * from .square import * from .square_root import * diff --git a/mathgenerator/funcs/basic_math/percentage_error.py b/mathgenerator/funcs/basic_math/percentage_error.py new file mode 100644 index 0000000..41c0b20 --- /dev/null +++ b/mathgenerator/funcs/basic_math/percentage_error.py @@ -0,0 +1,28 @@ +from .__init__ import * + + +def gen_func(maxValue=100, minValue=-100, format='string'): + observed_value = random.randint(minValue, maxValue) + exact_value = random.randint(minValue, maxValue) + + if observed_value * exact_value < 0: + observed_value *= -1 + + error = (abs(observed_value - exact_value) / abs(exact_value)) * 100 + error = round(error, 2) + + if format == 'string': + problem = f"Find the percentage error when observed value equals {observed_value} and exact value equals {exact_value}." + solution = str(error) + "%" + return problem, solution + + elif format == 'latex': + return 'Latex unavailable' + + else: + return observed_value, exact_value, error + + +percentage_error = Generator( + "Percentage error", , gen_func, + ["maxValue=100", "minValue=-100"])