Calculator Collection | About the Calculator Series | Inside the Code | Home

Precision versus Units

Enter precision in one of the following units: ratio, percent, ppm, dB, Nbits.
Then, click on "Calc Other Units" in the same row.


ratio = per / 100
per = ratio * 100
ppm = ratio * 1e6
dB = 20*log10( ratio )
Nbits = log2( 1/ ratio)

Download / Basics

To run or modify on your PC, download a simple version of this file.
For a quick tour of some JavaScript / HTML basics, check out Inside the Code.


Although the form has many buttons named "Cal Other Units", just one function call handles them all. The trick lies in passing a specific text ("ppm", "dB", Nbits", etc.) to the function depending on which button was pressed. The text is passed to the variable "units" inside the routine..

// calc Precision
function get_Precision( units ) {

	// get values directly from form
	var ratio=document.myForm.ratio.value;		
	var per=document.myForm.per.value;
	var ppm=document.myForm.ppm.value;
	var dB=document.myForm.dB.value;
	var Nbits=document.myForm.Nbits.value;
	// convert entered unit to ratio
	if ( units.indexOf("ratio") >= 0) {
		ratio = ratio*1;
	if ( units.indexOf("per") >= 0) {
		ratio = per/100;
	if ( units.indexOf("ppm") >= 0) {
		ratio = ppm/1e6;
	if ( units.indexOf("dB") >= 0) {
		ratio = Math.pow(10,dB/20);
	if ( units.indexOf("Nbits") >= 0) {
		ratio = Math.pow(2,-Nbits);
	// calc all remaining units
	per = ratio*100;
	ppm = ratio*1e6;
	dB = 20*Math.log(ratio)/Math.log(10);
	Nbits = Math.log(1/ratio)/Math.log(2);
	document.myForm.ratio.value = (ratio).toPrecision(2);
	document.myForm.per.value = (per).toPrecision(2);
	document.myForm.ppm.value = (ppm).toPrecision(4);
	document.myForm.dB.value = (dB).toPrecision(3);
	document.myForm.Nbits.value = (Nbits).toPrecision(2);


The code checks which units have been passed using the JavaScript function: units.indexOf("text"). This function return a number indicating the index in the string where "text" is found. If no "text" is found, the function returns a -1.

As far as the calulations go, the code first converts any other units to a ratio. Then, the ratio is converted to all other units.


Here's the HTML code for the button next to the "ppm" input box. The function call for this button "get_Precision( 'ppm')"  passes the text 'ppm' to the routine.

<td><input type="text" name="ppm" size="8" value=""></td>
<td><input type="button" value="Calc Other Units"