# Difference between revisions of "LOG"

Jump to navigation
Jump to search

imported>Clippy m |
imported>SMcNeill (style guidelines) |
||

(4 intermediate revisions by one other user not shown) | |||

Line 1: | Line 1: | ||

− | The | + | 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 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. | * The natural logarithm of ''a'' is defined as the integral from 1 to ''a'' of dx/x. | ||

Line 13: | Line 13: | ||

+ | {{PageExamples}} | ||

''Example 1:'' [[FUNCTION]] to find the base ten logarithm of a numerical value. | ''Example 1:'' [[FUNCTION]] to find the base ten logarithm of a numerical value. | ||

{{CodeStart}} | {{CodeStart}} | ||

− | + | FUNCTION Log10#(value AS DOUBLE) {{Cl|STATIC}} | |

− | FUNCTION Log10#(value AS DOUBLE) | ||

Log10# = LOG(value) / LOG(10.#) | Log10# = LOG(value) / LOG(10.#) | ||

− | END FUNCTION | + | END FUNCTION '' '' |

− | |||

{{CodeEnd}} | {{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. | + | :''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. | ''Example 2:'' A binary FUNCTION to convert [[INTEGER]] values using LOG to find the number of digits the return will be. | ||

− | {{CodeStart}} | + | {{CodeStart}} '' '' |

− | + | FUNCTION BIN$ (n&) | |

− | FUNCTION | + | IF n& < 0 THEN EXIT FUNCTION 'positive numbers only! negative error! |

− | FOR p% = 0 TO {{Cl|LOG}}(n& + .1) | + | FOR p% = 0 TO INT({{Cl|LOG}}(n& + .1) / {{Cl|LOG}}(2)) ' added +.1 to get 0 to work |

− | + | IF n& {{Cl|AND}} 2 ^ p% THEN s$ = "1" + s$ ELSE s$ = "0" + s$ 'find bits on | |

− | NEXT p% | + | NEXT p% |

− | IF s$ = "" THEN | + | IF s$ = "" THEN BIN$ = "&B0" ELSE BIN$ = "&B" + s$ 'check for zero return '' '' |

END FUNCTION | END FUNCTION | ||

{{CodeEnd}} | {{CodeEnd}} | ||

− | : ''Explanation:'' The LOG of | + | : ''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) | ||

+ | *[http://qb64.net/wiki/index.php?title=Mathematical_Operations#Derived_Mathematical_Functions Derived Trigonometric Functions] | ||

{{PageNavigation}} | {{PageNavigation}} |

## Latest revision as of 14:40, 9 October 2017

The LOG math function returns the natural logarithm of a specified numerical value.

## Contents

## Syntax

*logarithm!*= LOG(*value*)

## Description

*value*MUST be greater than 0. "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.

## Examples

*Example 1:* FUNCTION to find the base ten logarithm of a numerical value.

FUNCTION Log10#(value AS DOUBLE) STATIC
Log10# = LOG(value) / LOG(10.#)
END FUNCTION * *

*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.

* *
FUNCTION BIN$ (n&)
IF n& < 0 THEN EXIT FUNCTION 'positive numbers only! negative error!
FOR p% = 0 TO INT(LOG(n& + .1) / LOG(2)) ' added +.1 to get 0 to work
IF n& AND 2 ^ p% THEN s$ = "1" + s$ ELSE s$ = "0" + s$ 'find bits on
NEXT p%
IF s$ = "" THEN BIN$ = "&B0" ELSE BIN$ = "&B" + s$ 'check for zero return * *
END FUNCTION

*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".

## See also

- EXP, &B (binary number)
- Derived Trigonometric Functions

*Navigation:*