Channels ▼

Bil Lewis

Dr. Dobb's Bloggers

The Language of Math

March 21, 2011

The language of math is an odd creature, idiosyncratic and given to a huge number of inconsistencies and assumptions. Some symbols are Roman, some are Greek, some are Hebrew, and some just unique. Their meanings are dependent upon the specific equation and the reader is supposed to be able to infer when a superscript is a exponential and when it's just an index. Sometimes two letters next to each other imply multiplication, sometimes not. Sometimes square brackets indicate arrays, sometimes they're just big parentheses.

Still, it works just fine for mathematicians. For computer scientists; not so much. Other than Guy Steele's Fortress language, we invariably translate from "Math" to Java or Fortran, etc. And that is a problem.

Consider the equations above (which I found on some random web page — I have no idea what it is). No computer scientists in their right minds would write out an equation that long. They'd abstract the various bits into small functions that were meaningful. The right side of the second line would probably look something like:

ave(a, b) * area(1, n, e) + residue(e);

My contention is that translating between different languages to do the same tasks is fundamentally a bad thing.

Math isn't going to change any time soon, but the hundreds of computer languages we have that all do the exact same thing might.

It's common to hear people talk about how Python is better than Java for some tasks, while TCL is better for others, etc. Despite the fact that they are implementing the exact same operations in nearly the same fashion. Indeed, many Python programs can be translated to Java by adding type declarations and brackets.

While I was pondering this point, a friend of mine dropped by and I felt moved to question him on this. Instead of a reasoned conversation about the issues, what I got was:

"Ruby is totally superior to Java. I can write a program in 1/10 the amount of code!"

"Cool," says I, "Can you give me an example? For example if I wanted to add the "values" of vowels in a sentence?"

In C, I might write:

char *s = "The quick brown fox";
int sum = 0;
 
while (*s != 0) {
    if (isVowel(*s)) sum += *s;
    s++;
    }
return sum;

How would he write that in Ruby?

He didn't give me an example. He got angry and huffed that I would have to read the Ruby book about active objects. Briefly scanning a Ruby book later, I infer he was saying that if you wrote a Java program using only JDBC, it'd be ugly. Using Hibernate, a Java program would look just about the same as a Ruby program.

Such language-religion makes discussion pretty difficult.

In Java, the above program would look something like this:

String s = "The quick brown fox";
int sum = 0;
 
for (int i = 0; i < s.length; i++) {
    if (isVowel(s.charAt(i)) sum += s.charAt(i);
    }
return sum;

At this point I should like to ask the question: "Will the two programs return the same value?"

Answer No. 1: "Yes! Absolutely!" is a fine answer.

Answer No. 2: "No! Absolutely not!" is also fine.

Answer No. 3: "I think so." is not fine and is the point of this post.

"I think so" is how programs fail and how people get killed. As a Hang Glider pilot, I am putting my life on the line every time I fly. If somebody told me "I think this carabineer will hold you," I wouldn't launch.

So when we're writing complex computer systems, integrating code from dozens of languages that we think will be compatible, we are asking for trouble. Scanning a few web pages, I find that bugs due to "I think this will work" abound. I see HTML tags showing up regularly where text is supposed to be. I see odd characters where quotation marks were intended. This is a bad thing.

I believe that if we chose a single language for computational tasks and stuck to it, our world would be a far better place.

— Bil

Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 


Video