Open source C quadratic equation solver function

I worked for about 2 days on this before everything was ironed out. I included comments for how to use it and how it works internally. There are a few lines specifically that I am really proud of, like where I detect if there is no solution AND prevent negative values from being passed into a sqrt() function in one stroke. The math may look a bit weird (using b*b instead of b^2), but anything that appears out of place is deliberate. The core maths to making this work only needs 2 lines, but the rest here makes it more powerful and maybe a tiny bit faster. Without farther ado, here is the function. (Had problems with formatting)

Now make it return imaginary solutions as well :stuck_out_tongue:

I’m in 8th grade. Please cut me some slack ;-;
At least I have it detect when the solution is imaginary

1 Like

8th grade, I have to say I’m pretty impressed. I probably could have done it, but probably not as simple as that.

You inspired me to write one in Java, which currently only takes integer input but gives complex output.

1 Like

i remember messing around with code like this in 6th
its all entertaining and god damn my code style was horrid =3=

10/10, zwei

Great job! By the way, b^2-4ac (what you have defined as ‘ppcsr’) is called the discriminant.

1 Like

Got it. I know the quadratic formula is derived from completing the square of the ax^2+bx+c=0, but I only know the labels coefficient and constant :stuck_out_tongue:

Also @kenzierocks, I cannot use java for the life of me. Object oriented stuff is too hard, so I do all my work in C/C++

Turns out imaginary numbers are EZ, so I will try to implement. The hardest part is simplifying the fraction XD

Currently running Recuva because I did this in C++ and somehow lost the source… Also, for 8th grade, not bad at all. I would suggest that you look into OOP though, can be very useful, if you use it right.

Why don’t you check if ppcsr is 0 which is equivalent to pcsr being 0 before doing the square root to make it even more efficient?

Oops! I was using pcsr in the calculation before I realized that it was not necessary. I will move it over in my VS project. Thanks for pointing that out!