Solved

# Parse Scientific Notated Number

Posted on 2003-10-24
493 Views
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.
0
Question by:beaudetious
• 4
• 3
• 2
• +2

LVL 22

Expert Comment

ID: 9617380

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

LVL 9

Accepted Solution

msdixon earned 125 total points
ID: 9617381
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

LVL 6

Expert Comment

ID: 9618751
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

LVL 6

Expert Comment

ID: 9618764
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

LVL 6

Expert Comment

ID: 9618771
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

LVL 1

Author Comment

ID: 9619518
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

LVL 44

Expert Comment

ID: 9619979
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

LVL 22

Expert Comment

ID: 9620158

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

LVL 44

Expert Comment

ID: 9620911
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

LVL 1

Author Comment

ID: 9627949
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

LVL 6

Expert Comment

ID: 9628553
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

LVL 22

Expert Comment

ID: 9628868

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

## Featured Post

### Suggested Solutions

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …