Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
logarithm to the base of 2 (binary logarithm), base 10 and base n
#1
As I see, QB64 only knows the logarithm function LOG() to the base e.
The following small program calculates the binary logarithm ( logarithm to the base of 2) and the logarithms to the base 10 and n

Code: (Select All)

DIM AS _FLOAT a, n
a = 22
PRINT log2##(a)
PRINT logn##(22, 2)
PRINT logn##(22, 10)
PRINT log10##(22)

'Function log2: logarithm to the base of 2 (binary logarithm)
FUNCTION log2## (a)
    log2## = (LOG(a) / LOG(2))
END FUNCTION

'Function log10: logarithm of a to the base of n
FUNCTION log10## (a)
    log10## = (log2##(a) / log2##(10))
END FUNCTION

'Function logn: logarithm of a to the base of n
FUNCTION logn## (a, n)
    logn## = (log2##(a) / log2(n))
END FUNCTION

LOG10 is often called LG and
LOG is often LN (for natural logarithm).

Could these functions be implemented as standard QB64PE functions: _LOG2(), _LOG10(), LOGn(,)?
Reply
#2
(10-02-2023, 12:18 PM)BSpinoza Wrote: As I see, QB64 only knows the logarithm function LOG() to the base e.
The following small program calculates the binary logarithm ( logarithm to the base of 2) and the logarithms to the base 10 and n

Code: (Select All)

DIM AS _FLOAT a, n
a = 22
PRINT log2##(a)
PRINT logn##(22, 2)
PRINT logn##(22, 10)
PRINT log10##(22)

'Function log2: logarithm to the base of 2 (binary logarithm)
FUNCTION log2## (a)
    log2## = (LOG(a) / LOG(2))
END FUNCTION

'Function log10: logarithm of a to the base of n
FUNCTION log10## (a)
    log10## = (log2##(a) / log2##(10))
END FUNCTION

'Function logn: logarithm of a to the base of n
FUNCTION logn## (a, n)
    logn## = (log2##(a) / log2(n))
END FUNCTION

LOG10 is often called LG and
LOG is often LN (for natural logarithm).

Could these functions be implemented as standard QB64PE functions: _LOG2(), _LOG10(), LOGn(,)?

Wouldn't it be more efficient to do all other log bases like you did Log2 as it is one less call to User Defined Function. ie 0 calls to user defined function
BaseNLog##(BaseN, a) = Log(a)/Log(BaseN) ' which includes Log2(a)'s which I think is in wiki already
b = b + ...
Reply
#3
This is actually beyond my realm. I also don't understand why the designers of BASIC used "LOG" for natural logarithm. Obviously they weren't American. -_O_-

I saw source code which `LOG()` is used only once: to be wrapped into an user function called `LN()`. Then that `LN()` was called to provide common log, called `L10()`.
Reply
#4
bplus wrote: 
Quote:Wouldn't it be more efficient to do all other log bases like you did Log2 as it is one less call to User Defined Function. ie 0 calls to user defined function
BaseNLog##(BaseN, a) = Log(a)/Log(BaseN) ' which includes Log2(a)'s which I think is in wiki already
I know this and thats right. In some conditions it would make some math formulas less complicate, if you have some special LOG functions, especially the LOG2 and LOG10.
This also know the creators of other programming languages, i.e. Python.
Reply




Users browsing this thread: 3 Guest(s)