Parse Scientific Notated Number

OK, I've got a number (say 0.5004) and I using custom formatting I get into a string such as "5E-01".  Now, my question is this, not using string methods (i.e. finding the index of "E" and use substring() to pull out the "-01" portion), is their a mathematical way to retrieve the integral value of "-01" in the scientific notated number?  I'd like a method that would return the following values:

value             s.n.               return
0.5004          "5E-01"          -1
5.004            "5E+01"         +1
50.04            "5E+02"         +2
etc.

Using the Math functions in C# is there a way to do this?  It's been many, many years since I've taken a math course or cracked a math book, so this might be an easy question.

If there's not, would someone help me come up with the right algorithm using string methods?  Thanks.
LVL 1
beaudetiousAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

_TAD_Commented:


yuppers.... you need to use logs.

.5 = 5 * 10^ -1
5 = 5 * 10^ 1
50 = 5 * 10^ 2


val = x * 10^ n

log(val) = log(x) + n*log(10)   -> log(10) = 1
log(val) = log(x) + n
log(val) - log(x) = n
log(val/x) = n

So....

If your value is 5,983,899  you know your scientific notation is going to be 5.983 899 E ??

Instead of doing all the multiplication or division you can simply log(5983899/5.983899) = 6


0
msdixonCommented:
the best way would be to store the actual value somewhere. you will loose precision if you convert back and forth between decimal and scientific notation display.

i don't know if there's anything build in (see the System.Math class)

if you really want to have a decimal equivalent of 5E-1 you can do something like this:

string text = "5E-01";
string[] values            = text.ToLower().Split('e');
double val                  = double.Parse(values[0]);
int exponent            = int.Parse(values[1]);
double ret                  = val * Math.Pow(10, exponent);
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
zrhCommented:
Try something like:

public static double GetExponent(double d) {
      double j;
      if(d >= 1)
            for(j = d; j > 10.0; j /= 10);
      else
            for(j = d; j < 1; j *= 10);

      return Math.Log10(d/j);
}

Hope that helps,
ZRH
0
OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

zrhCommented:
Sorry didn't see rest of question (I think your asking for this too...), to get s.n. in string:
public static string GetScientificNotation(double d) {
     double j;
     if(d >= 1)
          for(j = d; j > 10.0; j /= 10);
     else
          for(j = d; j < 1; j *= 10);

     return j + "E" + Math.Log10(d/j);
}
ZRH
0
zrhCommented:
I'm sorry, I don't really know what your asking. :)
But if your asking to get a double from a string with a number in scientific notation in it, you can just use the Double.Parse(string) method.
It supports scientific notation.

But besure to catch that pesky FormatException....

ZRH
0
beaudetiousAuthor Commented:
Folks, I really just want an integer value of the portion of the scientifically notated number that follows "E" (or "e" whatever).  I knew how to do it using string functions (figured it out yesterday) but I thought perhaps there was a mathematical approach similar to zrh's.  I'll leave the actual formatting of the number to my grid component.
0
Arthur_WoodCommented:
the exponent is the Integer part of the Logarithm BASE 10 of the Number.

the Log function in VB 6 is  the natural logarithm which is the logarithm to the base e. The constant e is approximately 2.718282.

in order to get Log Base 10, define your own function, in a Module, like this:

Public  Function Log10(X) as Double
    Log10 = Log(X) / Log(10#)
End Function

then if you take the Integer value of the Log10(x) you will the the number you need.

AW

.

0
_TAD_Commented:


from a math standpoint, it doesn't matter if you use natural log or base 10 log (or log base 3, 7 9 42...etc, etc).  The only thing that is imprtant is that every element uses a log.


The main reason for using log base 10 is because logBase10(10) = 1

If you have an equation 370.45  = 42^n you could use a log base 42  and do a few conversions

log42(370.45) = n * log42(42)   => n * 1

log42(370.45) = n

n = 1.585-ish


To keep things simple I'd just go with whatever your programming language uses as a default for its log function and just do the algebra.  Converting to log base 42 makes the problem much simpler if you are doing things long hand.  

As long as you've got a computer doing the work, then you don't need to make the math easy.
0
Arthur_WoodCommented:
almost all programming languages that I have ever dealt with (over a time span of 40+ years of programming) use Ln for Log, and NOT Log 10.

And using Log 10 makes the answer to  beaudetious' original question almost trivial.

True, you can convert from Ln to Log 10 quite easily, to get the correct exponent for Scientific notation, but Log10 makes it much easier.

AW
0
beaudetiousAuthor Commented:
Which is more efficient?  The string approach as mentioned by msdixon or the mathematical approach mentioned by zrh.  I've tried both successfully and am tempted to go with the string approach since it's fast, simple and easy for future maintenance programmers to understand.
0
zrhCommented:
One thing to consider, using the parse method of double, and int can lead to many bugs, because if the format isn't exactly how is expected, it will throw exceptions.  And also, I would think that string manipulation would always be slower than integer/double math.  The string is stored in a character array, so theres a pretty good chance that loops are used in the ToLower, Split, and Parse methods.  The loops in the mathmatical version probably could be optimized some, but my opinion is that the mathmatical approach is faster.

However, depending upon your maintainance programmer's "ability", the string method would probably be easier to understand.

Hope that helps,
ZRH
0
_TAD_Commented:


I'll have to side with ZRH on this one...

While the string manipulation answer might be easier to understand right off, it will always be a bit slower.

However, if you are only doing this once or twice in a program, then the 20 ms longer you have to wait is really trivial.  If on the other hand you have to do this 1,000, 10,000 or a million times (in a loop) then I would *strongly* encourage using the mathematical method put forth by ZRH.  

20ms * 1 million itterations comes to nearly 6 hours longer to use the string function over the math version.

ps> the additional 20 ms for the string funciton is a ficticious number.  It could be more or less than that number.  I have not actually tested the duration.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.