# Significant Digits (Significant Figures)

Discussion in 'Javascript' started by SMH, Jan 7, 2007.

1. ### SMHGuest

Anyone found any mature/well-tested code that actually produces
significant figures for any real number result of an arithmetic operation,
of course represented as a string.

This is code I developed some time ago, but I don't think all the bugs
have been worked out or that all the logic is sound, given the criteria
for determining significant digits.

======= begin code ========

/* Two functions are defined:

1. getSigDigits() is supposed to return to the caller the
count of significant digits of the argument passed to it.

The function takes a single argument of type string or number.
If passed a string, it uses regular expressions to attempt
to find the count of signficant digits.
If passed a number, it renders the number to a string
represented by the toExponential() method and then uses
a regular expression to attempt to analyze the mantissa.

2. setSigDigits() is supposed to return to the caller a number
properly formatted (and rounded) represented as a string
with the proper significant digits.

It takes two arguments:
(i) parameter #1 is the value to be adjusted/formatted, and
can be of type string or number (limited argument checking
is done; and could probaby be improved)
(ii) parameter #2 is the count of significant digits to use
to format the value; more extensive argument checking is
done to make sure it is a positive integer, and a default
is applied if there is a range or type error.

The function essentially attempts to obtain a mantissa
of a number that might be expressed in scientific notation,
to which rounding is applied in the case of truncation or
to which the number is extended with zeroes.

The idea would be to determine the count of significant
digits on each operand in an arithmetic operation using
getSigDigits(), producing the answer/result, and then
formatting the answer with setSigDigits().

*/

function getSigDigits(value)
{
var result;
if (typeof(value) == "string")
{
var sigdigs = 0;
result = value.match(/(\d+)\.?(\d*)|(\d*)\.?(\d+)/);
if (result[1] != null || results[2] != null)
{
sigdigs += results[1] ? result[1].length : 0;
sigdigs += results[2] ? result[2].length : 0;
return (sigdigs);
}
sigdigs += results[3] ? result[3].length : 0;
sigdigs += results[4] ? result[4].length : 0;
return (sigdigs);
}
else if (typeof(value) != "number")
return (null);
if ((result = value.toExponential().match
(/[eE]{1}[+\-]?\d{1,3}/)) == null)
return (null);
return (result[1].length);
}

function setSigDigits(value, digits)
{
if (typeof(value) == "undefined" || value == null)
return (null);
var exp, val;
if (isNaN(digits) == true || digits <= 0)
digits = 6;
var sign = 1;
if (value != 0.0)
{
var sign = 1;
if (value < 0)
{
sign = -1;
value = Math.abs(value);
}
val = log10(value);
val -= (exp = Math.floor(val)) - 7;
val = Math.round(Math.pow(10, val));
val = Math.pow(10, log10(val) - 7 + exp);
val *= sign;
}
else
val = value;
val = new String(val);
if (val.search(/\./) > digits)
val = val.substr(0, digits);
else
val = val.substr(0, digits + 1);
return (val);
}

======= end code ========

SMH, Jan 7, 2007