DAY 008: LOG - Printable Version +- QB64 Phoenix Edition (https://staging.qb64phoenix.com) +-- Forum: Official Links (https://staging.qb64phoenix.com/forumdisplay.php?fid=16) +--- Forum: Learning Resources and Archives (https://staging.qb64phoenix.com/forumdisplay.php?fid=13) +---- Forum: Keyword of the Day! (https://staging.qb64phoenix.com/forumdisplay.php?fid=49) +---- Thread: DAY 008: LOG (/showthread.php?tid=1114) Pages:
1
2
|
DAY 008: LOG - SMcNeill - 11-13-2022 Our first showcase of a nice math keyword! As my math skills are all rusty and out of practice, I'd like to ask one of our resident math gurus if they'd be so nice as to offer us up a nice description of what LOG is, how to use it, and maybe showcase us a few simple examples of it in use. If nobody else wants to help fill in this topic, I'll come back to it later this evening and try my hands at a poor LOG example, if necessary, but I definitely feel as if we have some folks around here who are much better with this type of topic than I am. RE: DAY 008: LOG - bplus - 11-13-2022 I don't use it much either but I did use it recently as is an alternate way to do powers. This Log is the natural Log e AKA Ln, Not Log base 10. I used this idea for String Math to do powers: Code: (Select All) ' alternate to power 5 ^ 5 PS I think I recall Steve using it to get the number of digits in a number? Update: that's in Wiki for a long pos integer. RE: DAY 008: LOG - mnrvovrfc - 11-13-2022 "LOG()" distinctly proved my ignorance while learning programming languages other than BASIC. To my surprise, it should have been called "LN()" because "LOG()" in BASIC only does natural log, not common log. It makes perfect sense to other people that a function called "LOG()" should return base-10 log. It would have been interesting to know who was the one responsible for this function-naming mess. I don't think this function began at Dartmouth. I know even less than bplus about this! I wasn't even patient enough to come up with that essence of the exponential curve in 512 points that I featured in one of the programs posted in this forum. It took me at least one hour to get it right in Lua. RE: DAY 008: LOG - SMcNeill - 11-13-2022 (11-13-2022, 03:24 PM)bplus Wrote: I don't use it much either but I did use it recently as is an alternate way to do powers. It's an easy way to get the number of binary digits in a number. Code: (Select All) Print "Number of BINARY digits required to represent a value:" RE: DAY 008: LOG - Pete - 11-13-2022 I love this keyword, and use it daily. Captain's LOG, Stardate... But in realites, Iz knowz nothin' about LOG. We need @Jack to the rescue for this one! On a side note, consider making a new QB64PE keyword, specific for ONLINE USE: _HOLLOW LOG It doesn't return any math answers, but if talk real nice to it, it might output COOKIES! Kiebler Pete RE: DAY 008: LOG - TerryRitchie - 11-13-2022 (11-13-2022, 05:55 PM)Pete Wrote: I love this keyword, and use it daily. Captain's LOG, Stardate... I wonder how many people understand that reference, LOL? I have not watched TV in over 30 years. Do they still use the Keebler Elves in commercials? RE: DAY 008: LOG - Pete - 11-13-2022 Oh come on Terry, What an elf does in the privacy of his hollow tree ought to be his own business! Pete RE: DAY 008: LOG - bplus - 11-13-2022 Pete's been spending too much time on the holo deck. RE: DAY 008: LOG - Pete - 11-13-2022 Beats the prices on Risa. Pete RE: DAY 008: LOG - luke - 11-19-2022 Here's a brief write-up for you: You are likely familiar with the exponentiation (power) operator ^, a kind of repeated multiplication. In general, we have p = b ^ x. Here we'll call x the "exponent" and b the "base". The logarithm is the inverse of exponentiation, similar to how division is the inverse of multiplication. Instead of calculating p given b and x, it calculates x given p and b. In other words, "what power do I need to raise the value b to so that I get p?" For instance, 16 = 2 ^ 4. What is the logarithm of 16 with the base of 2? 4. "But Luke," I hear you cry, "the LOG function only takes one parameter, how do I pass it the 16 and the 2?" It turns out that over the years the maths and engineering folks have found themselves using the same values for the base parameter over and over. Probably the most common choices for the base when calculating a logarithm are 2, 10 and e (a special number equal to about 2.718 but going on forever like pi). The LOG function in BASIC calculates what's called the "natural logarithm", which uses e as the base. So when you write LOG(9), you're asking the computer "what value of x do I need so that e ^ x = 9?" or approximately, so that 2.71828 ^ x = 9. It turns out LOG(9) is about 2.197, and if you check you'll see 2.71828 ^ 2.197 is pretty darn close to 9. Why didn't the designers of BASIC let you use any value for the base? One reason is that it's quite easy to calculate any kind of logarithm with a simple function: Function logarithm(base, value) logarithm = Log(value) / Log(base) End Function You just divide by the LOG of the base you want to use and you get your answer. Armed with this, we can take a look at the other common bases, 2 and 10. When we write down numbers, we use a place value system where each place makes a digit worth 10 times more. This means the number 173 is the sum of powers of 10: 173 = 1 * 10^2 + 7 * 10^1 + 3 * 10^0. Logarithms let us go the other way and work out how many digits (or places) we need to get a certain value. Because we're using decimal, this is a base 10 logarithm: Print Int(logarithm(10, 173)) + 1 '3 Print Int(logarithm(10, 2914)) + 1 '4 We need Int here because this is a math function that gradually increases to the next value, and +1 because it's actually giving you the exponent of the highest power of 10 in the "173 = 1 * 10^2 + 7 * 10^1 + 3 * 10^0" representation. This trick works for binary too: doing it with base 2 will tell you how many bits a number has. A final note: the base of the natural logarithm e (2.718...) can be had with the EXP function. EXP(x) calculates e ^ x, so EXP(1) is e itself. |