NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

Journal of HollyKing (5210)

Monday February 20, 2006
10:21 PM

Interval Computation Module Design

[ #28751 ]

Greetings!

I have a need to perform some Interval Computations for one of my projects. Since I was unable to find a module on CPAN I'm going to have to write my own. I would like to upload my module to CPAN so the next lucky stiff doesn't have to repeat my effort. Having never written a module for CPAN before I wanted to get some feedback about my plans before diving too deep. I know there are many answers so I'm not looking for the one true way, but some advice from those who've gone before me.

In this context an interval is a new type of number like a complex number. They can be added, subtracted, multiplied and divided. We can define exponentation, trigonometric functions and a whole host of other functions.

For my application an interval represents a value with a known amount of error. The interval `[4, 6]` means that the real answer is somewhere between 4 and 6, but we don't know exactly where. Because floating point calculations aren't exact, the results of interval calculations are rounded out by rounding the lower bound down and the upper bound up to the next representable numbers. The end result gives you a lower and upper bound that contain the real answer.

Here is an example of some basic operations on the intervals `x = [1, 4]` and `y = [9, 12]`:

`x + y = [1 + 9, 4 + 12] = [10, 16]`

`x - y = [1 - 12, 9 - 4] = [-11, 5]`

`x * y = [ min(1 * 4, 1 * 12, 4 * 9, 4 * 12), max(1 * 9, 1 * 12, 4 * 9, 4 * 12)] = [9, 48]`

`x / y = { magic Perl code involving reciprocals and checking for division by zero } = [ 1/12, 4/9 ]`

First, I was thinking of calling my module `Math::Interval`. That name captures the purpose and isn't unweildy. Another option was `Number::Interval` since intervals can be a new type of number like complex numbers. If you were looking for a module to perform interval calculations where would you expect to find it?

Next, my plan is to create a class that encapsulates construction and some validation while most of the interface would be contained in functions. For example to add two intervals the user would call `Math::Interval::add(iv1, iv1)` which would return the result as an interval. Would it be better to put everything inside the class? I know that this would increase potential naming conflicts if people just imported the entire namespace. Code like `\$arg1->add(\$arg2)` looks wrong to me.

Thanks for any pointers and suggestions!

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.