Convert float to double and back - easiest way without a loss ofprecision?

  • Thread starter Thread starter Paul
  • Start date Start date
P

Paul

float f = 161.71000;
double d = f; // d = 161.71000671386719

I have seen the discussions of why this happens. But unfortunately
when this is converted back to a float again it becomes 161.71001
which is unacceptable to my client. The conversion must be done due
to an external library I must use.

What is the easiest way to convert this to try to retain the original
accuracy? If the conversion to the double could just contain
something like 161.7100049 or less I would be fine - surely that sort
of precision is available with a double?

I was thinking something like Convert.ToDouble(f.ToString()+'0') which
I know is slow and inelegant, but I cannot be sure the number will
contain a decimal and testing to see if it does seems a real
tangent.

Surely this is such a classic programming problem that someone will
have solved it in a more elegant way?

Any advice would be very much appreciated!

cheers,
Paul.
 
Paul said:
float f = 161.71000;
double d = f; // d = 161.71000671386719

I have seen the discussions of why this happens. But unfortunately
when this is converted back to a float again it becomes 161.71001
which is unacceptable to my client. The conversion must be done due
to an external library I must use.

Can't you replace all floats with doubles in your code ?
 
Paul said:
float f = 161.71000;
double d = f; // d = 161.71000671386719

I have seen the discussions of why this happens. But unfortunately
when this is converted back to a float again it becomes 161.71001
which is unacceptable to my client. The conversion must be done due
to an external library I must use.

What is the easiest way to convert this to try to retain the original
accuracy? If the conversion to the double could just contain
something like 161.7100049 or less I would be fine - surely that sort
of precision is available with a double?

I was thinking something like Convert.ToDouble(f.ToString()+'0') which
I know is slow and inelegant, but I cannot be sure the number will
contain a decimal and testing to see if it does seems a real
tangent.

Surely this is such a classic programming problem that someone will
have solved it in a more elegant way?

Any advice would be very much appreciated!

cheers,
Paul.


For a wonderful explanation, please read Jon Skeet's artical here:
http://www.yoda.arachsys.com/csharp/floatingpoint.html
 
Thanks for your comments. Like I say, I understand why it is
happening and I am not fighting it.

I just have a float that my 3rd party data layer requires as a double
before it saves it to the database as a float type.

Therefore I would really like to find the simplest way to convert
161.71000 as a float to 161.7100049 or less as a double so that when
this is converted back, I do not get the 161.71001 in the final
float.

However because this becomes 161.71000671386719 it does become
161.71001 in the database.

cheers,
Paul.
 
Back
Top