3

Can anyone offer any advice on how to get the mantissa and exponent from a double in VB.net? I know I can do a string parse and some conversion to ints but I wondered if anyone had a mathematical equivalent formula that would allow me to do this?

Many thanks

WizardsSleeve
  • 185
  • 2
  • 4
  • 11

2 Answers2

2

Do you want to get the "native" mantissa and exponent within the IEEE-754 value? That's actually fairly easy: use BitConverter.DoubleToInt64Bits to get the value as an integer (which is easier to perform bit operations on) and then see my article on .NET binary floating point types for which bits are where.

I have some C# code which extracts the various parts in order to convert it to a precise decimal representation - you could convert the relevant bits of that into VB reasonably easily.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
0

You should try this:

Public Function DeclString(ByVal dDegrees As Double) As String
   Dim Flag As String
   Dim ddecimal As Double
   Dim iDegrees As Integer
   If dDegrees < 0 Then Flag = "S" Else Flag = "N"
   iDegrees = Int(Abs(dDegrees))
   ddecimal = (Abs(dDegrees) - iDegrees) * 60 ' + 0.5
   If ddecimal > 59.5 Then iDegrees = iDegrees + 1: ddecimal = 0
   DeclString = Format$(iDegrees, "00") + Flag + Format$(ddecimal, "00")
End Function
Cydrick Trudel
  • 9,957
  • 8
  • 41
  • 63
bangquack
  • 1
  • 1