Skip to content
Follow me on twitter!

How to be more confident about your own programs: an example using Perl

Programming is one of the most difficult branches of applied mathematics; the poorer mathematicians had better remain pure mathematicians.

Perl Camel

It was Edsger W. Dijkstra, the famous computer scientist, who wrote these words in his note named “How do we tell truths that might hurt?“. I am sure that many people didn’t like to read them, and didn’t understand what he meant.

Although it is not my intention to discuss his words, I want to present a simple example that demonstrates how mathematics can be used to program better and to make you more confident about your own programs.

This post includes a fair amount of mathematical definitions and concepts, but they should not be difficult to understand. After the mathematical discussion, I present an example using the Perl programming language (it also applies to languages like C or Java).

The Problem

The problem I am going to deal with, involves the ceiling and floor functions. If you don’t remember, the floor of a real number LaTeX Formula is (usually) written as LaTeX Formula and it is defined as the greatest integer that is at most LaTeX Formula. Similarly, the ceiling of a real number LaTeX Formula is written as LaTeX Formula and it is defined as the smallest integer that is at least LaTeX Formula.

The goal is to implement the ceiling function supposing that our programming language only provides the floor function to round numbers. Formally, given a real LaTeX Formula, we want to calculate an expression LaTeX Formula such that:

LaTeX Formula

The ceiling and floor functions

As I said before, the floor of a real number LaTeX Formula is the greatest integer that is at most LaTeX Formula. Formally, we capture this notion in the following definition:

For all real LaTeX Formula, LaTeX Formula is an integer such that, for all integers LaTeX Formula,

LaTeX Formula

This very compact and elegant definition allows us to derive some properties very quickly. If, for instance, we instantiate LaTeX Formula to be LaTeX Formula, we get:

LaTeX Formula

Since the left side of the equivalence is true, the right side is also true. We thus have our first property:

LaTeX Formula

Please note that since LaTeX Formula is an integer by definition, we can only replace it by integer values. That is why we can replace it by LaTeX Formula, which is an integer, but we can’t instantiate it to be LaTeX Formula because LaTeX Formula is a real number. However, we can replace LaTeX Formula by LaTeX Formula, since all integers are also reals. Replacing LaTeX Formula by LaTeX Formula, we get:

LaTeX Formula

which simplifies to

LaTeX Formula

Now, instantianting LaTeX Formula to LaTeX Formula in (0), we can also conclude that

LaTeX Formula

Finally, from (1) and (2), we conclude that

LaTeX Formula

We already knew this property: the floor of an integer number LaTeX Formula is LaTeX Formula.

We can do a very similar analysis for the ceiling function. We’ve seen that the ceiling of a real LaTeX Formula is the smallest integer that is at least LaTeX Formula. So, formally, we have:

For all real LaTeX Formula, LaTeX Formula is an integer such that, for all integers LaTeX Formula,

LaTeX Formula

Note that we can apply the contrapositive rule (that’s when we negate both sides) to this definition. Doing that, we get the equivalent definition:

For all real LaTeX Formula, LaTeX Formula is an integer such that, for all integers LaTeX Formula,

LaTeX Formula

I leave as an exercise to prove the dual properties of the ones we’ve seen for the floor function.

Indirect equality and the solution to the problem

Ok, so now that we have seen the formal definitions of the ceiling and floor functions, we can proceed. Note that these definitions have the aproppriate shape to use the proof technique named Indirect Equality. Briefly, the rule of Indirect Equality claims that two numbers LaTeX Formula and LaTeX Formula are equal if, for all numbers LaTeX Formula of the same type as LaTeX Formula and LaTeX Formula, the following holds:

LaTeX Formula

Recall that we want to calculate an expression LaTeX Formula, such that, for a given real LaTeX Formula:

LaTeX Formula

To use indirect equality, we will prove, for all LaTeX Formula, the following:

LaTeX Formula

The detailed proof is as follows:

LaTeX Formula

We have thus proved that:

LaTeX Formula

Note that we still have a problem: our floor expression still involves an application of the ceiling function! However, since

LaTeX Formula

we can easily determine the value of LaTeX Formula by a simple case analysis. If LaTeX Formula is an integer, then LaTeX Formula , else LaTeX Formula. Hence,

LaTeX Formula

An example in Perl

An example of a language that does not provide a native function ceiling is Perl. The only native function related with rounding numbers (as far as I know) is the function int. This function is a mixture of the floor and ceiling function: for positive numbers it behaves as the floor function and for negative numbers it behaves as the ceiling function. This means that to solve the problem in Perl, we just have to worry about positive arguments!

Therefore, from our previous analysis, we can write a correct implementation of the ceiling function as follows:

sub correct_ceil {
  my $r = shift;
  my $frac = $r - int($r);
  if($frac<=0) {
    return int($r);
  } else {
    return 1+int($r);
  }
}

And that’s it! We have a real proof that this function is correct! Although the problem is simple, it is very common to make mistakes. As an example, I found the following wrong ceiling implementation in a programming forum:


# ceil() funtion to use instead of loading in
# the POSIX module each time
{
local ($amount) = @_;
$floor = int($amount);
$ceil = int($amount + 0.5);
if ($ceil == $floor)
{
$ceil++;
}
return $ceil;
}

Clearly, this function does not work for integer arguments! I just hope it is not being used anywhere :)

Note: in Perl you can import the module Posix and use the function ceil, but our goal was to write the ceiling function knowing that the floor function is defined.

Conclusions

The main goal of this post was to show how mathematical arguments can be used to give us confidence in our own programs. I’m aware that, although the ceiling and floor functions are widely used (in financial applications, for instance), this example could be presented in a different way. The most obvious drawback is that the main proof is quite long; it could be shortened, but I would have to omit some intermediate steps.

On the positive side, once we adopt the definitions given, most part of the proofs involving the ceiling and floor functions become similar, and thus systematic. This allows us to reuse the proof techniques over and over again.

I hope you have enjoyed the post and I’d be glad to get some comments!

A much better solution by Rob Mayoff

Rob Mayoff proposed a much better solution (see comments) to this problem! A proof of his solution can be:

LaTeX Formula

Therefore,

LaTeX Formula.

Thanks Rob!

Related Articles:

3 Comments

  1. Rob Mayoff wrote:

    There’s a simpler (IMHO) definition of ceil(x) in terms of floor(x):

    For every integer n, n = -floor(-y). (eq. 2)
    By algebra, n = y. (eq. 3)
    Substitute eqs. 2 and 3 into eq. 1: for every integer n, -n >= -floor(-y) iff -n >= y. (eq. 4)
    If predicate p(-n) is true for all integers n, then p(n) is also true for all integers n since both n and -n are integers. So replace -n with n in eq. 4.
    For every integer n, n >= -floor(-y) iff n >= y.
    For every integer n, n >= ceil(y) iff n >= y. (def’n of ceil)
    Therefore ceil(y) = -floor(-y).

    In perl:

    sub ceil {
    return ($_[0]

    Thursday, July 26, 2007 at 2:44 am | Permalink
  2. Rob Mayoff wrote:

    My previous comment got completely hosed because the blog software threw away every less-than-symbol and all following characters until the next greater-than symbol. Oh well. The point is that ceil(x) = -floor(-x).

    Thursday, July 26, 2007 at 2:48 am | Permalink
  3. jff wrote:

    Thanks Rob! I’ve added the proof for your solution to the post. I don’t understand something in your argument, though. In equation 3, you say that n = y. But n is a natural and y is a real. And then you use this for equation 4. I believe the solution is correct, but I don’t understand your proof.

    Regarding the less-than in comments, I’ll try to fix it. Sorry about that!

    Thursday, July 26, 2007 at 8:25 am | Permalink

2 Trackbacks/Pingbacks

  1. [...] How to be more confident about your own programs an example using Posted by root 3 hours ago (http://www.joaoff.com) Jul 26 2007 the problem i am going to deal with involves the ceiling and floor functions my previous comment got completely hosed because the blog a square grid path problem jo o ferreira 2009 powered by wordpress Discuss  |  Bury |  News | How to be more confident about your own programs an example using [...]

  2. [...] How to be more confident about your own programs an example using Posted by root 1 hour 52 minutes ago (http://www.joaoff.com) Indirect equality and the solution to the problem i 39 m aware that although the ceiling and floor functions are widely used in financial applications my previous comment got completely hosed because the blog software threw away a square grid path problem Discuss  |  Bury |  News | How to be more confident about your own programs an example using [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*