473,382 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Converting from Single to double error

Hi,
I have a problem converting values from Single to double.
eg. When the Single value 12.19 is converted to double, the result is
12.1899995803833.

Anyone know how to avoid this?

Regards Totto
May 5 '06 #1
7 2668
There is the possibility of small amounts of error when dealing with
singles/doubles as they are estimations not exact values

There are really 3 options ..

1) use Math.Round to convert the value
2) use a lossless number format (i.e. BigDecimal) which operates on strings
or a fraction class
http://geekswithblogs.net/gyoung/arc.../01/76869.aspx includes
links to both
3) it seems like you might be doing monetary calculations here ... a common
pattern is to not treat things as "Dollars" but to put the value in an
integer representing pennies ... this is much faster for calculations as
well.

"Tor Aadnevik" <to**********@Intellifield.no> wrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
Hi,
I have a problem converting values from Single to double.
eg. When the Single value 12.19 is converted to double, the result is
12.1899995803833.

Anyone know how to avoid this?

Regards Totto

May 5 '06 #2
Hi Greg
Tanks for your answere.
Both the Single and the Double values are types that belongs to systems that
are out of my control.
But mybe if I use your option number 3 and multiply the Single value with a
big number so its without decimals and then convert it to double. I will
give it a test!

Tor

"Greg Young" <Dr*************@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
There is the possibility of small amounts of error when dealing with
singles/doubles as they are estimations not exact values

There are really 3 options ..

1) use Math.Round to convert the value
2) use a lossless number format (i.e. BigDecimal) which operates on
strings or a fraction class
http://geekswithblogs.net/gyoung/arc.../01/76869.aspx includes
links to both
3) it seems like you might be doing monetary calculations here ... a
common pattern is to not treat things as "Dollars" but to put the value in
an integer representing pennies ... this is much faster for calculations
as well.

"Tor Aadnevik" <to**********@Intellifield.no> wrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
Hi,
I have a problem converting values from Single to double.
eg. When the Single value 12.19 is converted to double, the result is
12.1899995803833.

Anyone know how to avoid this?

Regards Totto


May 5 '06 #3
1) I meant math.round after the operation to the proper precision
2) BigDecimal is another type :) (you can find it in the J#
redistributeable).

"Tor Aadnevik" <to**********@Intellifield.no> wrote in message
news:eK**************@TK2MSFTNGP04.phx.gbl...
Hi Greg
Tanks for your answere.
Both the Single and the Double values are types that belongs to systems
that are out of my control.
But mybe if I use your option number 3 and multiply the Single value with
a big number so its without decimals and then convert it to double. I will
give it a test!

Tor

"Greg Young" <Dr*************@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
There is the possibility of small amounts of error when dealing with
singles/doubles as they are estimations not exact values

There are really 3 options ..

1) use Math.Round to convert the value
2) use a lossless number format (i.e. BigDecimal) which operates on
strings or a fraction class
http://geekswithblogs.net/gyoung/arc.../01/76869.aspx includes
links to both
3) it seems like you might be doing monetary calculations here ... a
common pattern is to not treat things as "Dollars" but to put the value
in an integer representing pennies ... this is much faster for
calculations as well.

"Tor Aadnevik" <to**********@Intellifield.no> wrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
Hi,
I have a problem converting values from Single to double.
eg. When the Single value 12.19 is converted to double, the result is
12.1899995803833.

Anyone know how to avoid this?

Regards Totto



May 5 '06 #4
The Single value is not exactly 12.19 to begin with, but the difference
is so small that the value gets rounded to 12.19 when displayed.

When you convert it to a Double, the difference between the actual value
and 12.19 remains the same (or changes very little), but as Double has
higher precision the difference becomes visible.

So the problem is not that the value changes, but that you suddenly see
how low precision the original value had.

Tor Aadnevik wrote:
Hi,
I have a problem converting values from Single to double.
eg. When the Single value 12.19 is converted to double, the result is
12.1899995803833.

Anyone know how to avoid this?

Regards Totto

May 5 '06 #5
Single s = 1.9F;

double d = (double)s;

It has to do with the way floating point numbers are stored, they are
approximated. It is not that you are just not seeing precision on the
single. see http://steve.hollasch.net/cgindex/coding/ieeefloat.html

Cheers,

Greg Young

"Göran Andersson" <gu***@guffa.com> wrote in message
news:u9**************@TK2MSFTNGP03.phx.gbl...
The Single value is not exactly 12.19 to begin with, but the difference is
so small that the value gets rounded to 12.19 when displayed.

When you convert it to a Double, the difference between the actual value
and 12.19 remains the same (or changes very little), but as Double has
higher precision the difference becomes visible.

So the problem is not that the value changes, but that you suddenly see
how low precision the original value had.

Tor Aadnevik wrote:
Hi,
I have a problem converting values from Single to double.
eg. When the Single value 12.19 is converted to double, the result is
12.1899995803833.

Anyone know how to avoid this?

Regards Totto

May 5 '06 #6
Greg Young wrote:
It has to do with the way floating point numbers are stored, they are
approximated. It is not that you are just not seeing precision on the


*All* doubles are not approximated, it's just that some floating point
values cannot be represented accurately in binary. It the same reason
that some numbers cannot be accurately represented in the decimal
number system like PI or the square root of 2 or the fraction 1/3.

May 5 '06 #7
I did some testing, and I couldn't find any number where converting the
number from float to double and then back to float would not give the
exact same number as before converting.

Wouldn't that mean that there is no loss of data at all when converting
from float to double?

Greg Young wrote:
Single s = 1.9F;

double d = (double)s;

It has to do with the way floating point numbers are stored, they are
approximated. It is not that you are just not seeing precision on the
single. see http://steve.hollasch.net/cgindex/coding/ieeefloat.html

Cheers,

Greg Young

"Göran Andersson" <gu***@guffa.com> wrote in message
news:u9**************@TK2MSFTNGP03.phx.gbl...
The Single value is not exactly 12.19 to begin with, but the difference is
so small that the value gets rounded to 12.19 when displayed.

When you convert it to a Double, the difference between the actual value
and 12.19 remains the same (or changes very little), but as Double has
higher precision the difference becomes visible.

So the problem is not that the value changes, but that you suddenly see
how low precision the original value had.

Tor Aadnevik wrote:
Hi,
I have a problem converting values from Single to double.
eg. When the Single value 12.19 is converted to double, the result is
12.1899995803833.

Anyone know how to avoid this?

Regards Totto


May 5 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: matt melton | last post by:
Hi there, I am trying to write a method that accepts an array of any primitive type and will return the same array without copying memory as an array of bytes. ie. I'd like to be able to do...
2
by: girish | last post by:
In my XML document, some node attributes data contains both single quot and double quote characters, such as <input msg="Hello "World", What's up"/>. The double quotes are in form of escape...
2
by: Benjamin Rutt | last post by:
Does anyone have C code laying around to do this? I have to read in some binary data files that contains some 4-byte IBM/370 floating point values. I would like a function to convert 4-byte...
12
by: Frederik Vanderhaeghe | last post by:
Hi, I have a problem converting text to a double. Why doesn't the code work: If Not (txtdocbedrag.Text = "") Then Select Case ddlBedrag.SelectedIndex Case 0 Case 1
116
by: Dilip | last post by:
Recently in our code, I ran into a situation where were stuffing a float inside a double. The precision was extended automatically because of that. To make a long story short, this caused...
10
by: Ron | last post by:
I want to calculate the surface area of a sphere from an inputed radius with option strict on. I guess I am not converting something correctly. Here is what I am doing: I have a textbox...
25
by: Blasting Cap | last post by:
I keep getting errors that pop up when I am trying to convert an application from dotnet framework 1.1 to framework 2.0. The old project was saved in sourcesafe from Visual Studio 2003, and I have...
2
by: clintonb | last post by:
Victor said: The double value that I'm trying to convert to GCSMoney (which is implemented as cents) was produced by multiplying a dollar amount by an interest rate to get interest. double...
7
by: ma740988 | last post by:
Consider the equation (flight dynamics stuff): Yaw (Degrees) = Azimuth Angle(Radians) * 180 (Degrees) / 3.1415926535897932384626433832795 (Radians) There's a valid reason to use single...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.