Thursday, November 10, 2011

The insignificance of significant figures in programming languages

If a city with a population figure of 500,000 gets three more residents, the population figure is... 500,000, not 500,003. The reasoning is this: the original figure was accurate only to the first digit (the hundred-thousands digit). It has a finite precision, and adding a number that is smaller than the precision has no affect on the original number.

Significant figures is not the same as "number of decimal places", although many people do confuse the two.

Significant figures are needed for calculations with measured quantities. Measurements will have some degree of imprecision, and the rigor of significant figures keeps our calculations honest. The rules for significant figures are more complex (and subtle) than a simple "use 3 decimal places". The number of decimal places will vary, and some calculations may affect positions to the left of the decimal point. (As in our "city with 500,000 residents" example.)

For a better description of significant figures, see the wikipedia page.

Applications such as Microsoft Excel (or LibreOffice Calc) have no built-in support for significant figures. Nor, to my knowledge, are there plug-ins or extensions to support calculations with significant figures.

Perhaps the lack of support for significant figures is caused by a lack of demand. Most spreadsheets are built to handle money, which is counted (not measured) and therefore does not fall under the domain of significant figures. (Monetary figures are considered to be exact, in most applications.)

Perhaps the lack of support is driven by the confusion between "decimal places" and "significant figures".

But perhaps the biggest reason for a lack of support for significant figures in applications is this: There is no support for significant figures in popular languages.

A quick search for C++, Java, Python, and Ruby yield no such corresponding packages. Interestingly, the only language that had a package for significant figures was Perl: CPAN has the Math::SigFigs package.

So the better question is: Why do programming languages have no support for significant figures?

2 comments:

chiptochip said...

Interesting. Can you give a non-trivial example where this would be useful? In the population example you could always just round it off to the desired significance after doing the addition.

jfitz said...

Numerical calculations that would benefit from built-in significant figures would be those that use measured quantities. Anyone using CAD/CAM systems or designing buildings or bridges will appreciate the need for them. Embedded systems (such as those in our automobiles) use measured quantities and should be using significant figures for their calculations.