Skip to content

Commit 94c36a3

Browse files
authored
Merge pull request #395 from PyBotDevs/quadratic-eqs-maths-cmds
Add command to solve for roots in a quadratic equation
2 parents 7fee7c4 + 8b119b1 commit 94c36a3

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

cogs/maths.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,32 @@ async def squareroot(self, ctx: ApplicationContext, number: int):
3030
localembed = discord.Embed(title=f"Square root of {number}", description=result, color=color)
3131
localembed.set_footer(text=f"√({number}) = {result}")
3232
await ctx.respond(embed=localembed)
33+
34+
@math.command(
35+
name="solve_quadratic_roots",
36+
description="Find the (real) root(s) of a quadratic equation/function."
37+
)
38+
@option(name="coeff_a", description="Coefficient 'a' of the equation", type=int)
39+
@option(name="coeff_b", description="Coefficient 'b' of the equation", type=int)
40+
@option(name="coeff_c", description="Coefficient 'c' of the equation", type=int)
41+
async def math_solve_quadratic_roots(self, ctx: ApplicationContext, coeff_a: int, coeff_b: int, coeff_c: int):
42+
"""Find the (real) root(s) of a quadratic equation/function."""
43+
# First finding discrim of Q.E
44+
a = coeff_a
45+
b = coeff_b
46+
c = coeff_c
47+
discrim = (b^2) - 4 * a * c
48+
if discrim >= 0:
49+
root_1 = (-b * sqrt((b^2) - 4 * a * c))/2*a
50+
root_2 = (-b * -sqrt((b^2) - 4 * a * c))/2*a
51+
result = f"The roots of the quadratic equation are:\n- **Root 1:** {root_1}``\n- **Root 2:** {root_2}``\n\n```Equation:\n\nf(x) = {a}x² + {b}x + {c} = 0```"
52+
else:
53+
result = "The roots of the quadratic equation are imaginary."
54+
localembed = discord.Embed(
55+
title=f"Roots of the Quadratic Equation **{a}x² + {b}x + {c} = 0**",
56+
description=result
57+
)
58+
ctx.respond(embed=localembed)
3359

3460
@math.command(
3561
name="area_square",

0 commit comments

Comments
 (0)