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.

Geek Culture / Math help please

Author
Message
Hawkblood
14
Years of Service
User Offline
Joined: 5th Dec 2009
Location:
Posted: 14th Apr 2014 22:54
This has to be the stupidest thing. I can't for the life of me figure out how to do a base 2 log, so I created a process to get the answer. I would like to know if there is a math function already out there......

this process gives the integer value of the input number.

The fastest code is the code never written.
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 14th Apr 2014 23:37
Try this:

Quote: "Matrix1 Utility Plug-ins


LOG2

Syntax

Value = LOG2 ( Value )

Description

This function will return the base-2 logarithm of the number provided.
"


Matrix1 is the answer to all my ills.



Powered by Free Banners
Seditious
10
Years of Service
User Offline
Joined: 2nd Aug 2013
Location: France
Posted: 14th Apr 2014 23:44
It's already included in the standard maths header (math.h or cmath): http://www.cprogramming.com/fod/log2.html
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 15th Apr 2014 00:06
Didn't notice you'd written C++ or a variant. Oops.

I was talking about DBPro.

I'm sure all real computing languages have logs to base 2 as a standard function.



Powered by Free Banners
Hawkblood
14
Years of Service
User Offline
Joined: 5th Dec 2009
Location:
Posted: 15th Apr 2014 00:10
I think it's been taken out of math.h and cmath, because when I #include them, I still can't get it. I made functions to cover it though:


The fastest code is the code never written.
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 15th Apr 2014 01:44
None of those will give the correct values for arbitrary arguments.

For example, log2(3.0) should be about 1.58496.



Powered by Free Banners
Hawkblood
14
Years of Service
User Offline
Joined: 5th Dec 2009
Location:
Posted: 15th Apr 2014 03:23
You are correct. The purpose of the functions was to give a number for mip generation purposes, not to be mathematically correct. For the functions to work, I would have to:

to be more accurate.....

The fastest code is the code never written.
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 15th Apr 2014 12:10
That should do it.



Powered by Free Banners
Dar13
15
Years of Service
User Offline
Joined: 12th May 2008
Location: Microsoft VisualStudio 2010 Professional
Posted: 15th Apr 2014 13:58


Hawkblood
14
Years of Service
User Offline
Joined: 5th Dec 2009
Location:
Posted: 15th Apr 2014 23:29
Mine eliminates the extra calculation of log(2) for faster results.

The fastest code is the code never written.
Neuro Fuzzy
16
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 25th Apr 2014 06:37
If you want an integer log2 (floored) this is the same as, for a positive integer, finding the position of the leftmost (largest figure) one bit!

There are a few bitwise formulas for this in the book Hacker's Delight.


"I <3 u 2 bbz" - Dark Frager
Dar13
15
Years of Service
User Offline
Joined: 12th May 2008
Location: Microsoft VisualStudio 2010 Professional
Posted: 25th Apr 2014 07:50
Quote: " Mine eliminates the extra calculation of log(2) for faster results."

I would hope that the standard library would use a look-up table for simple values of log().

@Neuro Fuzzy
Cool!

Hawkblood
14
Years of Service
User Offline
Joined: 5th Dec 2009
Location:
Posted: 27th Apr 2014 02:38
Quote: "I would hope that the standard library would use a look-up table for simple values of log()."

It's still a call to a function (maybe in-line, but still a call). I'm sure that multiplier isn't giving me a huge advantage, but I try to optimize as much as possible.

The fastest code is the code never written.
Seditious
10
Years of Service
User Offline
Joined: 2nd Aug 2013
Location: France
Posted: 27th Apr 2014 06:10
Quote: "It's still a call to a function (maybe in-line, but still a call). "


If it's inline (as it will be with full optimization) it isn't a call.

Login to post a reply

Server time is: 2024-04-27 14:33:31
Your offset time is: 2024-04-27 14:33:31