Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Code Snippets / [DBP] ln(x)

Author
Message
Neuro Fuzzy
16
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 25th Apr 2010 05:06 Edited at: 25th Apr 2010 05:07


ln(x). The math is pretty much just what I've read off of wikipedia. I am not this math-savvy, and I think solving this involves a good understanding of calculus, the "arithmetic-geometric mean", pi's relation to e (i is probably involved somewhere in there) and Newtons Method. I know none of these. Don't ask me how it works. I'll definitely be going back and trying to re-derive this, but for now... meh. Here's an example of it:


in the line "dot x#*pixels_per_unit+screen width()/2,-ln(x#)*pixels_per_unit+screen height()/2" , the negative sign is by ln(x#) because pixel 0,0 is at the top left and, w,h is at the bottom right, but we want negatives at the bottom and positives at the top.

[edit]

I've already found a plug-in somewhere with this function, I just wanted to do it myself cuz i was bored.


baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 28th Apr 2010 15:19
I hate to show my ignorance (as many times as I do) but what exactly does this function do?

Neuro Fuzzy
16
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 28th Apr 2010 17:01
natural logarithms


baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 28th Apr 2010 17:16
OK, so I spent a while looking at this article (and other links):
http://en.wikipedia.org/wiki/Natural_logarithm

...and got a bit lost in the maths.

What would you actually use the function for?

I'm not trying to be a pain, just to understand.

Neuro Fuzzy
16
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 29th Apr 2010 01:24 Edited at: 29th Apr 2010 01:25
The most famed example would be computing continuously compounded interest.
Say you have a bank account with $10,000 in it, and you want to wait until it has $60,000, in it.

If it grew at 6.5% per year, compounded monthly, the equation would be:
money=$10000*(1+.065/12)^(12*T) where T is the time after being deposited in years. Now... what do you do if you want to find T?
6=1.066971852^T
using logarithms, we can find that T=ln(6)/ln(1.066971852)=27.64 years.

If it grew at 6.5% per year compounded continuously (every single moment), the equation would be 6=e^(.065*T), and so T=ln(6)/.065

Besides stuff like that, it would be useful if you wanted to create a logarithmic graph of something, like this:


the natural logarithm is more fundamental, but you can figure out log base n with the formula:
log_n(x)=ln(x)/ln(n)


baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 29th Apr 2010 10:51
Thanks @Neuro Fuzzy, you have both made me laugh and taught me something!!

It's been a long time since I did logarithms at school!!

Cheers

Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 6th May 2010 20:26
Or you could use this:

natural logarithm function

or even one of IanM's Matrix1 utilities.
Dr Tank
14
Years of Service
User Offline
Joined: 1st Apr 2009
Location: Southampton, UK
Posted: 20th May 2010 23:41
That's cool.

ln(x) tends to crop up a lot and is really useful. Last time I used it in DBP (using a plugin i found on here ages ago) was for setting sound volumes, since the sound volume you set is logarithmic.

I expect to bump into it again before too long. The various approximations are interesting, and some experimentation will be required to find the best tradeoff for a given task between accuracy and speed.

The wikipedia article looks informative. My usual response when faced with making faster versions of functions like this, or implementing functions not available in DBP, is to use a simple series expansion. However in the case of ln(x), it appears there are better ways.
Neuro Fuzzy
16
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 21st May 2010 04:14
Yeah. I really want to prove the stuff on the wiki article one day.


Login to post a reply

Server time is: 2024-03-29 07:35:12
Your offset time is: 2024-03-29 07:35:12