Rational Numbers

BlitzMax Forums/Brucey's Modules/Rational Numbers

Brucey(Posted 2009) [#1]
Announcing yet another addition to the modules related to mathematics : BaH.Rational

Rational numbers are any number that can be expressed as the ratio or quotient a/b, of two integers. Fractions!

This module is built on the Boost library, so you will also need BaH.Boost. (Other modules already using BaH.Boost, include BaH.Mathtoolkit and BaH.DateTime),

Here's a small example of use :

1/3 * 1/6 = 1/18

Local third:TRational = Rational(1, 3) ' 1/3
Local sixth:TRational = Rational(1, 6) ' 1/6

Local eighteenth:TRational = third.MultiplyR(sixth) ' multiply by rational


The download is available from the google code site, here.


Have fun :-)


Floyd(Posted 2009) [#2]
can be expressed as the quotient a/b of two integers

I would call it the ratio of two integers, just to drive home the point.


Brucey(Posted 2009) [#3]
Very true. Thanks.


Armitage 1982(Posted 2009) [#4]
Interesting !

I wonder in what situation this module would be useful.


Artemis(Posted 2009) [#5]
Didn't that already exists as one of your modules? Or do I have a deja-vu? Or did I write such an module a long time ago? Strange ;-)

Usage Example: Writing an scientific calculator.

This is just another situation which screams for operator overloading. ;-)

Wouldn't it be cool to just say:
Local a:TRational, b:TRational, c:TRational
a = Rational(1, 3)
b = Rational(2, 5)
c = a * b

But this is just a wish… unlikely that Mark does something like that.


Brucey(Posted 2009) [#6]
Wouldn't it be cool to just say:

Yes, it would... which is how it works in C++, and as such in the wrapper :
boost::rational<int> * bmx_rational_multiplyr(boost::rational<int> * rat, boost::rational<int> * value) {
	boost::rational<int> * r = new boost::rational<int>();
	*r = *rat * *value;
	return r;
}

Unfortunately, we have to live with what BlitzMax allows us to do...


Didn't that already exists as one of your modules?

It's been in SVN for a while, but unreleased.


Warpy(Posted 2009) [#7]
yes please more maths brucey!


Brucey(Posted 2009) [#8]
yes please more maths brucey!

I imagine this stuff would be up your alley... which is implemented in BaH.MathToolkit.

I'm always on the lookout for other interesting (non-GPL) libraries... :-)