It is currently Sat May 25, 2013 9:53 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 11 posts ] 
Author Message
 Post subject: How to do Log2 ?
PostPosted: Tue May 29, 2012 10:47 pm 
Offline
User
User

Joined: Wed May 09, 2012 12:40 am
Posts: 28
I need the function Log2 and came up with the following:

Code:
Define x.d, num.d
x = 8
num = Log(x)/Log(2)


which gives num = 2.99999999999996 which is close but not exact. The exact answer is 3 which is 2^3 = 8 or Log2(8) = 3. Perhaps this difference is due to floating point error.

QUESTION:
Is there a better way to calculate Log2 than the above?

Thank you.


Top
 Profile  
 
 Post subject: Re: How to do Log2 ?
PostPosted: Tue May 29, 2012 11:06 pm 
Offline
Addict
Addict
User avatar

Joined: Wed Dec 23, 2009 10:14 pm
Posts: 1387
Location: Boston, MA
You already have the correct answer for a double.
Code:
Define x.d, num.d
x = 8
num = Log(x)/Log(2)
Debug num
x = 3 * 6.1 / 2 / 6.1 * 2
Debug x
x = 3
Debug x

_________________
To understand recursion, you must first understand recursion. ~ unknown
I never make stupid mistakes. Only very, very clever ones. ~ John Peel


Top
 Profile  
 
 Post subject: Re: How to do Log2 ?
PostPosted: Tue May 29, 2012 11:08 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Fri Jan 21, 2011 8:25 am
Posts: 549
Code:
#Log2 = 0.6931471805599453094

Define x.d
Define num.d

x = 8
num = Log(x) / #Log2

debug num

_________________
Image
ImageImageImage
"You can disagree with me as much as you want, but during this talk, by definition, anybody who disagrees is stupid and ugly."
- Linus Torvalds


Top
 Profile  
 
 Post subject: Re: How to do Log2 ?
PostPosted: Tue May 29, 2012 11:14 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Thu Jan 10, 2008 1:30 pm
Posts: 711
Location: Germany, Glienicke
if you want to use it for quads and use only integers:
Code:
Procedure Log2(Quad.q)
   While Quad <> 0
      Result + 1
      Quad>>1
   Wend
   ProcedureReturn Result-1
EndProcedure

Debug Log2(8)
Debug Log2(1024)

_________________
Image


Top
 Profile  
 
 Post subject: Re: How to do Log2 ?
PostPosted: Wed May 30, 2012 5:10 am 
Offline
Addict
Addict

Joined: Sun Aug 08, 2004 5:21 am
Posts: 1088
Location: Netherlands
Code:
Procedure.d Log2(x.d)
  Protected l.d = x
  !fld1
  !fld qword [p.v_l]
  !fyl2x
  !fstp qword [p.v_l]
  ProcedureReturn l
EndProcedure


Top
 Profile  
 
 Post subject: Re: How to do Log2 ?
PostPosted: Wed May 30, 2012 2:57 pm 
Offline
User
User

Joined: Wed May 09, 2012 12:40 am
Posts: 28
Thank you for your excellent replies.


Top
 Profile  
 
 Post subject: Re: How to do Log2 ?
PostPosted: Wed May 30, 2012 3:52 pm 
Offline
Addict
Addict
User avatar

Joined: Thu Jun 07, 2007 3:25 pm
Posts: 1575
Location: Berlin, Germany
2wilbert:
Very cool! 8)

What is the minimal required prozessor for running this code?

Regards, Little John

_________________
Math problems?
Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x].


Top
 Profile  
 
 Post subject: Re: How to do Log2 ?
PostPosted: Wed May 30, 2012 4:22 pm 
Offline
Addict
Addict

Joined: Sun Aug 08, 2004 5:21 am
Posts: 1088
Location: Netherlands
Little John, an old 486 processor should be sufficient.


Top
 Profile  
 
 Post subject: Re: How to do Log2 ?
PostPosted: Wed May 30, 2012 4:26 pm 
Offline
Addict
Addict
User avatar

Joined: Thu Jun 07, 2007 3:25 pm
Posts: 1575
Location: Berlin, Germany
Great, thank you!

_________________
Math problems?
Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x].


Top
 Profile  
 
 Post subject: Re: How to do Log2 ?
PostPosted: Thu May 31, 2012 12:32 am 
Offline
Addict
Addict

Joined: Fri Apr 25, 2003 11:10 pm
Posts: 852
a little shorter :twisted:
Code:
Procedure.d Log2(x.d)
  !fld1
  !fld qword [p.v_x]
  !fyl2x
  ProcedureReturn
EndProcedure

return value can be left in st(0) :)


Top
 Profile  
 
 Post subject: Re: How to do Log2 ?
PostPosted: Thu May 31, 2012 6:34 am 
Offline
Addict
Addict

Joined: Sun Aug 08, 2004 5:21 am
Posts: 1088
Location: Netherlands
jack wrote:
a little shorter

I know it's shorter.
The main reason why I did it a bit different is that I wasn't sure your solution would work on x64 since that officially expects a floating point value to be returned in xmm0.
I figured if I would PureBasic let handle the way it returns the value that that might be a more stable option. But maybe I'm wrong 8)

Another option, if you only need the integer part of the result and the source is a double is to do it like this
Code:
x.d = 64
Debug PeekW(@x + 6) >> 4 - 1023


If both source and result are integer, you can also use bsr
Code:
Procedure.i Log2(x.i)
  CompilerIf #PB_Compiler_Processor = #PB_Processor_x86
    !mov eax, -1
    !bsr edx, [p.v_x]
    !cmovnz eax, edx
  CompilerElse
    !mov rax, -1
    !bsr rdx, [p.v_x]
    !cmovnz rax, rdx
  CompilerEndIf
  ProcedureReturn
EndProcedure


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 11 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye