Link to home
Start Free TrialLog in
Avatar of kcvs
kcvs

asked on

Finding number of significant digits

Hello, I am creating an application in Adobe Flash Actionscript 3, and I need a function that can inspect and return the number of significant digits in a floating point number.  I have tried comparing results of String(myNum) and myNum.toPrecision(SigDigsRequired), however, this function breaks down with numbers like 200, where

trace("200",  Number(200).toPrecision(2),  Number(200).toExponential(2));

gives me this:
200   2.0e+2   2.00e+2

What I really need is a function like getNumberOfSigDigs(number)

Any Ideas?
Avatar of dhsindy
dhsindy
Flag of United States of America image

Significant digits depends on where the number came from (accuracy of the measurement, etc.).  I don't think you can have a computer function that can determine the accuracy of the input.  You would have to pre-define what the inputs represent.
Avatar of rascalpants

this seems like a good use for a Regular Expression, which i suck at, or just a string comparison with conditionals...

based on the rules here...

1. All non-zero digits are significant.
2. In a number without a decimal point, only zeros BETWEEN non-zero digits are significant (unless a bar indicates the last significant digit--see below).
3. In a number with a decimal point, all zeros to the right of the first non-zero digit are significant.

from....  http://en.wikipedia.org/wiki/Significant_digit


I think there is a pretty good chance that you can come up with an algorithm for it.

maybe a conditional like this...


basically, you just pass a String into the function... if you try to pass a Number, it will take a number like 100.0 and turn it to 100


does this work for you?


rp / ZA

function checkForSig( theNum:String ):int
{
	var sigNums:int;
	if( theNum.indexOf(".") > -1 ) sigNums = theNum.split(".").join("").length;
	else sigNums = theNum.split("0").join("").length;
 
	return sigNums;
}
 
trace( checkForSig( "100.01" ) );  // give me 5

Open in new window


actually, the last one had a fault...

try this one...  let me know if you can find a number that is not caught...


rp / ZA



// AS 3.0
 
function checkForSig( theNum:String ):int
{
	var sigNums:int;
	if( theNum.indexOf(".") > -1 )
	{
		sigNums = theNum.split(".").join("").length;
	}
	else if( theNum.indexOf("0") > -1 )
	{
		var arr:Array = theNum.split("");
		var len:int = arr.length;
		sigNums = arr[ len - 1 ] == "0" ? theNum.split("0").join("").length : theNum.length;
	}
	else
	{
		sigNums = theNum.length;
	}
	
	return sigNums;
}
 
trace( checkForSig( "10.50" ) );

Open in new window

Avatar of kcvs
kcvs

ASKER

Hi rascalpants, I found that your algorithm did not work for 0.0000010 (which should be 2):

trace( checkForSig( "0.0000010" ) );
//outputs 8


and one more update to fix the occurance of 0.00012 as having 6 instead of the correct value of 2...


hope this works for you...  at least it was a fun challenge...


rp / ZA

// AS 3.0
 
function checkForSig( theNum:String ):int
{
	var sigNums:int;
	if( theNum.indexOf(".") > -1 )
	{
		var decArr:Array = theNum.split(".");
		sigNums = decArr[ 0 ] == "0" ? decArr.join("").split("0").join("").length : decArr.join("").length;
	}
	else if( theNum.indexOf("0") > -1 )
	{
		var arr:Array = theNum.split("");
		var len:int = arr.length;
		sigNums = arr[ len - 1 ] == "0" ? theNum.split("0").join("").length : theNum.length;
	}
	else
	{
		sigNums = theNum.length;
	}
	
	return sigNums;
}
 
trace( checkForSig( "120.00" ) );

Open in new window

you where too quick for me  ;)


rp / ZA
hold on one more second...

:)

rp / ZA
ASKER CERTIFIED SOLUTION
Avatar of rascalpants
rascalpants
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kcvs

ASKER

Thanks!
no problem...  I love a challenge  ;)

what exactly can this be used for now?  lol




rp / ZA

Avatar of kcvs

ASKER

I have a graphing component in flash, and in the interest of making a graph that looks nice, I am trying to find a suitable value for the difference between consecutive x-axis labels.  The graphing utility can graph any range of values, and supports zooming and moving around.  This causes some pretty messy numbers to be displayed on the x-axis labels.  This code will help me find the best difference between the consecutive x-labels so the display is intervals are easy to read.

:)

wow...  my code sounds like childs play compared to what you are putting it into

;)

good luck...

rp / ZA