mirror of
https://github.com/QB64Official/qb64.git
synced 2024-08-22 09:05:08 +00:00
44 lines
2.1 KiB
Text
44 lines
2.1 KiB
Text
The [[LOG]] math function returns the natural logarithm of a specified numerical value.
|
|
|
|
|
|
{{PageSyntax}}
|
|
: {{Parameter|logarithm!}} = [[LOG]]({{Parameter|value}})
|
|
|
|
|
|
{{PageDescription}}
|
|
* {{Parameter|value}} MUST be greater than 0. [[ERROR Codes|"Illegal function call" error]] occurs if negative or zero values are used.
|
|
* The natural logarithm is the logarithm to the base '''e = 2.718282''' (approximately).
|
|
* The natural logarithm of ''a'' is defined as the integral from 1 to ''a'' of dx/x.
|
|
* Returns are default [[SINGLE]] precision unless the value parameter uses [[DOUBLE]] precision.
|
|
|
|
|
|
{{PageExamples}}
|
|
''Example 1:'' [[FUNCTION]] to find the base ten logarithm of a numerical value.
|
|
{{CodeStart}}
|
|
{{Cl|FUNCTION}} Log10#(value {{Cl|AS}} {{Cl|DOUBLE}}) {{Cl|STATIC}}
|
|
Log10# = {{Cl|LOG}}(value) / LOG(10.#)
|
|
{{Cl|END FUNCTION}} '' ''
|
|
{{CodeEnd}}
|
|
|
|
:''Explanation:'' The natural logarithm of the value is divided by the base 10 logarithm. The LOG of ten is designated as a DOUBLE precision return by using # after the Log10 value. The return tells you the number of times 10 goes into a value.
|
|
|
|
|
|
''Example 2:'' A binary FUNCTION to convert [[INTEGER]] values using LOG to find the number of digits the return will be.
|
|
{{CodeStart}} '' ''
|
|
{{Cl|FUNCTION}} BIN$ (n&)
|
|
{{Cl|IF}} n& < 0 {{Cl|THEN}} {{Cl|EXIT FUNCTION}} 'positive numbers only! negative error!
|
|
{{Cl|FOR}} p% = 0 {{Cl|TO}} {{Cl|INT}}({{Cl|LOG}}(n& + .1) / {{Cl|LOG}}(2)) 'added +.1 to get 0 to work
|
|
{{Cl|IF}} n& {{Cl|AND}} 2 ^ p% {{Cl|THEN}} s$ = "1" + s$ {{Cl|ELSE}} s$ = "0" + s$ 'find bits on
|
|
{{Cl|NEXT}} p%
|
|
{{Cl|IF}} s$ = "" {{Cl|THEN}} BIN$ = "&B0" {{Cl|ELSE}} BIN$ = "&B" + s$ 'check for zero return '' ''
|
|
{{Cl|END FUNCTION}}
|
|
|
|
{{CodeEnd}}
|
|
: ''Explanation:'' The LOG of a '''positive''' [[INTEGER]] value is divided by the LOG of 2 to determine the number of binary digits that will be returned. The FOR loop compares the value with the exponents of two and determines if a bit is ON or OFF as "1" or "0".
|
|
|
|
|
|
{{PageSeeAlso}}
|
|
*[[EXP]], [[&B]] (binary number)
|
|
|
|
|
|
{{PageNavigation}}
|