Heaven Posted April 17, 2003 Posted April 17, 2003 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. Quote
Moderators Robby Posted April 17, 2003 Moderators Posted April 17, 2003 if you set 'Option Strict On' you cannot do this... s -=1000000000000 'subtract 1 trillion form the value Quote Visit...Bassic Software
*Gurus* Derek Stone Posted April 17, 2003 *Gurus* Posted April 17, 2003 You take a 64-bit integer value, convert it to a string, set a 32-bit equal to it (the string value) and then proceed to perform subtraction on a string value... You might, just might, want to rethink that very poorly written code. Quote Posting Guidelines
Heaven Posted April 18, 2003 Author Posted April 18, 2003 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.) Quote
Moderators Robby Posted April 18, 2003 Moderators Posted April 18, 2003 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) Quote Visit...Bassic Software
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.