Maths lib or dll for Blitz3D

Blitz3D Forums/Blitz3D Programming/Maths lib or dll for Blitz3D

Prym(Posted 2013) [#1]
Does a library for math ( 3root, Nroot, and other ) in Blitz3D exist ?


virtlands(Posted 2013) [#2]
That's a great Q.

I don't remember such a library, yet there are scattered math topics throughout this forum.
--------------------------------------------------------------------------
You probably already know this code here; This is an elementary way to do roots:

Function ThirdRoot#(v#)
Return Exp( Log(v)/3 )
End Function

Function NthRoot#(v#, n#)
Return Exp( Log(v)/n )
End Function
-------------------------------------
If you wish to have DOUBLE Precision math, then there is a library for that:

Doubles for Blitz3D by RGR
http://www.blitzbasic.com/codearcs/codearcs.php?code=1971

Blitz Doublw by Abram
http://www.blitzbasic.co.nz/codearcs/codearcs.php?code=1905#comments

Blitz Double by Abram
http://www.blitzbasic.com/codearcs/codearcs.php?code=1905
-------------------------------------
other math related posts:

Blitz3D C++ Geometry Code by Marksibly
http://www.blitzbasic.com/Community/posts.php?topic=42657

Vector Math Library by Beeps
http://www.blitzbasic.com/codearcs/codearcs.php?code=936
-------------------------------------
I know that PureBasic is an excellent language for creating DLLs.
http://www.purebasic.com/

Tiny C is free, and can also create DLLs:
http://bellard.org/tcc/
Tiny C is sort of command-line based, so you'll have to get used to that.
Tiny C documentation on creating DLLs: http://bellard.org/tcc/tcc-doc.html

DLLs seem to be hard to find. So, you can either figure out how to make your own Math DLL,
or you can transfer math code from various places into Blitz3D.
-------------------------------------
some sample codes: ;)

calculating cube root by John T.H.
http://forums.purebasic.com/english/viewtopic.php?p=210283

PureBasic UNclassified Nth root
http://megasnippets.com/source-codes/purebasic/nth_root

Huge Integer Math and Encryption, [includes various math routines]
http://www.bestsoftware4download.com/software/k-purebasic-t-free-hime-huge-integer-math-and-encryption-download-wgevwgkq.html

Lots of 3D Graphic Maths
http://www.blitzbasic.com/codearcs/codearcs.php?cat=13

a whole horde of stuff by Teasy
http://www.bettiesart.com/tc/blitz/lib.html


Floyd(Posted 2013) [#3]
We already have arbitrary powers, x^y.

x^(1.0/3.0) is 1/3 root.


Yasha(Posted 2013) [#4]
Tiny C is free, and can also create DLLs:


Note that while Tiny C is a jaw-droppingly brilliant piece of compiler design, it doesn't bother reporting errors in any level of detail (or... at all). Unless you're an expert C coder and can spot errors yourself, you might find it very difficult to get much done. Pelles C is an industrial-strength compiler that's nearly as small, fast and easy to use (so good for newcomers) and comes with a Blitz-style graphical editor.

(I have nothing helpful to add on-topic, sorry.)

Last edited 2013


virtlands(Posted 2013) [#5]
Thanks for sharing the Pelles C.
I have now added it to my collection.


Kryzon(Posted 2013) [#6]
Wow, nice low-down at post #2. Thanks for sharing.


virtlands(Posted 2013) [#7]
Yeah, Floyd's idea of
ThirdRoot(x) = x^(1.0/3.0)

if much faster than my idea of
ThirdRoot(x) = Exp( Log(x)/3 )

---just out of curiousity I placed both in an extensive timing test,
---and Floyd' code is about 16.5 times faster than my long version.

On my machine:

x^(1.0/3.0) takes about 0.0625 millisecond
Exp( Log(v)/3 ) takes about 1.03 millisecond


Last edited 2013


Kryzon(Posted 2013) [#8]
Hi. I was complimenting your post VirtLands, the amount of resources you posted.

Anyway, the "^" operator is supposed to be faster than a function: being native to Blitz3D, it probably represents the Pow(x,y) from math.h in C++, so it's expected to have improved performance over vanilla Blitz3D code.

However, if you're going for integer powers, it's been noted that using multiplications is much faster than that power operator:

Using the native operator
Cube(x) = x^3

is slower than using multiplications
Cube(x) = x*x*x

Last edited 2013


Yasha(Posted 2013) [#9]
Note also that unlike the other operators, there's no integer version of ^ (makes sense - math.h doesn't provide one either): it will always give a floating point result no matter what you pass into it. This means that you have to watch for the single-precision limit, which I guess you might reach quite easily with this operator.

In the unlikely event that this is an actual problem (not just obscure trivia), some alternative solutions can be found here: http://stackoverflow.com/questions/101439/the-most-efficient-way-to-implement-an-integer-based-power-function-powint-int


Axel Wheeler(Posted 2013) [#10]
Depending on your application, if you are doing a lot of these per frame and if they are all based on integers (or if integers would be close enough) you could put them in an array at startup. Looking them up is faster than either form of calculation above.

Local limit=1000000
Dim ThirdRoot(limit)
Local x
Local startTime,endTime
Local v#

startTime=MilliSecs()
For x=0 To limit-1
	ThirdRoot(x) = x^(1.0/3.0)
Next
endTime=MilliSecs()

Print "Calculation time was "+(endTime-startTime)


startTime=MilliSecs()
For x=0 To limit-1
	v#= ThirdRoot(x)
Next
endTime=MilliSecs()

Print "Array lookup time was "+(endTime-startTime)

WaitKey()



Thus you just run this kind of thing once at startup.

Of course it doesn't do negative numbers or floats, however for floats I suppose you could still do it, you'd just have to calculate the root, then store in the array index that is, say 100 times the original lookup value. You'd be sacrificing precision for speed, but it should work? Maybe?


virtlands(Posted 2013) [#11]
; Thanks Yasha.

; Your link led to another link, which quite possibly has the
; fastest integer power function in the universe.
;
; It's power_v4(a,b) from Santiago's Blog:
; So, here's that code which I translated from 'C' to Blitz3D.

It's excellent, and complex, ..

Of course, 'doubles' are not natural to Blitz3d, so I had to demote it to 'long'.

As for Axel Wheeler's idea of creating tables, that kind of has possibilities, and I was wondering if it could be accurate to interpolate
third-root-tables created from integer domains? I'm guessing that it would not lead to accurate results, because the world of floats is so vast.

[Wikipedia subject: Interpolation_ http://en.wikipedia.org/wiki/Interpolation ]

edited too many times 1970

Last edited 2013