To take the quizzes, you must log in.

*Username:***student***Password:***Formulas.1**You can view this site in

the language of your choice.

- Significant figures
### Significant figures

## The sigfig() function

Starting with version 4.81, the Formulas question type has a built-in sigfig() function which displays values with the desired number of significant figures.

You can skip the rest of this page if you are using version 4.81 of the Formulas question type.

## Workarounds to the sigfig() function

Versions of the

**Formulas question type**earlier than 4.81 had no built-in function to display values with a given number of significant digits.Although it is relatively easy to use a javascript method or develop the required algorithm, different aspects of this seemingly simple problem are discussed in the rest of the page below.

**Tip:**Install version 4.81 of the Formulas question type and use the sigfig() function.The following solutions are useful workarounds if you are using earlier versions of the Formulas question type.

### Variable assignment

The following two variable assignment lines format any number with the desired number of significant digits :

- a=nsf-1-floor(log10(abs(x))); xr=x==0?x:round(x*pow(10,a),0)*pow(10,-a);
where x is the number to be rounded, nsf the number of significant digits to keep and xr the resulting rounded value.

#### Example

Use the Formulas variable assignment. Display integer and real values with different numbers of significant figures.

The Formulas question should look like this:

The Formulas variable assignment is very short and does not requires a java script; it is very easy to use. It always displays numbers in the integer or decimal format.

However, it does not display trailing zeros for real numbers. For example, for three significant figures it displays 4.7 as 4.7 not 4.70. This may or may not be suitable. See the section below to see how trailing zeros can be displayed.

### Javascript toPrecision() method

*The toPrecision() method returns a string representing the Number object to the specified precision.*Reference: MDN ↗. Note that the Number object can be an integer or a real value.#### Example

Use the javascript built-in toPrecision() method. Display integer and real values with different numbers of significant figures.

## Settings

General Question name! Significant figures - toPrecision() Variables Global variables a=1500345600789; b=1500345.600789; c=0.0001500345600789; out=[" Select and click Check"]; Main question Question text! Significant figures - toPresicion() Numbers are displayed below with different numbers of significant figures: <table> <tbody> ⁞ <tr> <td>1</td> <td><span id="xr11"></span></td> <td><span id="xr12"></span></td> <td><span id="xr13"></span></td> </tr> ⁞ <script> document.getElementById("xr11").innerHTML = f201803241848({a},1); document.getElementById("xr21").innerHTML = f201803241848({a},2); document.getElementById("xr31").innerHTML = f201803241848({a},3); document.getElementById("xr41").innerHTML = f201803241848({a},4); ⁞ function f201803241848(x,s) { var xr = x.toPrecision(s); return xr; } </script> Part 1 Part's mark* 1 Answer type Number Answer* 0 Grading criteria* Absolute error == 0 Part's text {_0:out}The Formulas question should look like this:

For

**integer values**, the following observations can be made:- The exponential format is always used, except for the trivial case where the number of significant figures is equal to the number of digits of the number.
- When the numbers of significant figures exceeds the number of digits of the number, zeros appear as decimals.

- The exponential format is always used, except for the trivial case where the number of significant figures is equal to the number of digits of the number.
Regarding the first observation, the exponential format is used because it clearly indicates the number of significant figures. For example, 1.5003456e+12 clearly has 8 significant figures whereas 1500345600000 could have any number of significant figures between 8 and 13 (if the last 5 zeros are relevant).

Yet, in many cases we would like to keep the integer format, i.e. 1500345600000 would be preferred over 1.5003456e+12.

Regarding the second observation, it would be preferable to not have a decimal point and decimal zeros, even if the number of significant figures is greater than the number of digit in the number. In this case, the number should be displayed simply with all its digits.

For

**real values**, the following observation can be made. For values greater than zero and when the number of significant figures is less than the number of digits of the integral part, the exponential format is used for the same reason as explained above for integer values.However, in many cases we would like to keep the real format, i.e. 1500000 would be preferred over 1.5e+6, 1.50e+6 or 1.500e+6.

## Integer format

### Variable assignment

The variable assignment, presented above, works well for integer numbers. See the first example at the top of this page.

### Java script

This format was developed using a java script. Contrary to the toPrecision() method, the integer format is always preserved.

Consider a number

*x*displayed with*s*significant figures as*xr*. We can handle loose input by setting the following rules:- If
*x*is real, it is first rounded to the nearest integer before applying the format. - If
*s*is negative, then*xr*= 0. - If
*s*is real, it is rounded to the nearest integer before applying the format. - If
*s*= 0, then*xr*= round(*x*).

- If
The java script for this format is as follows:

- if (s<0) {xr = 0;} else { s = Math.round(s); if (s==0) {xr=Math.round(x);} else { var a = s-1-Math.floor(Math.log10(Math.abs(x))); var b = Math.pow(10,a); var c = Math.pow(10,-a); var d = Math.round(x)*b; var e = Math.round(d); var xr = Math.round(e*c); } }
#### Example

Use the integer format loose input. Let the user try any value.

## Settings

General Question name! Significant figures - Integer format (loose input) Variables Global variables out=[" Select and click Check"]; Main question Question text! <input type="number" id="number201803242140" style="width:100px" step="any"> <input type="number" id="sigfig201803242140" style="width:45px" step="any"> <input type="button" value="Submit" onclick="f201803242135()"> <p>Gives: <span id="id201803242134"></span></p> <script> function f201803242135() { x = document.getElementById("number201803242140").value; s = document.getElementById("sigfig201803242140").value; if (s<0) {xr = 0;} else { s = Math.round(s); if (s==0) {xr=Math.round(x);} else { var a = s-1-Math.floor(Math.log10(Math.abs(x))); var b = Math.pow(10,a); var c = Math.pow(10,-a); var d = Math.round(x)*b; var e = Math.round(d); var xr = Math.round(e*c); } } document.getElementById("id201803242134").innerHTML = xr; } </script> Part 1 Part's mark* 1 Answer type Number Answer* 0 Grading criteria* Absolute error == 0 Part's text {_0:out}The Formulas question should look like this:

## Javascript (decimal format with trailing zeros)

#### Example

This example is a significant figures drill.

General Question name! Significant figures drill Variables Random variables # a plus or minus sign # b exponent varying from -4 to 4 in steps of 0.01 # rnsf random number of significant figures: 1, 2, 3, or 4 a={-1,1}; b={-4:4.01:0.01}; rnsf={1,2,3,4}; Global variables # nx number x = ± 10^b nx=a*pow(10,b); # Rounding routine: # x number to round # nsf number of significant figures # xr rounded value of x x=nx; nsf=rnsf; xr=x==0?x:round(x*pow(10,nsf-1-floor(log10(abs(x)))),0)*pow(10,-nsf+1+floor(log10(abs(x)))); Main question Question text! Significant figures drill Part 1 Part's mark* 1 Answer type Number Answer* xr Grading criteria* Relative error < 0.0001 Part's text Number to round: {nx} Number of significant digits to keep: {ncs} Rounded number: {_0} Combined feedback For any correct response The correct answer is: {xr} For any incorrect response The correct answer is: {xr}

In the Variables fields (random, global and local), lines starting with # are treated as comments.

It is always a good idea to document your work using comments in the '''Variables''' fields.In this example, values to be rounded range from -10 000 to +10 000. In order to have the same number of values in each order of magnitude, hence a more interesting exercise, the random variable b is used as a power of 10. By letting:

*x*= ± 10^{b}with b ranging from -4 to +4 in steps of 0.01, there are one hundred random values in each order of magnitude (0.000 1 to 0.001, 0.001 to 0.01, 0.01 to 0.1, 0.1 to 1, 1 to 10, 10 to 100, 100 to 1 000 and 1 000 to 10 000), both positive and negative.

:*In total, this example generates 6 408 different random questions*2 (a={-1,1}) × 801 (b={-4:4.01:0.01}) × 4 (rnsf={1,2,3,4}) = 6 408

The Formulas question should look like this: