|
-
May 23rd, 2012, 04:38 PM
#1
Thread Starter
Addicted Member
[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?
-
May 23rd, 2012, 04:56 PM
#2
Re: Converting text to double
VB6 Doubles don't give you 15 digits of precision.
-
May 23rd, 2012, 05:32 PM
#3
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.
-
May 23rd, 2012, 05:42 PM
#4
Thread Starter
Addicted Member
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
-
May 23rd, 2012, 05:46 PM
#5
Thread Starter
Addicted Member
Re: Converting text to double
 Originally Posted by Lenggries
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.
-
May 23rd, 2012, 05:48 PM
#6
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.
-
May 23rd, 2012, 05:52 PM
#7
Re: Converting text to double
 Originally Posted by Gymbo
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.
-
May 23rd, 2012, 05:55 PM
#8
Re: Converting text to double
 Originally Posted by Gymbo
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.
-
May 23rd, 2012, 05:57 PM
#9
Re: Converting text to double
 Originally Posted by jcis
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.
-
May 23rd, 2012, 05:59 PM
#10
Thread Starter
Addicted Member
Re: Converting text to double
 Originally Posted by jcis
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.
-
May 23rd, 2012, 06:01 PM
#11
Re: Converting text to double
 Originally Posted by jmsrickland
Not so. I got the correct answer - see post #3
x - y should be 0, not -2.88657986402541E-15
-
May 23rd, 2012, 06:03 PM
#12
Thread Starter
Addicted Member
Re: Converting text to double
I just tried declaring z as a Double, got the same results, -2.88657986402541E-15
-
May 23rd, 2012, 06:09 PM
#13
Thread Starter
Addicted Member
Re: Converting text to double
 Originally Posted by jmsrickland
Then why did you ask what am I doing wrong?
Because the answer should be zero!
-
May 23rd, 2012, 06:12 PM
#14
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.
-
May 23rd, 2012, 07:35 PM
#15
Re: Converting text to double
 Originally Posted by Gymbo
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.
-
May 23rd, 2012, 07:55 PM
#16
Re: Converting text to double
Very strange, I have no idea why this may be happening.
-
May 23rd, 2012, 08:49 PM
#17
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:
Last edited by jcis; May 23rd, 2012 at 09:25 PM.
-
May 23rd, 2012, 09:08 PM
#18
Thread Starter
Addicted Member
Re: Converting text to double
 Originally Posted by jmsrickland
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).
-
May 23rd, 2012, 09:26 PM
#19
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.
-
May 24th, 2012, 09:49 AM
#20
Thread Starter
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|