Code: Select all
Define x.d, num.d
x = 8
num = Log(x)/Log(2)
QUESTION:
Is there a better way to calculate Log2 than the above?
Thank you.
Code: Select all
Define x.d, num.d
x = 8
num = Log(x)/Log(2)
Code: Select all
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
Code: Select all
#Log2 = 0.6931471805599453094
Define x.d
Define num.d
x = 8
num = Log(x) / #Log2
debug num
Code: Select all
Procedure Log2(Quad.q)
While Quad <> 0
Result + 1
Quad>>1
Wend
ProcedureReturn Result-1
EndProcedure
Debug Log2(8)
Debug Log2(1024)
Code: Select all
Procedure.d Log2(x.d)
Protected l.d = x
!fld1
!fld qword [p.v_l]
!fyl2x
!fstp qword [p.v_l]
ProcedureReturn l
EndProcedure
Code: Select all
Procedure.d Log2(x.d)
!fld1
!fld qword [p.v_x]
!fyl2x
ProcedureReturn
EndProcedure
I know it's shorter.jack wrote:a little shorter
Code: Select all
x.d = 64
Debug PeekW(@x + 6) >> 4 - 1023
Code: Select all
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