Solved

Parse Scientific Notated Number

Posted on 2003-10-24
12
494 Views
Last Modified: 2008-02-01
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
Comment
Question by:beaudetious
  • 4
  • 3
  • 2
  • +2
12 Comments
 
LVL 22

Expert Comment

by:_TAD_
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

by:
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

by:zrh
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

by:zrh
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

by:zrh
ID: 9618771
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
 
LVL 1

Author Comment

by:beaudetious
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 44

Expert Comment

by:Arthur_Wood
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

by:_TAD_
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

by:Arthur_Wood
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

by:beaudetious
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

by:zrh
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

by:_TAD_
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
C# Error - Add Failed 12 78
How to avoid saving tif files a 2nd time using VS2015 and C#? 1 27
DateTimepicker 4 33
C# Json POSt to Rest API 4 32
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now