473,735 Members | 10,006 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.189999580383 3.

Anyone know how to avoid this?

Regards Totto
May 5 '06 #1
7 2706
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**********@I ntellifield.no> wrote in message
news:%2******** ********@TK2MSF TNGP05.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.189999580383 3.

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******** ********@TK2MSF TNGP04.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**********@I ntellifield.no> wrote in message
news:%2******** ********@TK2MSF TNGP05.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.189999580383 3.

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#
redistributeabl e).

"Tor Aadnevik" <to**********@I ntellifield.no> wrote in message
news:eK******** ******@TK2MSFTN GP04.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******** ********@TK2MSF TNGP04.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**********@I ntellifield.no> wrote in message
news:%2******** ********@TK2MSF TNGP05.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.189999580383 3.

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.189999580383 3.

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.co m> wrote in message
news:u9******** ******@TK2MSFTN GP03.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.189999580383 3.

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.co m> wrote in message
news:u9******** ******@TK2MSFTN GP03.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.189999580383 3.

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
13924
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 something like: char chars = "Hello!"; byte bytes = (byte) chars;
2
14470
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 sequence in the XML document. am not able to locate such elements using an XPath expression. I trie the following: //*
2
15865
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 IBM/370 float values into IEEE 754 'float' values that I can use directly with any modern C environment (that uses IEEE 754). I can handle endian conversion myself (the files are stored as big endian, and I'll process them on a little endian...
12
3186
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
35937
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 problems elsewhere in another part of the system where that figure was used for some calculation and some eventual truncation led to the system going haywire. So my question is, given this code: int main() { float f = 59.89F;
10
1992
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 txtradius that I enter a radius into. I then have a lable that I want the surface area to be displayed in called lblsurface
25
4387
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 opened it in Visual Studio 2005. I've cleared up most errors, but have one that beats the heck out of me. I have a page: sales/Item.aspx that has several datagrids in it.
2
5116
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 amount = 126.60; double interestRate = .075; double interest = amount * interestRate;
7
4787
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 precision floating point types. The number of decimal digits guaranteed to be correct on my implementation is 6. (i.e numeric_limits < float >::digits10 = 6 ) If I'm reading the IEEE standard, I'd could paraphrase the issue
0
8962
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8785
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9463
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9251
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9200
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8201
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4822
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2739
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2188
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.