Commit f804e60f by likorn

cought! the errors and fixed the thing with infinite and complex numbers. i hope XD

parent cf63888d
Showing with 21 additions and 19 deletions
...@@ -2,7 +2,7 @@ import math ...@@ -2,7 +2,7 @@ import math
def input_float(text): def input_float(text):
# check if the input can be converted to float to prevent errors # check if the input can be converted to a float to prevent errors
inp = input(text) inp = input(text)
try: try:
float(inp) float(inp)
...@@ -21,19 +21,14 @@ def input_with_conditions(text, operator): ...@@ -21,19 +21,14 @@ def input_with_conditions(text, operator):
# check if the input fits the conditions listed in the task, i.e. # check if the input fits the conditions listed in the task, i.e.
# H > 0, C >=1 # H > 0, C >=1
# since we'll need to divide by A I also added A != 0 to prevent the errors # since we'll need to divide by A I also added A != 0 to prevent the errors
error_msg = "The value doesn't fit the conditions, try again "
value = input_float(text) value = input_float(text)
if (operator == '>' and value <= 0) or (operator == '>=' and value < 1): if (operator == '>' and value <= 0) or (operator == '>=' and value < 1):
return input_with_conditions(error_msg, operator) return input_with_conditions("The value doesn't fit the conditions, try again ", operator)
elif operator == '!=' and value <= 0:
error_msg = "Hey, sorry, we'll have to divide by A and get the square root of it, so all of the values less " \
"or equal to 0 are unacceptable "
return input_with_conditions(error_msg, operator)
return value return value
def calculate_x(until): def calculate_x(until):
# calculate x in the necessary points # calculate x in the necessary point
xx = a + h xx = a + h
for i in range(1, until + 1): for i in range(1, until + 1):
xx = xx + h * math.pow(c, i) xx = xx + h * math.pow(c, i)
...@@ -41,9 +36,14 @@ def calculate_x(until): ...@@ -41,9 +36,14 @@ def calculate_x(until):
def calculate_y(xx): def calculate_y(xx):
# calculate y according to the formula and return the (x, y) function in an array # calculate y according to the formula
yy = float(math.pow(xx, 2) + 20 * xx - 14) / math.sqrt(xx) - (1 + xx) / xx if xx < 0:
return round(yy, 3) return 'complex number'
elif xx == 0:
return 'infinite'
else:
yy = float(math.pow(xx, 2) + 20 * xx - 14) / math.sqrt(xx) - (1 + xx) / xx
return round(yy, 3)
def print_table(): def print_table():
...@@ -58,26 +58,28 @@ def print_table(): ...@@ -58,26 +58,28 @@ def print_table():
# input all the values: A, H, C, YM # input all the values: A, H, C, YM
a = input_with_conditions("Enter a starting value A = ", '!=') # a = input_with_conditions("Enter a starting value A = ", '!=')
a = input_float("Enter a starting value A = ")
h = input_with_conditions("Enter a step H = ", '>') h = input_with_conditions("Enter a step H = ", '>')
c = input_with_conditions("Enter the step's coefficient C = ", '>=') c = input_with_conditions("Enter the step's coefficient C = ", '>=')
ym = input_float("Enter function value upper limit YM = ") ym = input_float("Enter function value upper limit YM = ")
x = [] x = []
y = [] y = []
if calculate_y(a) <= ym: if calculate_y(a) == 'complex number' or calculate_y(a) == 'infinite' or calculate_y(a) <= ym:
x.append(round(a, 3)) x.append(round(a, 3))
y.append(calculate_y(a)) y.append(calculate_y(a))
if calculate_y(a + h) <= ym: if calculate_y(a + h) == 'complex number' or calculate_y(a + h) == 'infinite' or calculate_y(a + h) <= ym:
x.append(round(a + h, 3)) x.append(round(a + h, 3))
y.append(calculate_y(a + h)) y.append(calculate_y(a + h))
counter = 1 counter = 1
while counter < 14: while counter < 14:
x.append(calculate_x(counter)) if calculate_y(calculate_x(counter)) <= ym \
y.append(calculate_y(calculate_x(counter))) or calculate_y(calculate_x(counter)) == 'complex number' or calculate_y(calculate_x(counter)) == 'infinite':
counter += 1 x.append(calculate_x(counter))
if not y[len(y) - 1] <= ym: y.append(calculate_y(calculate_x(counter)))
y.remove(y[len(y) - 1]) counter += 1
else:
break break
print_table() print_table()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment