WizardsSleeve
WizardsSleeve

Reputation: 195

vb.net mantissa and exponent calculation from double

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

Upvotes: 3

Views: 2143

Answers (2)

bangquack
bangquack

Reputation: 1

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

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1502256

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.

Upvotes: 2

Related Questions