In many mathematical tasks you need functions to calculate powers, roots or logarithms. Gambas provides you with various functions for this purpose.
Since dealing with potencies, roots and logarithms is not part of the daily exercises, three basic considerations are put before the description of the corresponding Gambas functions:
The mathematical operation' Logarithmic operation' hides nothing else than the 'exponent search' for a given number (potency value) and a given basis. If logarithm = log10 (1000) the exponent is searched for, for which 10exponent = 1000. The exponent you are looking for is 3, because 10³ = 1000, which had to be said before you logarithmically start like wild on it.. .
Function | Description |
---|---|
potency value = Exp(exponent AS Float) AS Float | Calculates the power value p to base e with the exponent' exponent' (p=eexponent). The exponent must be smaller than 709.779999 to avoid overflow errors. The number e is the Euler number. |
potency value = Expm (exponent AS Float) AS Float | Calculates the power value p to base e with the exponent' exponent' for very small exponents. The following applies: Expm(x) = Exp(x) - 1. |
potency value = Exp2 (exponent AS Float) AS Float | Calculates the power value p for base 2 with the exponent' exponent' (p = 2exponent). |
potency value = Exp10 (exponent AS Float) AS Float | Calculates the power value p to base 10 with the exponent' exponent' (p = 10exponent). |
Table 9.6.2.1: Operation: Potentiating
Function | Description |
---|---|
2root = Sqr(number AS Float) AS Float | Calculates the 2nd root of a number. The real number' number' must not be negative (number? 0). |
3root = Cbr(number AS Float) AS Float | Calculates the cubic root of a number. The real number' number' can be 0 (zero), negative or positive. |
Table 9.6.2.1: Operation: root extraction
Function | Description |
---|---|
exponent = Log (numerus AS Float) As Float | Calculates the natural logarithm of a number (numerus) to the base e (Euler's number) with logarithm = ln(numerus). The symbol' ln' stands for logaritmus naturalis. The number (numerus) must be greater than zero. We are looking for the exponent for which eexponent = numerus applies. |
exponent = Logp (numerus AS Float) AS Float | Calculates the logarithm to base e for very small numeric values. The following applies: Logp(x) = Log(1 + x). |
exponent = Log2 (numerus AS Float) AS Float | Calculates the logarithm of a number (numerus) to base 2. the number must be greater than zero. We are looking for the exponent for which 2exponent = numerus applies. |
exponent = Log10 (number AS Float) AS Float | Calculates the logarithm of a number (numerus) to the base 10.10. The number must be greater than zero. We are looking for the exponent for which 10exponent = numerus applies. |
Table 9.6.4.1: Operation: Logarithmic logging
Hints:
The calculation of the 5th root from the real number 83, for example, requires the use of some of the above-mentioned functions, which is also reflected in the source code extract:
w = (83)^0.2 ln(w) = 0.5 * ln(83) = 0,88376812155932 w = e^ln(w) = e^0,88376812155932 = 2,42000140696596
The sample is executed in a similar way:
p = (2,42000140696596)^5 ln(p) = 5 * ln(2,42000140696596) = 4,4188406077966 p = e^(5 * ln(2,42000140696596))
Public Sub btn5Root83_Click() Dim w As Float Print "ln(w) = "; 0.2 * Log(83) w = Exp(0.2 * Log(83)) Print "w = "; Exp(0.2 * Log(83)) Print "Probe 1 (trivial) = "; w * w * w * w * w Print "ln(p) = "; 5 * Log(Exp(0.2 * Log(83))) Print "Potenzwert = "; Exp(5 * Log(Exp(0.2 * Log(83)))) End
Output in the console of the IDE:
ln(w) = 0,88376812155932 w = 2,42000140696596 Probe 1 (trivial) = 83,0000000000001 ln(p) = 4,4188406077966 Potenzwert = 83
The calculation of the power value a^b for a > 0 and any b (a, b ∈ ℝ) works with the following approach: a^b = Exp (b*Log (a)) and delivers 100 in the next calculation:
a = Cbr(100) ' 3. root of 100 b = 3 Print Exp(b * Log(a))
For a = Cbr(100) and b = 6, the approximate value is 9999.999999999999999 ≈ 10000.
In vielen mathematischen Aufgaben benötigen Sie Funktionen, um Potenzen, Wurzeln oder Logarithmen zu berechnen. Gambas stellt Ihnen für diese Zwecke diverse Funktionen zur Verfügung.
Da der Umgang mit Potenzen, Wurzeln und Logarithmen nicht zu den täglichen Übungen zählt, werden drei grundlegende Betrachtungen der Beschreibung der entsprechenden Gambas-Funktionen vorangestellt:
Hinter der mathematischen Operation 'Logarithmieren' verbirgt sich nichts anderes als die 'Exponentensuche' für eine vorgegebene Zahl (Potenzwert) und gegebener Basis. Bei logarithmus = log10(1000) wird der Exponent gesucht, für den 10exponent = 1000 ist. Der gesuchte Exponent ist 3, denn 10³ = 1000. Das musste einmal gesagt werden, bevor Sie wie wild drauf los logarithmieren … .
Tabelle 9.6.2.1: Operation: Potenzieren
Funktion | Beschreibung |
---|---|
2root = Sqr ( number AS Float ) AS Float | Berechnet die 2. Wurzel einer Zahl. Die reelle Zahl 'number' darf nicht negativ sein (number ≥ 0). |
3root = Cbr ( number AS Float ) AS Float | Berechnet die Kubikwurzel einer Zahl. Die reelle Zahl 'number' kann 0 (Null), negativ oder positiv sein. |
Tabelle 9.6.2.1: Operation: Radizieren
Tabelle 9.6.4.1: Operation: Logarithmieren
Hinweise:
Die Berechnung der 5. Wurzel aus der reellen Zahl 83 erfordert zum Beispiel den Einsatz einiger der o.a. Funktionen, was sich auch im u.a. Quelltext-Ausschnitt widerspiegelt:
w = (83)^0.2 ln(w) = 0.5 * ln(83) = 0,88376812155932 w = e^ln(w) = e^0,88376812155932 = 2,42000140696596
Die Probe wird ähnlich ausgeführt:
p = (2,42000140696596)^5 ln(p) = 5 * ln(2,42000140696596) = 4,4188406077966 p = e^(5 * ln(2,42000140696596))
Public Sub btn5Root83_Click() Dim w As Float Print "ln(w) = "; 0.2 * Log(83) w = Exp(0.2 * Log(83)) Print "w = "; Exp(0.2 * Log(83)) Print "Probe 1 (trivial) = "; w * w * w * w * w Print "ln(p) = "; 5 * Log(Exp(0.2 * Log(83))) Print "Potenzwert = "; Exp(5 * Log(Exp(0.2 * Log(83)))) End
Ausgabe in der Konsole der IDE:
ln(w) = 0,88376812155932 w = 2,42000140696596 Probe 1 (trivial) = 83,0000000000001 ln(p) = 4,4188406077966 Potenzwert = 83
Die Berechnung des Potenzwerts a^b für a > 0 und beliebige b (a,b ∈ ℝ) funktioniert über den folgenden Ansatz problemlos: a^b = Exp(b*Log(a)) und liefert 100 in der nächsten Berechnung:
a = Cbr(100) ' 3. Wurzel aus 100 b = 3 Print Exp(b * Log(a))
Für a = Cbr(100) und b = 6 ergibt sich der Näherungswert 9999,99999999999 ≈ 10000.