Function Repository Resource:

SignificantDigits

Source Notebook

Determine the number of significant digits in a number

Contributed by: Wolfram Chemistry Team

ResourceFunction["SignificantDigits"][x]

gives the number of significant digits in x.

ResourceFunction["SignificantDigits"][{x1,x2,}]

gives the list of results for each xi.

Details and Options

All nonzero digits and zeros between nonzero digits are significant digits.
Zeros employed to position the decimal are not significant.
Zeros to the right of the decimal that are also right of the last nonzero digit are significant digits.
Trailing zeros in Integer numbers are not significant by default.
ResourceFunction["SignificantDigits"] automatically threads over lists.
Setting the option "DigitDecompose"True makes ResourceFunction["SignificantDigits"] return the number of leading zeros, significant digits and trailing zeros for the digits before and after the decimal as an Association of six Integer numbers. Significant trailing zeros are returned as negative values.
Setting the option "ExactNumber"True makes ResourceFunction["SignificantDigits"] treat Integer numbers as exact numbers.
Setting the option "TrimLeadingZeros"True makes ResourceFunction["SignificantDigits"] trim the leading zeros before processing.
Setting the option "UseTrailingZeros"True makes ResourceFunction["SignificantDigits"] treat trailing zeros in Integer numbers as significant.
ResourceFunction["SignificantDigits"] can also accept a string representing a number, such as "1.234".

Examples

Basic Examples (5) 

Find the number of significant digits in a Real number:

In[1]:=
ResourceFunction["SignificantDigits"][3.14]
Out[1]=

Find the number of significant digits in a String representing a number:

In[2]:=
ResourceFunction["SignificantDigits"]["1.2000"]
Out[2]=

Find the number of significant digits in an Integer number:

In[3]:=
ResourceFunction["SignificantDigits"][-69]
Out[3]=

Find the number of significant digits in an exact Real number represented as a Rational number:

In[4]:=
ResourceFunction["SignificantDigits"][5/6]
Out[4]=

Determine the number of significant digits for a List of values:

In[5]:=
ResourceFunction["SignificantDigits"][{0., 12.5, 7892, "-1.6900"}]
Out[5]=

Scope (8) 

Numerical Precision (1) 

Accuracy and Precision information in arbitrary-precision numbers is obeyed:

In[6]:=
ResourceFunction[
 "SignificantDigits"][{123.456`6, 123.456``200, 674``3.7, 734`8.1}]
Out[6]=

String Formats (5) 

AccountingForm is supported:

In[7]:=
ResourceFunction["SignificantDigits"][{"(19)", "(34.5190)"}]
Out[7]=

DecimalForm and PercentForm are supported:

In[8]:=
ResourceFunction[
 "SignificantDigits"][{"0.4839``4", ".62004", "393`3%"}]
Out[8]=

FortranForm and CForm are supported:

In[9]:=
ResourceFunction[
 "SignificantDigits"][{"1.1e5", "-3.2121E+4", "5.676898385d-09", "-23.5712D100"}]
Out[9]=

ISO expanded uncertainty is supported:

In[10]:=
ResourceFunction["SignificantDigits"][{"23.4782(32)", "45.6(1)"}]
Out[10]=

ScientificForm is supported:

In[11]:=
ResourceFunction[
 "SignificantDigits"][{"1.39856*^-6", "-38.490``3x10^3", "+1.2`2X10^-8"}]
Out[11]=

Uncertainty (1) 

Uncertainty in Around and VectorAround is obeyed:

In[12]:=
ResourceFunction[
 "SignificantDigits"][{Around[1.341, 0.01], Around[54.66``3], Around[56.723, {0.018, 0.076}]}]
Out[12]=
In[13]:=
ResourceFunction["SignificantDigits"][
 VectorAround[{1.8, 2.43}, {0.1, 0.01}]]
Out[13]=

Units (1) 

Quantity, QuantityArray and MixedMagnitude values with units are supported:

In[14]:=
ResourceFunction["SignificantDigits"][Quantity[3.45, "Moles"]]
Out[14]=
In[15]:=
ResourceFunction["SignificantDigits"][
 QuantityArray[{2., 1.59, 9.0`2}, "Meters"]]
Out[15]=
In[16]:=
ResourceFunction["SignificantDigits"][
 Quantity[MixedMagnitude[{1, 2}], MixedUnit[{"Feet", "Inches"}]]]
Out[16]=

Options (4) 

DigitDecompose (1) 

The number of leading zeros, significant digits and trailing zeros before and after the decimal can be returned as an Association:

In[17]:=
ResourceFunction[
 "SignificantDigits"][{"0067940", 0.0567, 123400, "1.000"}, "DigitDecompose" -> True]
Out[17]=

ExactNumber (1) 

Integer numbers can be treated as exact numbers:

In[18]:=
ResourceFunction["SignificantDigits"][{1230, 183, "393"}, "ExactNumber" -> True]
Out[18]=

TrimLeadingZeros (1) 

Leading zeros in a String of digits can be trimmed before processing:

In[19]:=
ResourceFunction[
 "SignificantDigits"][{"0098", "000.789", "00056.049"}, "DigitDecompose" -> True]
Out[19]=
In[20]:=
ResourceFunction[
 "SignificantDigits"][{"0098", "000.789", "00056.049"}, "DigitDecompose" -> True, "TrimLeadingZeros" -> True]
Out[20]=

UseTrailingZeros (1) 

Trailing zeros in Integer numbers can be treated as significant:

In[21]:=
ResourceFunction["SignificantDigits"][{"0680900", 4060}, "DigitDecompose" -> True]
Out[21]=
In[22]:=
ResourceFunction["SignificantDigits"][{"0680900", 4060}, "DigitDecompose" -> True, "UseTrailingZeros" -> True]
Out[22]=

Applications (1) 

Do significant digit-compliant subtraction:

In[23]:=
ResourceFunction["SignificantDigits"][5.6 - 5.78]
Out[23]=
In[24]:=
sigfigSubtract[x_, y_] := Module[
  {sfx, sfy, sfr, result},
  result = x - y;
  {sfx, sfy, sfr} = ResourceFunction["SignificantDigits"][{x, y, result}, "DigitDecompose" -> True];
  SetPrecision[result, sfr[[2]] + Min[sfx[[5]], sfy[[5]]]]
  ]
In[25]:=
ResourceFunction["SignificantDigits"][sigfigSubtract[5.6, 5.78]]
Out[25]=

Properties and Relations (5) 

Trailing zeros in Integer numbers are always insignificant by default:

In[26]:=
ResourceFunction[
 "SignificantDigits"][{800, 800., 3*^6, "4520", "23000"}]
Out[26]=

Enabling exact numbers also enables "UseTrailingZeros":

In[27]:=
ResourceFunction["SignificantDigits"]["009500", "ExactNumber" -> True,
  "DigitDecompose" -> True]
Out[27]=

Whitespace and commas in String values are ignored:

In[28]:=
ResourceFunction[
 "SignificantDigits"][{"  67,000  ", "3. 4 5 x 10^-4", "1 . D    -56   "}]
Out[28]=

String values without numbers in supported formats fail:

In[29]:=
ResourceFunction[
 "SignificantDigits"][{"Hello", "This is a test", "They are 75 years old."}]
Out[29]=

SignificantDigits output for Integer numbers is related to IntegerLength and IntegerExponent results:

In[30]:=
int = 12000;
ResourceFunction["SignificantDigits"][int] == IntegerLength[int] - IntegerExponent[int]
Out[31]=

SignificantDigits output for arbitrary-precision numbers is related to Precision and Accuracy results:

In[32]:=
ResourceFunction["SignificantDigits"][4.56`3 + 6.798``4] == Ceiling[Precision[4.56`3 + 6.798``4]]
Out[32]=
In[33]:=
ResourceFunction["SignificantDigits"][4.56`3 + 6.798``4, "DigitDecompose" -> True][[5]] == Floor[Accuracy[4.56`3 + 6.798``4]]
Out[33]=

Possible Issues (4) 

Around and VectorAround may produce unexpected results:

In[34]:=
ResourceFunction[
 "SignificantDigits"][{Around[1.23, 0.01], Around[1.23, 0.02]}]
Out[34]=

MachinePrecision numbers may produce unexpected results after computations:

In[35]:=
ResourceFunction["SignificantDigits"][{5.6, 5.789}]
Out[35]=
In[36]:=
ResourceFunction["SignificantDigits"][5.6 + 5.78]
Out[36]=
In[37]:=
ResourceFunction["SignificantDigits"][0.1*7]
Out[37]=

Scientific notation containing a decimal implies a Real number and may produce unexpected results:

In[38]:=
ResourceFunction["SignificantDigits"][{6.75*^5, 6.75`3*^5}]
Out[38]=

Trailing zeros in Real numbers are dropped unless specified using SetPrecision or SetAccuracy:

In[39]:=
ResourceFunction[
 "SignificantDigits"][{100.00, 9.0, SetAccuracy[9.0, 1], SetPrecision[9.0, 2]}]
Out[39]=

"UseTrailingZeros" is required to process an Integer number containing trailing zeros and a truncating Precision:

In[40]:=
ResourceFunction["SignificantDigits"][4900`3]
Out[40]=
In[41]:=
ResourceFunction["SignificantDigits"][4900`3, "UseTrailingZeros" -> True]
Out[41]=

Neat Examples (1) 

Compute a significant digit-compliant molecular mass from an element tally:

In[42]:=
sigfigMolarMass[elemtally_List] := Module[
   {acc, atm, mm},
   atm = ElementData[#, "AtomicMass"] & /@ elemtally[[All, 1]];
   acc = ResourceFunction["SignificantDigits"][atm, "DigitDecompose" -> True][[All, 5]];
   mm = atm . elemtally[[All, 2]];
   SetPrecision[mm, ResourceFunction["SignificantDigits"][mm, "DigitDecompose" -> True][[2]] + Min[acc]]
   ];
In[43]:=
(* TcOH *)
sigfigMolarMass[{{"Tc", 1}, {"O", 1}, {"H", 1}}]
Out[43]=

Publisher

Wolfram Chemistry Team

Version History

  • 1.0.0 – 03 December 2019

Source Metadata

Related Resources

License Information