Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I'm using visual studio.net pro, and this is occuring when I'm using VB

 

Hope some one has the answer to this one.

I just want to know the Why this happens.

 

Here's my code:

 

Dim l as Long

Dim S as Single

 

l = Long.MaxValue 'set the long variable to its maximum value

Debug.WriteLine(l) 'display the results to output (which are 922337203684775807)

 

s= Convert.ToString(l) 'convert the long to a string

s -=1000000000000 'subtract 1 trillion form the value

l= Convert.toInt64(s) 'convert the answer back to a long

 

Debug.WriteLine(l) 'display the results to ourput (which are 9223370937343148032)

 

'end of code

 

now as you can see you don't get what you would expect. a single should have just rounded off at 7 places and returned the answer of 9223371 + a lot of trailing zeros.

 

I've looked at the .net library and the only thing I can find is that the results would be "inaccurate" duh.... I am trying to find out why . any ideas??? i'm sure it has something to do with overflow and possibly PositiveInfinity.

Posted

re; data conversion

 

I Know that it is poorly written code. as a matter of a fact.. it's not a good to convert from a long back to a single. but.... that's not what I'm trying to find out. I'm trying to find out what is happening to the result. why is it coming back as inaccurately as it is. I think (and I may be wrong) that it might have something to do with when the non integer number is converted to binary from base 10 and then back to decimal for display. i'm wondering if maybe the fact that some base 10 non integer numbers, when converted to binary act more like an irrational number and go to positive infinity (like as an example pi. or 0.2 ) if anyone knows what is happening with the numbers, I'd sure like to know (it's turned into a vendeta for me.)

  • Moderators
Posted

One thing I don't understand is why you declared S as Single, then you Convert it to String.

Did you mean to declare S as String ?

 

Is so, then this will work...

Dim l As Long
Dim S As String

l = Long.MaxValue
S = l.ToString
S = (Convert.ToInt64(S) - 1000000000000).ToString
l = Convert.ToInt64(S)

MessageBox.Show(Long.MaxValue & ControlChars.NewLine & l.ToString)

Visit...Bassic Software

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...