Login or Signup

Rational Numbers

BlitzMax Forums/Brucey's Modules/Rational Numbers

Brucey1+ years ago #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 :-)

Floyd1+ years ago #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.

Brucey1+ years ago #3
Very true. Thanks.

Armitage 19821+ years ago #4
Interesting !

I wonder in what situation this module would be useful.

Artemis1+ years ago #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.

Brucey1+ years ago #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.

Warpy1+ years ago #7
yes please more maths brucey!

Brucey1+ years ago #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... :-)

Blitz Social Club