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] another natural logarithm function

Author
Message
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 21st May 2007 12:51
The following code demonstrates another way of calculating natural logarithms and gives you some idea of the speed on your machine.



Let me know if you experience problems with certain number ranges. It should run at much the same speed regardless of the value whose logarithm is required.
jason p sage
17
Years of Service
User Offline
Joined: 10th Jun 2007
Location: Ellington, CT USA
Posted: 14th Jun 2007 01:16
Well, I was unawares of binary manipulation capabilities and the DWORD data type before looking at your code.

I admit I didn't know what Natural Logarithm really was before this post either! It seems quite unnatural. However after a gaunt on WikiPedia - it seems its useful for fractals in my opinion kinda - at LEAST useful for drawing things like blackholes and water - this is all based on what I saw at wikipedia.

I'm no math expert - but its funny - as a professional programmer I seem to get by. I wish I knew calculus - opens many doors in the 3d realm. So for now I'll just look up formulas and devise my own when I need them like I always have.

Now that I understand What ln(x) is (Thank for sparking me on my research) I can see why it can take awhile and why writing a FAST function to do it is important.

Thank You for all the above. This is a good thing for the tool box - graphically speaking that is. That is the only use I can see where this is useful - but I'm SURE this is ignorance on my part.

Know way to many languages - Master of none
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 14th Jun 2007 13:09
Quote: "Well, I was unawares of binary manipulation capabilities and the DWORD data type before looking at your code."


They can be useful indeed.

Quote: "This is a good thing for the tool box - graphically speaking that is. That is the only use I can see where this is useful "


Strangely enough, that's why I needed it too.

Thanks for the feedback.
GatorHex
19
Years of Service
User Offline
Joined: 5th Apr 2005
Location: Gunchester, UK
Posted: 15th Sep 2007 00:58 Edited at: 15th Sep 2007 01:01
Thanks for this function Green Gandalf I found it useful for converting Experience into Levels

levelAdjust# = LN(2.0)
exp# = 256.0
level# = LN(exp#)/levelAdjust#

print level#
wait key

DinoHunter (still no nVidia compo voucher!), CPU/GPU Benchmark, DarkFish Encryption DLL, War MMOG (WIP), 3D Model Viewer
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 15th Sep 2007 18:25
@gg,
A few releases back I added two new functions for converting between dwords & floats for just this kind of manipulation, so you no longer need to use a memblock to do it:

Dword Value = CAST FLOAT TO DWORD ( Float Value )
Float Value = CAST DWORD TO FLOAT ( Dword Value )

I've used it for generating INF, negative INF, NAN, negative NAN and negative zero float values. Before these functions I was also using a 4 byte memblock for the conversion.

Utility plugins collection and
http://www.matrix1.demon.co.uk for older plug-ins and example code
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 16th Sep 2007 21:16
@IanM

Thanks for the info. I've now downloaded your latest plug-in and got the new commands which will be useful for just this sort of thing. However, I won't change this yet because not everyone will have your plug-ins.

Any chance of adding a version of the natural log function to your plug-ins? Or is there one already? (Too lazy to look ... )

If not, I'll write another version of this using your plug-in commands.

One day I'll get to grips with C++ and do this sort of plug-in stuff myself. (Yet another project for the ever-growing list of interesting/useful things to do ... )

@GatorHex

Thanks for the feedback.
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 17th Sep 2007 16:03
Quote: "Any chance of adding a version of the natural log function to your plug-ins? Or is there one already?"


Sure, I can add them - there's one function in plug-in 17 for bit manipulation that tells you the position of the highest bit, which is effectively an integer version of log2, but no others yet.

Utility plugins collection and
http://www.matrix1.demon.co.uk for older plug-ins and example code
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 17th Sep 2007 17:18
Quote: "which is effectively an integer version of log2"


That's probably what GatorHex needed for his application.

Quote: "Sure, I can add them"


That would be good - how's it likely to compare speedwise?
Jeff Miller
19
Years of Service
User Offline
Joined: 22nd Mar 2005
Location: New Jersey, USA
Posted: 18th Sep 2007 03:49
Yes, that is exactly what Gatorhex needed; he was looking for interger powers. I sent here because he specifically needed to determine the power to which 2 was raised to get to, e.g., 128. I told him LN(e.g. 128)/LN(2), but sent him here due to the lack of a DBP LN function.
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 19th Sep 2007 01:14
It's the really well named 'BIT FIND LAST SET' function:



Finding the power-of-2 with round-down (like the above function) is simply a matter of finding the highest bit set.

I've also got some bit manipulation functions in native DBPro that do the same, and others that round up or down to nearest power-of-2 if they are needed. In fact, they'd make a useful code snippet on their own.

Utility plugins collection and
http://www.matrix1.demon.co.uk for older plug-ins and example code

Login to post a reply

Server time is: 2024-11-22 18:50:51
Your offset time is: 2024-11-22 18:50:51