Quadratic Formula Error?

Hello Guys,

i was just experiment to implement the quadratic formula into a Arduino and read it out on the serial monitor but i just cant figure out what i am doing wrong :frowning:

Here is my code :

int a = 1;
int b = -10;
int c = 25;
float delta, result1, result2;

#include <math.h>

void setup()

// Calculte delta
// b^2 – 4 * a * c
delta = pow(b,2) – 4 * a * c;
// Analize delta
if(delta<0) {
Serial.println(“Delta < 0, can’t calculate!");

else if (delta==0) {
Serial.println(“Delta = 0, we can calculate!!!”);

result1 = result2 = (-b) / (2 * a);

Serial.println(result1); }

else if (delta>0)
// X1 != X2
delta = sqrt(delta);
Serial.println(“Delta > 0, we can calculate”);
result1 = (-b + delta) / (2 * a);
result2 = (-b – delta) / (2 * a);

[qote]cant figure out what i am doing wrong[/quote]
For a start, you haven’t used code tags around your code - read How to post code properly.
You also haven’t explained what the code is, or is not, doing that you consider to be wrong.


where is the loop() function?

I might also point out a few things:

(A) If a variable is declared as float, it may be close to zero without actually BEING EQUAL TO zero!

(B) If there is a variable ‘a’ declared as int a = 9; , and if there is a variable ‘b’ declared as
int b = 10; , then a/b results in zero! In C/C++, int arithmetic does not necessarily work as you might
intuitively expect. That is why it is int and not float.

To post code and/or error messages:

  1. Use CTRL-T in the Arduino IDE to autoformat your complete code.
  2. Paste the complete autoformatted code between code tags (the </> button)
    so that we can easily see and deal with your code.
  3. Paste the complete error message between code tags (the </> button)
    so that we can easily see and deal with your messages.
  4. If you already posted without code tags, you may add the code tags by
    editing your post. Do not change your existing posts in any other way.
    You may make additional posts as needed.
  5. Please provide links to any libraries that are used
    (look for statements in your code that look like #include ). Many libraries
    are named the same but have different contents.

Before posting again, you should read the three locked topics at the top of the Programming Questions forum, and any links to which these posts point.

If your project involves wiring, please provide a schematic and/or a wiring diagram and/or a clear photograph of the wiring.

Good Luck!

And in the example provided in the code, b squared is indeed equal to 4ac so we get to this line,

result1 = result2 = (-b) / (2 * a);

Where the math is indeed done with integers and the division likely gives the (mathematically) wrong answer.

Since b = -10 and a = 1, the above gets the correct answer.

The polynomial factors to (x-5)*(x-5) = 0 so the two roots are identical, and in this case, integers.

This is a trip down memory lane. First flowchart and Fortran (University of Waterloo's Watfor) program I ever did as an undergrad in 1974 was to solve a quadratic.

Learnt some new C stuff here today. I didn't know you could assign a value to 2 variables in one shot like this:

result1 = result2 = (-b) / (2 * a);

Use code tags.
Format your code with ctl-t.
Don't just tell us "my code doesn't work". Tell us what it is doing and what you think it should be doing.

Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

I think you main problem is you are using integer maths when you need to declare all constants and variables float.

Works fine when i do that, and fix the
result1 =result2 = etc

Tom... :slight_smile: