Templates For Numerics

From WhyJavaIsGreat

No messy signed/unsigned/short/long/double issues. Question: in C++ what size/signedness is the result of this: uint64*1000?

In JavaLanguage you have to accept the definitions in the language. If you don't like the sizes you're given then you need to create a class, which is no longer a primitive type (and for which you can't have overloaded operators).

In contrast, CeePlusPlus allows to to control and precisely define the answer to the above question. The solution is to use templates. A template can encapsulate the task of finding the sizeof(int) on a machine, and provide efficient implementations for Uint<N>, SaturatedInt<N>, IntegerRange<low,high>, etc. that have very precise semantics.

Examples include the integer library at http://www.boost.org (see BoostLibraries), the fixed-point library in SystemCee and, of course, various wheels that I've invented myself. (Some implementations even know that Uint<54>*Uint<54> -> Uint<108>; and that Int<23>+Int<3> -> Int<24>).

Of course, the question at the top of this page, as stated, has no correct answer. But a library can define an answer that is correct in the context of the library. Uint<64>(x)*Int<11>(1000) might reasonably be expected to result in Int<76>. A different template might (correctly) answer Int<128>; another might (also correctly) define the answer as Int<64>. The point is that it is possible to write a template that unambiguously answers such questions for the user of the library. This is difficult in languages like Java or Eiffel that don't allow a library to add new primitive types by value-based specialization of generics. (Any OO language can fake it at run-time, but that's not always what's wanted. Of course, not all languages can overload the operators so even the run-time variants can be a bit clumsy to use).

-- DaveWhipp

Dave, why do you care so much about sizes unless it is to interface with some other low-level code that cares about sizes? As long as you have a portable format, and ranges are correct, who cares? Also, why care about signed versus unsigned? -- rad

You are right, sometimes. Of course, numerics is one of those areas where some people need to care about size and sign etc.. Matching code to the 'natural' types of a given CPU can change run times by order days to weeks. So yes, in that case you care. If you are doing toy problems or small runs, there is no reason not to write in java for example. Or in perl/python/whatever for that matter. Of course, if this is the case, why not do it in matlab or something similar?

However, when performance really matters, none of the above cut it. Carefully constructed template libraries can give you the speed of fortran 77 (see BlitzPlusPlus, pooma, tnt) with the flexibility and library support of c++ (not the best, but compared to fortran....). This is why c++ is rapidly becoming the LanguageOfChoice for high end numerics.

-- anon 3

Sounds like a very good example of DoTheMostComplexThingThatCouldPossiblyWork

Maybe, but also OnceAndOnlyOnce. You write the template once, put it in a library, and then you never have to worry about it again. If, like me, you need a lot of different sizes of integer (I model hardware), then *using* these templates is simpler then using the built-in types. -- AnonymousDonor

The <N> parameter makes it complex - the user has to say up front what precision they want. This often amounts to premature specification and/or optimisation. In Smalltalk you can just use an Integer and the system will get the size right, growing from 24 bits to 25 bits if it needs to. Of course, you define a variable length Integer in C++ too. There are 2 problems:

-- AnonymousDonor2

But sometimes you need to know the size up front, not for premature optimization but for appropriate optimization. Sometimes speed counts (and smalltalk doesn't have it). On the other hand, this should not apply to the vast majority of codes. -- anon 3

also as stated above these things are generally used when doing things like designing hardware, where knowing the size of each number is probably the entire point of modelling it in software in the first place.


The real power of templates for numerical programming shows in methods like ExpressionTemplates.


See UsingTemplates, TemplateMetaprogramming


CategoryCpp CategoryJava CategoryCppTemplates


EditText of this page (last edited August 19, 2010) or FindPage with title or text search