Extinction in Pascal programming language: tips and tricks

There are a huge number of programming languages, and Pascal is not the last among them. And if you are going to seriously engage in programming in the future, you should start acquaintance with this world precisely from studying this language, since it is easier to perceive and, importantly, the program is absolutely free.

In the article we will analyze such a difficult problem as exponentiation. Pascal, alas, does not provide us with a separate operator for solving this problem, unlike other programming languages. So here, as they say, we have to get out with improvised means, resorting to cycles and mathematical operators - this is where we can give free rein to our imagination. Consider several methods for solving this problem.

Let us be given the simplest task, where a certain number must be raised to an integer positive degree. Suppose we raise the number a to 4 degrees. A simple mathematical operation is performed here: b: = a * a * a * a.

Program text
In the next step, we will complicate the task and compose a universal program that will raise any number to any integer positive degree. There is an opportunity to use any loop, but we will consider a simpler method using a loop with a counter. In the image on the left you can see the full text of our program, there are also explanations of the operations performed. By the way, it should be noted that the raised number a does not need to be specified by Integer, but it is possible, say, Real, which allows raising a fractional number to a power.

The previous examples allow us to raise numbers only to integer positive degrees. But there are problems where it is necessary to raise a number to a fractional power. When writing such a program, we need knowledge of the properties of logarithms. In particular: a b = e b ln a . Based on this, the desired fragment of our program will look like: r: = exp (b * ln (a)) . But here we are faced with the fact that this operator does not work with zero and negative numbers. In order for our program to carry out raising to the power of 0, we need to set the condition: If b = 0 Then r: = 1 Else r: = exp (b * ln (a)). But what does exponentiation look like for a negative number?

Program text

Pascal makes us think again. Here we have to perform this operation with the module of our number and take data with a negative result. Then check the parity of the degree: if our degree was even, then we take the module from the result. In this case, our program will look like: r: = (- 1) * exp (b * ln (abs (a))); If Round (b / 2) = b / 2 Then r: = abs (r). The condition here checks whether the degree is even or not.

As a result, we come to a more universal model of our program, which will work with any numbers. That is, now we must combine all of the above into a single whole. In the image on the right, you can see the full text of our finished program. Pay attention to the given data type. Unlike the first program, Real is used here, because here we are already working with any numbers, not just integers. So, raising to the power of real numbers is fully considered by us. It remains to consider only one question.

Program text
Turning to it, it should be noted that in solving this problem, sufficiently deep knowledge in programming is required. This is exponentiation of a complex number. Here you can try to use various solutions, for example, the Moire formula, but there are difficulties with the translation of a complex number into a trigonometric form. There is a solution to this problem in the task of multiplying two complex numbers and a simple cycle with a counter, i.e. repeating this procedure is equal to the number of times. In the provided example, you can deal with the text of this program in more detail.

Source: https://habr.com/ru/post/K13622/


All Articles