title: Converting a number to normalized scientific notation
body: <p>I'm trying to create a method converting a number to normalized scientific notation, here is the code that I'm using to calculate mantissa and exponent:</p>
  
  <pre><code>ConvNSN 1000, M, P
  MsgBox M &amp; "e" &amp; P
  
  Sub ConvNSN(N, M, P)
      If N = 0 Then
          P = 0
          M = 0
      Else
          P = Int(Log(Abs(N)) / Log(10))
          M = N / 10 ^ P
      End If
  End Sub
  </code></pre>
  
  <p>The problem I am facing is that this code gives wrong exponent value for some numbers, eg 1000, 10E+6, 10E+9, 10E+12, 10E+13, etc... Exactly for 1000 converted should be 1e3, but not 10e2. It's obvious that the same problem with numbers, whose logarithms are close to an integer value, like <code>Log(1 - 5.55111512312578E-17) / Log(10)</code>, which result is 0, however <code>1 - 5.55111512312578E-17</code> less then 1, and result has to be negative.</p>
  
  <p>How can I get rid of <code>Double</code> type imprecision, and get this code to work properly?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I assume the fastest and quite accurate method to calculate mantissa and exponent of number in normalized scientific notation may be as follows:</p>
  
  <pre><code>Sub ConvNSN(N, M, P)
      Dim A
      If N = 0 Then
          P = 0
          M = 0
          Exit Sub
      End If
      A = Abs(N)
      If A &lt; 1 Then
          P = Int(Log(A) / Log(10))
      Else
          P = Int(Log(A) / Log(10) * (2 + Log(.1) / Log(10)))
      End If
      M = N / 10 ^ P
  End Sub
  </code></pre>
  
  <p>Or another one, based on @Bob's solution: </p>
  
  <pre><code>Sub ConvNSN(N, M, P)
      If N = 0 Then
          P = 0
          M = 0
      Else
          P = Int(Log(Abs(N)) / Log(10))
          M = N / 10 ^ P
      End If
      If Abs(M) = "10" Then
          M = M / 10
          P = P + 1
      End If
  End Sub
  </code></pre>
  
  <p>First one slightly faster. Both of them process exponent from -322 to 308, but return not normalized mantissa with powers of 10 less then -310. I have not tested them yet with numbers, whose logarithms are a marginally less but very close to an integer values.</p>
  
  <p><strong>UPDATE 2</strong></p>
  
  <p>I decided to attach here an extra <code>Sub ConvEN()</code>, allowing to represent a number in engineering notation with SI prefixes from "p" to "T":</p>
  
  <pre><code>N = .0000456789
  ConvNSN N, M, P
  M = Round(M, 2)
  ConvEN M, P, R, S
  MsgBox R &amp; " " &amp; S &amp; "Units"
  
  Sub ConvNSN(N, M, P)
      Dim A
      If N = 0 Then
          P = 0
          M = 0
          Exit Sub
      End If
      A = Abs(N)
      If A &lt; 1 Then
          P = Int(Log(A) / Log(10))
      Else
          P = Int(Log(A) / Log(10) * (2 + Log(.1) / Log(10)))
      End If
      M = N / 10 ^ P
  End Sub
  
  Sub ConvEN(M, P, R, S)
      DIM Q, P3
      Q = int(P / 3)
      P3 = Q * 3
      If Q &gt;= -4 And Q &lt;= 4 Then
          S = Array("p", "n", ChrW(&amp;H03BC), "m", "", "k", "M", "G", "T")(Q + 4)
      Else
          S = "e" &amp; P3 &amp; " "
      End If
      R = M * 10 ^ (P - P3)
  End Sub
  </code></pre>