Results 1 to 20 of 20

Thread: [RESOLVED] Converting text to double

  1. #1

    Thread Starter
    Addicted Member Gymbo's Avatar
    Join Date
    Jan 2006
    Location
    The Oregon Coast
    Posts
    213

    Resolved [RESOLVED] Converting text to double

    Hi,

    I have a textbox control that contains the text "1.73205080756888" (sqrt(3)), I want to convert that to a double such that it's equal to the square root of 3 to 15 places.

    Code:
    dim x as double, y as double, z as variant
    
    z = sqrt(3)
    textbox.text = z
    x = sqrt(3)
    y = Cdbl(textbox.text)
    debug.print x - y
    The result is -2.88657986402541E-15 not 0

    What am I doing wrong?

  2. #2
    Hyperactive Member Lenggries's Avatar
    Join Date
    Sep 2009
    Posts
    353

    Re: Converting text to double

    VB6 Doubles don't give you 15 digits of precision.

  3. #3
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Converting text to double

    Code:
    z = 3 ^ (0.5)                     ' = 1.73205080756888 
    TextBox.Text = z                  ' = "1.73205080756888"
    x = 3 ^ (0.5)                     ' = 1.73205080756888
    y = CDbl(TextBox.Text)            ' = 1.73205080756888
    MsgBox x - y                      ' = -2.88657986402541E-15 
    TextBox.Text = x - y              ' = "-2.88657986402541E-15"
    Works for me. However, you would think it should be 0 because

    x = 1.73205080756888
    y = 1.73205080756888
    x - y = 1.73205080756888 - 1.73205080756888 = 0
    Last edited by jmsrickland; May 23rd, 2012 at 05:38 PM.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  4. #4

    Thread Starter
    Addicted Member Gymbo's Avatar
    Join Date
    Jan 2006
    Location
    The Oregon Coast
    Posts
    213

    Re: Converting text to double

    This is the code I used to test this situation.

    Code:
    Private Sub testtxt()
    Dim x As Double, y As Double, z
    z = Sqr(3)
    x = Sqr(3)
    txtXreg.text = z
    y = CDbl(txtXreg.text)
    Debug.Print x - y
    Stop
    End Sub
    And this is the results I got:

    -2.88657986402541E-15

  5. #5

    Thread Starter
    Addicted Member Gymbo's Avatar
    Join Date
    Jan 2006
    Location
    The Oregon Coast
    Posts
    213

    Re: Converting text to double

    Quote Originally Posted by Lenggries View Post
    VB6 Doubles don't give you 15 digits of precision.
    How many digits does it give? More than 14, less than 16; I can't find an answer to that in the documentation.

  6. #6
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: Converting text to double

    You're losing precision in the implicit conversion Double->Variant->Text->Double. Specially the conversion to Text is the reason for the difference you get.

    Also: is this VB6 or VBA? in VB6 it's sqr() not sqrt()
    Last edited by jcis; May 23rd, 2012 at 05:53 PM.

  7. #7
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Converting text to double

    Quote Originally Posted by Gymbo View Post
    How many digits does it give? More than 14, less than 16; I can't find an answer to that in the documentation.
    Looks like 14


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  8. #8
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Converting text to double

    Quote Originally Posted by Gymbo View Post
    This is the code I used to test this situation.

    Code:
    Private Sub testtxt()
    Dim x As Double, y As Double, z
    z = Sqr(3)
    x = Sqr(3)
    txtXreg.text = z
    y = CDbl(txtXreg.text)
    Debug.Print x - y
    Stop
    End Sub
    And this is the results I got:

    -2.88657986402541E-15
    Then why did you ask what am I doing wrong?


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  9. #9
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Converting text to double

    Quote Originally Posted by jcis View Post
    You're losing precision in the implicit conversion Double->Variant->Text->Double. Specially the conversion to Text is the reason for the difference you get.

    Also: is this VB6 or VBA? in VB6 it's sqr() not sqrt()
    Not so. I got the correct answer - see post #3


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  10. #10

    Thread Starter
    Addicted Member Gymbo's Avatar
    Join Date
    Jan 2006
    Location
    The Oregon Coast
    Posts
    213

    Re: Converting text to double

    Quote Originally Posted by jcis View Post
    You're losing precision in the implicit conversion Double->Variant->Text->Double. Specially the conversion to Text is the reason for the difference you get.

    Also: is this VB6 or VBA? in VB6 it's sqr() not sqrt()
    I thought Variant would work the best since the value in the textbox could be almost anything.

    I tried it as a String with the same results.

    VB6, the sqrt() was a typo.

  11. #11
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: Converting text to double

    Quote Originally Posted by jmsrickland View Post
    Not so. I got the correct answer - see post #3
    x - y should be 0, not -2.88657986402541E-15

  12. #12

    Thread Starter
    Addicted Member Gymbo's Avatar
    Join Date
    Jan 2006
    Location
    The Oregon Coast
    Posts
    213

    Re: Converting text to double

    I just tried declaring z as a Double, got the same results, -2.88657986402541E-15

  13. #13

    Thread Starter
    Addicted Member Gymbo's Avatar
    Join Date
    Jan 2006
    Location
    The Oregon Coast
    Posts
    213

    Re: Converting text to double

    Quote Originally Posted by jmsrickland View Post
    Then why did you ask what am I doing wrong?
    Because the answer should be zero!

  14. #14
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: Converting text to double

    VB & VBA use the same scalar types. See Data Type Summary, also INFO: Visual Basic and Arithmetic Precision

    All of the numeric types are binary numbers, including even Decimal (VT_DECIMAL):
    Decimal variables are stored as 96-bit (12-byte) signed integers scaled by a variable power of 10
    12 bytes of signed integer and 4 bytes of signed integer "power of 10" scale factor.

    So since VB doesn't use decimal digits (even for Decimal data!) the number of "digits of precision" is always an approximation.

  15. #15
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Converting text to double

    Quote Originally Posted by Gymbo View Post
    Because the answer should be zero!
    Oh, yeah. I missed that in your original post. I read it as you were getting 0 instead of -2.88657986402541E-15. You know, what is strange when I did a test the very first time I got an answer of 0 and I thought then that it was supposed to be 0 (which it should be) then I made some changes and then I got that very long number.

    The below code produces 0

    Code:
    Private Sub Command1_Click()
    Dim x As Double, y As Double, z As Variant
    
    z = 1.73205080756888  ' =  3 ^ (0.5)  or sqr(3)
    TextBox.Text = z
    x = 1.73205080756888  ' = 3 ^ (0.5))  or sqr(3)
    y = CDbl(TextBox.Text)
    MsgBox x - y               ' answer is 0
    
    TextBox.Text = x - y    ' answer is 0
    End Sub
    but the below code yields -2.88657986402541E-15

    Code:
    Private Sub Command1_Click()
    Dim x As Double, y As Double, z As Variant
    
    z =  3 ^ (0.5)
    TextBox.Text = z
    x = 3 ^ (0.5) 
    y = CDbl(TextBox.Text)
    MsgBox x - y 
    
    TextBox.Text = x - y
    End Sub
    Last edited by jmsrickland; May 23rd, 2012 at 07:39 PM.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  16. #16
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    Re: Converting text to double

    Very strange, I have no idea why this may be happening.

  17. #17
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: Converting text to double

    If we you do this:
    Code:
    Dim x as Double
    
     x = Sqr(3)
    The value inside x is 1.7320508075688772 but if you show this value either with MsgBox or adding it to a TextBox or using the Inmediate Window or when you place the mouse pointer over x variable or whatever else you want that will convert this to Text, then the value you'll see is this: 1.73205080756888

    So, no matter if it's an implicit or explicit conversion to text, when you try to see the value inside the Double variable you're already making an implicit conversion to text, the value you see is not the real value inside the variable.So when you convert x to String last 3 decimal positions are rounded as 8, see:

    1.7320508075688772
    1.73205080756888

    And that is the reason for that value, here it's more clear:
    Code:
    MsgBox Sqr(3) - 1.73205080756888 'Result = -2,88657986402541E-15 
    MsgBox 1.7320508075688772 - 1.73205080756888 = 'Result =  -2,88657986402541E-15 
    Btw, +/- 2.88657986402541E-15 is a very small difference, insignificant for most Applications: ~= +/- 0,000000000000003 (aprox)

    In the OP first post, all this happen when he converts to text:
    Code:
    textbox.text = z
    Last edited by jcis; May 23rd, 2012 at 09:25 PM.

  18. #18

    Thread Starter
    Addicted Member Gymbo's Avatar
    Join Date
    Jan 2006
    Location
    The Oregon Coast
    Posts
    213

    Re: Converting text to double

    Quote Originally Posted by jmsrickland View Post
    The below code produces 0

    Code:
    Private Sub Command1_Click()
    Dim x As Double, y As Double, z As Variant
    
    z = 1.73205080756888  ' =  3 ^ (0.5)  or sqr(3)
    TextBox.Text = z
    x = 1.73205080756888  ' = 3 ^ (0.5))  or sqr(3)
    y = CDbl(TextBox.Text)
    MsgBox x - y               ' answer is 0
    
    TextBox.Text = x - y    ' answer is 0
    End Sub
    but the below code yields -2.88657986402541E-15

    Code:
    Private Sub Command1_Click()
    Dim x As Double, y As Double, z As Variant
    
    z =  3 ^ (0.5)
    TextBox.Text = z
    x = 3 ^ (0.5) 
    y = CDbl(TextBox.Text)
    MsgBox x - y 
    
    TextBox.Text = x - y
    End Sub
    In your first example did you just enter the 15 digit numbers, or did you calculate them?

    Of course if you entered the numbers it would produce a zero (0).

  19. #19
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Converting text to double

    jcis,

    Very nice explanation. Now I see what is going on where before I had no idea.

    Gymbo,

    Of course if you entered the numbers it would produce a zero (0).

    Yeah, but I didn't know that. The reason it didn't make any sence to me was because when I did it the 2nd time I compared each step with what I entered explicitly the 1st time and they were exactly the same except when it got to the answer. Really blew my mind.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  20. #20

    Thread Starter
    Addicted Member Gymbo's Avatar
    Join Date
    Jan 2006
    Location
    The Oregon Coast
    Posts
    213

    Re: Converting text to double

    fwiw

    Convert x and y to Strings [Cstr()], then convert them both back to Doubles [Cdbl()] seems to have solved my dilemma.

    Thank you, everyone for your input.

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