Page 1 of 2 12>
Topic Options
#112468 - 2004-01-26 08:23 AM Enhanced Math - power of n
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11624
Loc: CA
Ok is there anyone of you MATH JUNKIES out there that can improve upon this UDF to make it support both DECIMAL and NEGATIVE numbers?


SQR() Raise a number to a power of x
http://www.kixtart.org/ubbthreads/showflat.php?Cat=&Number=82661&page=39&view=collapsed&sb=3&o=all&fpart=1

Top
#112469 - 2004-01-26 02:05 PM Re: Enhanced Math - power on n
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
How about:
Code:
 

Function Pwr($BV,Optional $PV)

DIM $z,$loop

if not $PV
$PV = 2.0
else
$PV = int($PV)
endif

$z = $BV * 1.0
$BV = $BV * 1.0
for $loop = 1 to $PV-1
$z = $z * $BV
EndIf
next
$Pwr = $z
EndFunction



This returned:
Code:
 
Pwr(2, 7) ? 128
Pwr(2, 8) ? 256
Pwr(-2,7) ? -128
Pwr(-2,8) 256
Pwr(1.5,3) 3.375
Pwr(9) 81



I'm not sure if you wanted fractional or negative exponents, but that would require that I look it up..

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#112470 - 2004-01-26 03:01 PM Re: Enhanced Math - power on n
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
This will handle fractional and negative base numbers and negative exponents. It will not handle fractional exponents, but it does handle n^0 :
Code:
Function Pwr($i,Optional $p)

If Not $p $p=2 EndIf

If $p<0
$p=0-$p
$i=1.0/$i
EndIf

$Pwr=1.0

While $p
$Pwr=$Pwr*$i
$p=$p-1
Loop

EndFunction


Top
#112471 - 2004-01-26 06:02 PM Re: Enhanced Math - power of n
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11624
Loc: CA
Thanks gentlemen but I'm also looking for it to support the fractional exponent. I'm looking to implement a form that will require this type of math.

Top
#112472 - 2004-01-26 06:29 PM Re: Enhanced Math - power of n
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
doc, don't forget, the next version of KF has the new floating-point MATH library, if there is anything you want me to add, just say so.
Top
#112473 - 2004-01-26 06:34 PM Re: Enhanced Math - power of n
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11164
Loc: Boston, MA, USA
Now, that I call a "stealth announcement" ! Wow, so, we'll have SIN/COS/TAN/ATN/E/PI/LOG/LN... and that kind of stuff?
_________________________
There are two types of vessels, submarines and targets.

Top
#112474 - 2004-01-26 06:43 PM Re: Enhanced Math - power of n
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11624
Loc: CA
Shawn,

How is that going to work now? Looks like you may have some new good stuff coming, but also harder now to track what code is KiXtart code and what code is KiXforms code.

Well since I'm not a math junkie myself I don't forsee myself needed a LOT of additional math support. For now I'm looking to add some code that requires something like 2^0.xx or 2^x.xx or similar.


Edited by NTDOC (2004-01-26 06:45 PM)

Top
#112475 - 2004-01-26 06:44 PM Re: Enhanced Math - power of n
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Right now there is COS() SIN() and PI, if there is anything else Kix-worthy, just let me know...

The next release of KF is done. Me and Stevie B are "syncing-up" the releases of KiXforms and KiXforms Designer ... just working on some last minute details ...

-Shawn

btw - I just added a new thingy lastnight ... it retrieves the commandline that was used to kix-off the script, the result is like this:

E:\>kix32 test.kix /r /y

[test.kix]

?"cmdLine = " + $System.Environment.CommandLine

[eof]

produces this:

cmdLine = kix32 t /r /y

funny thing is, no we got it - it isn't as "great" as one would suppose. if you try something like this:

E:\> kix32 test.kix /y shawn.txt

kix tries to open shawn.txt as a kixscript lol

-Shawn


Edited by Shawn (2004-01-26 06:50 PM)

Top
#112476 - 2004-01-26 06:46 PM Re: Enhanced Math - power of n
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11624
Loc: CA
Does it support POWER OF X.XX ? Or can it be added to this release?
Top
#112477 - 2004-01-26 06:51 PM Re: Enhanced Math - power of n
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11164
Loc: Boston, MA, USA
Definitely power (with support for fractions and negative numbers), sqare root (all other roots can be calculated via power), logarithm and natural logarithm.
_________________________
There are two types of vessels, submarines and targets.

Top
#112478 - 2004-01-26 06:52 PM Re: Enhanced Math - power of n
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
doc, will add POW( x, y ) where:

x = a number to be raised to a power.
y = a number that specifies a power.

Return Value

The number x raised to the power y.

--- Should be much faster than a UDF.

Top
#112479 - 2004-01-26 07:00 PM Re: Enhanced Math - power of n
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
The math library is dead simple to use, even standalone:

Code:

; Root namespace

$System = CreateObject("Kixtart.System")

; Math object

$Math = $System.Math()

; Stuff ...

?"Cos=" $Math.Cos(0.5)

?"Sin=" $Math.Sin(0.5)



yields:

Quote:


Cos=0.877582561890373
Sin=0.479425538604203





Top
#112480 - 2004-01-27 02:11 AM Re: Enhanced Math - power of n
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
ok, got the following in KF now:

COS()
LOG()
LOG10()
POW()
SIN()
SQRT()

anything else usefull ?

-Shawn

Top
#112481 - 2004-01-27 04:53 AM Re: Enhanced Math - power of n
Jack Lothian Offline
MM club member
*****

Registered: 1999-10-22
Posts: 1169
Loc: Ottawa,Ontario, Canada
You can do fractional exponents by transforming to a log, then multiplying by the exponent & exponentializing it but I don't think a number system exists that combines both fractional components & negative base numbers. The imaginary number system sort of satisfies this demand but I am not sure it can be generalized to include all possible fractional powers. Figuring out what to do with the imaginary numbers returned would also be a challenge.

Some power functions in some languages can sort of do this but what they are really doing is invoking different arithmetic sub-functions depending on whether or not the exponent is integer or not & also depending on whether or not the base is negative. A fractional component is usually handled by either a power expansion approximation using integer powers or by using the log but there is usually a check to return a error code if both fractional exponents & negative base numbers are given to the routine at the same time.

Even without a log function, you can calculate the log using a power expansion like a Taylor expansion. I believe the recommended expansion is given in most books of math functions, tables, intregals, etc. The inverse function is also given. I bet some of the math libraries on the web also have formulae for this.


Edited by Jack Lothian (2004-01-27 05:01 AM)
_________________________
Jack

Top
#112482 - 2004-01-27 07:12 AM Re: Enhanced Math - power of n
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11624
Loc: CA
WOW Jack,

Thanks for the lesson. I mainly need fractional support of the exponent. I don't see using negative base numbers and fractional exponents in the same formula for my usage.

I wouldn't mind having easy functions to implement the OHMS/POWER LAWS but not a "must" have. If I was better at math and conversion it could probably be done with KiX or at least with VBS.

Top
#112483 - 2004-01-27 04:56 PM Re: Enhanced Math - power of n
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11164
Loc: Boston, MA, USA
I also did not mean negative base numbers but fractional and negative exponents with regards to the power function. And support for imaginary numbers can be implemented manually by creating two-element arrays that contain both the real and imaginary part of a number. One can then write a library of basic math functions working on these arrays. Hmm, sounds like fun, I could then write a UDF to perform FFT/IFFT/DFT/IDFT calculations and display these results, maybe combine this with a fucntion generator,...

Jack: Next KiXgolf challenge = write UDF library for basic (+-*/^) math routines for complex numbers ;-)


Edited by sealeopard (2004-01-27 07:35 PM)
_________________________
There are two types of vessels, submarines and targets.

Top
#112484 - 2004-01-27 07:03 PM Re: Enhanced Math - power of n
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11624
Loc: CA
MATH JUNKIE ALERT!

See, I told you there were some on the board.

Top
#112485 - 2004-01-27 07:41 PM Re: Enhanced Math - power of n
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11164
Loc: Boston, MA, USA
Yes, and we're out to get you math-ignoramuses!

Beware of the Math-Illuminati!
_________________________
There are two types of vessels, submarines and targets.

Top
#112486 - 2004-01-27 09:06 PM Re: Enhanced Math - power of n
Chris S. Offline
MM club member
*****

Registered: 2002-03-18
Posts: 2368
Loc: Earth
If I could figure out your IP address you would be in so much trouble right now. ;-)
Top
#112487 - 2004-01-27 10:31 PM Re: Enhanced Math - power of n
Jack Lothian Offline
MM club member
*****

Registered: 1999-10-22
Posts: 1169
Loc: Ottawa,Ontario, Canada
I have no idea if this will work but I sort of combined Shawn's & Richard's snibbets to get this.

Code:
 

Function Pwr($b,$e)
Dim $t
If (vartype($b)<2 or vartype($b)>6)
$Pwr=0.0
Exit 10
EndIf
If (vartype($e)<2 or vartype($e)>6)
$Pwr=0.0
Exit 20
EndIf

$System = CreateObject("Kixtart.System")

; Math object

$Math = $System.Math()

If ($e=0)
$Pwr=1.0
Else
If ($b=0 or $b=1)
$Pwr=$b
Else
If (vartype($e)=2 or vartype($e)=3 or CDbl(int($e))=$e)
$t=int(abs($e))
$Pwr=1.0
While $t
$Pwr=$Pwr*$b
$t=$t-1
Loop
If ($e<0)
$Pwr=1.0/$Pwr
EndIf
Else
If ($b<0)
$Pwr=0.0
Exit 11
EndIf
$t = $Math.log($b)*$e
$Pwr = $Math.exp($t)
EndIf
Endif
EndIf

EndFunction



I am assuming that the function Log() is the natural logarithm function. If it isn't you will need to subsitute the correct function.


Edited by Jack Lothian (2004-01-27 11:01 PM)
_________________________
Jack

Top
Page 1 of 2 12>


Moderator:  Arend_, Allen, Jochen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 239 anonymous users online.
Newest Members
Timothy, Jojo67, MaikSimon, kvn317, kixtarts2025
17874 Registered Users

Generated in 0.101 seconds in which 0.027 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org