# PureBasic Forum

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

 All times are UTC + 1 hour

 Page 1 of 1 [ 11 posts ]
 Print view Previous topic | Next topic
Author Message
 Post subject: How to do Log2 ?Posted: Tue May 29, 2012 10:47 pm
 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

 Post subject: Re: How to do Log2 ?Posted: Tue May 29, 2012 11:06 pm

Joined: Wed Dec 23, 2009 10:14 pm
Posts: 1387
Location: Boston, MA
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

 Post subject: Re: How to do Log2 ?Posted: Tue May 29, 2012 11:08 pm
 Enthusiast

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

_________________

"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

 Post subject: Re: How to do Log2 ?Posted: Tue May 29, 2012 11:14 pm
 Enthusiast

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:
Result + 1
Wend
ProcedureReturn Result-1
EndProcedure

Debug Log2(8)
Debug Log2(1024)

_________________

Top

 Post subject: Re: How to do Log2 ?Posted: Wed May 30, 2012 5:10 am

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

 Post subject: Re: How to do Log2 ?Posted: Wed May 30, 2012 2:57 pm
 User

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

Top

 Post subject: Re: How to do Log2 ?Posted: Wed May 30, 2012 3:52 pm

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

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

 Post subject: Re: How to do Log2 ?Posted: Wed May 30, 2012 4:22 pm

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

Top

 Post subject: Re: How to do Log2 ?Posted: Wed May 30, 2012 4:26 pm

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

 Post subject: Re: How to do Log2 ?Posted: Thu May 31, 2012 12:32 am

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

return value can be left in st(0)

Top

 Post subject: Re: How to do Log2 ?Posted: Thu May 31, 2012 6:34 am

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

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

 Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending
 Page 1 of 1 [ 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 forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forum

Search for:
 Jump to:  Select a forum ------------------ PureBasic    Coding Questions    Game Programming    3D Programming    Assembly Programming    The PureBasic Editor    The PureBasic Form Designer    General Discussion    Feature Requests and Wishlists    Tricks 'n' Tips Bug Reports    Bugs - Windows    Bugs - Linux    Bugs - Mac OSX    Bugs - Documentation OS Specific    AmigaOS    Linux    Windows    Mac OSX Miscellaneous    Announcement    Off Topic Showcase    Applications - Feedback and Discussion    PureFORM & JaPBe    TailBite