Tag Archives: discriminant

Quadratic equation – part II

In the previous post we created a simple program to solve a quadratic equation. We used the well known method from school:

"Quadratic equation:
'a*x^2 + b*x + c = 0
a = 3 ', ' b = 4 ', ' c = -5
'Discriminant - ' D = b^2 - 4*a*c
x_1 = (-b - sqr(D))/(2*a)
x_2 = (-b + sqr(D))/(2*a)

In this post we will make our program more professional and add some advanced features. Currently, it is fine for personal use, but if we want to give it to other users, we need to work a little bit harder.

For example, let’s try to enter a = 0 and see what happens. In fact, it will turn into a linear equation, which solution is x = -c/b. However, our program will end up with error due to division by zero. Calcpad will show -∞, +∞ or undefined, if we have 0/0 as it will happen for x_2. It is similar for the case of a negative discriminant D < 0 (unless we use complex numbers).

This is not very good. Every professional program must work correctly for any possible input data. It should either provide a sensible result or a comprehensible message to the user. But how to do this with Calcpad?

The good news is that Calcpad supports boolean expressions and program flow control as follows:

#if 
    <code>
#else if 
    <code>
...
#else
    <code>
#end if

This is a very powerful feature. It allows you to split the solution entirely, depending on some intermediate values. It is not just about using different formulas. You can even display different text and images into the output. It is very difficult to do this with Excel.

Dots “...” mean that you can add as many conditions as you need (do not place the dots into the code) Instead of <condition> you can put any expression that compares values, e.g. a ≡ 0 or D < 0. So, lets add some logic into our program as follows:

"Quadratic equation:
'a*x^2 + b*x + c = 0
a = 3 ', ' b = 4 ', ' c = -5
#if a ≡ 0
    'The coefficient a is equal to zero. The equation is linear:
    x = -c/b
#else
    'Discriminant - ' D = b^2 - 4*a*c
    #if D ≥ 0
        'The discriminant is greater or equal to zero. Real roots exist.
        q = -0.5*(b + sign(b)*sqr(D))
        x_1 = q/a
        x_2 = c/q
    #else
        'The discriminant is less than zero. There are no real roots.
    #end if
#end if

Now, the program will display the correct answer in any case. You can copy the above script into Calcpad and try for a = 0 or b = 5. We also made another improvement: The classical formula x1,2 = (-b ± sqr(D))/(2·a) is numerically unstable. The answer will be inaccurate due to the approximate representation of floating point numbers by the computer. The new formula solves this problem.

So far, so good. We managed to improve our program, but it is still not very convenient for other people to use it. When the program gets larger and more complicated, it becomes difficult for users to work directly with the source code and find where to change the input values. There is also a risk to accidentally damage some formulas and produce incorrect results.

That is why, Calcpad provides a quick way to generate input forms directly from the source code. All you need is to put question marks ? wherever you want to enter values. In our program, we need to change the third line as follows:

a = ? ', ' b = ? ', ' c = ?

Then press the “form” button (left to the orange arrow) and you will get the input form:

Input form

But how we made the solution details to disappear during the input? In Calcpad, you can select which part of the output will be visible at each stage by using special keywords:

  • #hide – always hide the content after this place ;
  • #pre   – show the content only in input mode (before calculations);
  • #post – show the content only in output mode (after calculations);
  • #show – always show the remaining content (cancel);

If you want to hide all calculation details during the input, just add #post after the third line. There is also another method for hiding. Since all the output is finally formatted as Html, you can include Html tags and CSS in Calcpad comments. You can use this to enclose some contents in Html comments and it will hide as well:

'<!--''-->

Do not use this to hide sensitive information because it can be seen in the Html source. For that purpose, we strongly recommend to use #hide. It entirely skips the content in the output Html.

So, our first program is ready. It is pretty much like the one on the official Calcpad website:

http://calcpad.net/Worksheet/246/quadratic-equation

The only additional thing is the function graph, but I am going to write an whole new post about graphing. Bellow is the final source code for the quadratic equation program. You can copy it an use it freely:

"Quadratic equation:
'a·x<sup>2</sup> + b·x + c = 0
a = ? ', ' b = ? ', ' c = ?
#post
#if '<!--'a ≡ 0'-->
    'The coefficient a is equal to zero. The equation is linear:
    x = -c/b
#else
    'Discriminant - ' D = b^2 - 4*a*c
    #if '<!--'D ≥ 0'-->
        'The discriminant is greater or equal to zero. Real roots exist.
        q = -0.5*(b + sign(b)*sqr(D))
        x_1 = q/a
        x_2 = c/q
    #else
        'The discriminant is less than zero. There are no real roots.
    #end if
#end if

My first program – quadratic equation

Lets write our first program with Calcpad. Most programming languages usually start with “Hello world!”, but for Calcpad, there is nothing much to do, except to write the text in quotes:

"Hello world from Calcpad!"

Then press Enter and the text will appear in the output.

So, let’s do something more useful and solve a real problem. The quadratic equation is simple enough to start with.

All you need to do is to enter the formulas and the text in the “Script” box on the left. Then press Enter or the orange arrow and you will get the results in the “Output” box on the right:

Quadratic equation

Bellow, you can copy the source code and try it by yourself:

"Quadratic equation:
'a*x^2 + b*x + c = 0
a = 3 ', ' b = 4 ', ' c = -5
'Discriminant - ' D = b^2 - 4*a*c
x_1 = (-b - sqr(D))/(2*a)
x_2 = (-b + Sqr(D))/(2*a)

All comments must be enclosed in quotes: double quotes for “titles” and single quotes for ‘simple text’. In this example, "Quadratic equation is a title and 'Discriminant - ' is text. The closing quote can be omitted, if there is nothing further on the line.

Everything outside the quotes is assumed to be math expressions.  Normally, each expression should be on a new line. If they are several expressions on the same line, they must be separated by comments, e.g. 

a = 3 ', ' b = 4 ', ' c = -5

Expressions are defined by using the commonly accepted mathematical notation: numbers, variables, operators “^ / \ % * - + ≡ ≠ ≤ ≥ =” and standard mathematical functions. A short description of the available syntax is initially displayed in the output box. For more information, please check the user manual:

http://calcpad.net/Help/Expressions

The output is automatically formatted in a professional looking Html report, ready for printing:

  • the sqr function is replaced by the square root symbol;
  • the “^” operator is formatted as superscript;
  • underscores “_” in variable names are replaced by subscripts etc.

All formulas are rendered into the calculation report. Additionally, Calcpad can show variable substitutions, so you can check every value in the output. This is a great advantage over Excel spreadsheets, where the actual formulas are hidden in the cells. It is important for structural calculations to be presented in a way that allows them to be easily followed and verified by the approving authorities.

In the next publication we will add more features to our quadratic equation program.