CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Dec 2009
    Posts
    1

    Incerementing a string "single" value

    I'm currently using a string as single value

    say:

    "
    Dim test As String
    test = 0
    "


    and with a command button:

    "
    test = test + 0.1
    "


    it seems to work fine on my computer and on some others
    when i used single, sometimes it turned the value test to 0.5000001

    but if i declare test as a string it all works out fine
    can anyone explain to me how this is possible ?

  2. #2
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: Incerementing a string "single" value

    antlion. I have moved your post to a new thread, as you have originally replied to an eight year old thread
    Last edited by HanneSThEGreaT; December 24th, 2009 at 03:09 AM.

  3. #3
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: Incerementing a string "single" value

    It is not usual to compute numbers with string variables, although it works as soon as you have initialized the variable with temp = 0.
    If you'd have left temp empty, temp = temp + 0.1 will produce a type mismatch error.

    When you work with the data type Single, due to the method how a computer is processing fractional data in binary digits, some rounding error occurs.
    This error does not appear when the data type Double is used.

    When you use strings, VBs automatic type conversion takes place. Before a string can be used in a numeric expression it will be converted to a numeric data type and the numeric result will be converted bnack to a string before it is stored again in the string variable.
    As it seems VB converts both expressions (temp and 0.1) into Doubles before computing, so as the rounding error of the Singles does not appear.

  4. #4
    Join Date
    Dec 2001
    Posts
    6,332

    Re: Incerementing a string "single" value

    It is called a floating point error. It is inherent in the way the processor handles the numeric data. This also applies to the Double data type. However, the Currency data type will give you a few decimal places, but without any floating point error.
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

  5. #5
    Join Date
    Mar 2002
    Location
    Croatia
    Posts
    275

    Re: Incerementing a string "single" value

    Quote Originally Posted by antlion View Post
    I'm currently using a string as single value

    say:

    "
    Dim test As String
    test = 0
    "


    and with a command button:

    "
    test = test + 0.1
    "


    it seems to work fine on my computer and on some others
    when i used single, sometimes it turned the value test to 0.5000001

    but if i declare test as a string it all works out fine
    can anyone explain to me how this is possible ?
    Did you tried something like this?
    Dim test As String
    test = CStr(0)
    and with a command button:"
    test = CStr( CSng(test) + 0.1 )
    I didn't run the code, but it shoud work with explicit type conversion.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured