Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Number changes value when converting from single to double 1

Status
Not open for further replies.

Motor11

Technical User
Jul 30, 2002
60
US
I am working on a project where values of up to 10 decimals points are significant.

I am loading values from a binary file, so I am forced to read them in as singles. They are stored in a temporary variable which, for the sake of simplicity, I am calling "mysingle". If certain requirements are fulfilled, that number is passed to a data structure of doubles. Simple enough, right?

However, my value gets changed when it goes from single to double. I wrote some simple code to illustrate this:

Private Sub Command1_Click()
Dim mysingle As Single
Dim mydouble As Double
mysingle = -0.120119
mydouble = mysingle
Debug.Print mydouble
End Sub

If you run this code, you can see that mydouble gets the value of: -0.120118997991085

I've also tried: mydouble=cdbl(mysingle) the results are the same.

Any ideas?

Thanks in advance,
Ryan
 
This seems to work but it's hardly elegant

mydouble = Val(Format(mysingle, "0.000000000"))
 
Thanks Golom and CCLINT,

Both solutions worked. I've gone with the CDec method and am now rolling again on my project.

One aside, what is up with this behavior? I imagine it has something to do with the way data is store internally. Can anyone elaborate, just for interest's sake?

Thanks again,
Ryan
 
The fractional part of a value cannot be represented exactly in binary, therefore what you get is the closest approximation based on the number bytes available to represent the value. When you convert to double, you increase the available number of bytes, thus getting a more accurate value. In one sense, you can consider it the opposite of rounding.

Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Thanks for answering CajunCenturion.
I wasn't back here in until now.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top