473,666 Members | 2,048 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

casting decimal to float to double error

Hi all,
the following c# example baffles me. my understanding of
the managed code of visual .net is that all casts like
this would be safe, but this example seems to contradict
this notion. if you look at the debugger info, the double
dou is half filled w/ garbage bits, instead of those bits
being zeroed out appropriately.

using System;
namespace decimaltodouble {
class Class1 {
[STAThread]
static void Main(string[] args) {
decimal dec = 0.4695M;
float fl = (float) dec;
double dou = (double) fl;
if (dou != 0.4695)
Console.Write(" bad cast, garbage bits");
Console.Read();
}
}
}
Nov 15 '05 #1
8 13000
> Hi all,
the following c# example baffles me. my understanding of
the managed code of visual .net is that all casts like
this would be safe, but this example seems to contradict
this notion. if you look at the debugger info, the double
dou is half filled w/ garbage bits, instead of those bits
being zeroed out appropriately.


This happens because you are casting a number between types with different
sizes. If you were to read about how you represent numbers in IEEE 32 bit
floating point format, IEEE 64 floating point format and 96 bit decimal
format, you wont find it strange at all.

Basically, you have a finite number of combinations (because there is a
finite number of bits) and an infinite number of, well, Real numbers. So,
you have to "aproxiamte " the number you really want to the closest number
you can represent. And you have to admit, 0.4695000052452 09 is pretty damn
close to 0.4695 (it's only 0.0000001% larger).

If you need any more detail, feel free to ask, or read it up on the web.
-JG
Nov 15 '05 #2
Quinn Kirsch wrote:
Hi all,
the following c# example baffles me. my understanding of
the managed code of visual .net is that all casts like
this would be safe, but this example seems to contradict
this notion. if you look at the debugger info, the double
dou is half filled w/ garbage bits, instead of those bits
being zeroed out appropriately.

using System;
namespace decimaltodouble {
class Class1 {
[STAThread]
static void Main(string[] args) {
decimal dec = 0.4695M;
float fl = (float) dec;
double dou = (double) fl;
if (dou != 0.4695)
Console.Write(" bad cast, garbage bits");
Console.Read();
}
}
}


The bits actually are zeroed out, but that's the basis of your problem.

The problem boils down to the fact that:

((double) (float) 0.4695) != ((double) 0.4695)

The decimal type in your sample program is unnecessary to the problem.

Take a look at the output of this program:

=============== =============== =============== =======
using System;

namespace ConsoleApplicat ion1 {
public class MainClass {

static void Main(string[] args) {
Console.WriteLi ne( " 0.4695f is {0:x}",
BitConverter.To UInt32( BitConverter.Ge tBytes( 0.4695f), 0));
Console.WriteLi ne( "(double) 0.4695f is {0:x}",
BitConverter.To UInt64( BitConverter.Ge tBytes( ((double)
0.4695f)), 0));
Console.WriteLi ne( " 0.4695d is {0:x}",
BitConverter.To UInt64( BitConverter.Ge tBytes( 0.4695d), 0));
}
}
}
=============== =============== =============== =======

The output I get is:

=============== =============== =============== =======
0.4695f is 3ef0624e
(double) 0.4695f is 3fde0c49c000000 0
0.4695d is 3fde0c49ba5e353 f
=============== =============== =============== =======

--
mikeb

Nov 15 '05 #3
Quinn Kirsch <an*******@disc ussions.microso ft.com> wrote:
the following c# example baffles me. my understanding of
the managed code of visual .net is that all casts like
this would be safe, but this example seems to contradict
this notion. if you look at the debugger info, the double
dou is half filled w/ garbage bits, instead of those bits
being zeroed out appropriately.


Basically you're casting the decimal to a float, which gives the
nearest float to 0.4695. You're then casting the float to double, which
will always preserve the value (as every float is exactly representable
in double) - but that's not necessarily the nearest *double* to 0.4695,
which is the thing you're next testing.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #4
I understand why these problems have all occurred in the
past with our friends C and C++, but I thought that these
type of problems were supposed to be corrected Java or
C#. I thought these languages were made to get rid
mistakes like mispointed pointers and uninitialized
memory, but I guess that they did not get it all.
-----Original Message-----
Hi all,
the following c# example baffles me. my understanding of the managed code of visual .net is that all casts like
this would be safe, but this example seems to contradict
this notion. if you look at the debugger info, the double dou is half filled w/ garbage bits, instead of those bits being zeroed out appropriately.
This happens because you are casting a number between

types with differentsizes. If you were to read about how you represent numbers in IEEE 32 bitfloating point format, IEEE 64 floating point format and 96 bit decimalformat, you wont find it strange at all.

Basically, you have a finite number of combinations (because there is afinite number of bits) and an infinite number of, well, Real numbers. So,you have to "aproxiamte " the number you really want to the closest numberyou can represent. And you have to admit, 0.4695000052452 09 is pretty damnclose to 0.4695 (it's only 0.0000001% larger).

If you need any more detail, feel free to ask, or read it up on the web.-JG
.

Nov 15 '05 #5
Quinn Kirsch <an*******@disc ussions.microso ft.com> wrote:
I understand why these problems have all occurred in the
past with our friends C and C++, but I thought that these
type of problems were supposed to be corrected Java or
C#. I thought these languages were made to get rid
mistakes like mispointed pointers and uninitialized
memory, but I guess that they did not get it all.


I think you misunderstand either the aims of the language and casting
in particular, or you misunderstand floating point.

The reason you *need* a cast from double or decimal to float is
precisely *because* some data will almost certainly be lost. It's not a
mistake, it's the way it's meant to be.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #6
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Oh, trust me, Java doesn't fix that problem either. What do you think
they added java.math.BigDe cimal for? ;) In fact, C#'s solution to this
(decimal) is much nicer IMO than Java's since you don't have to say

(a.add(b)).subt ract(c);

to say a + b - c. Not to mention the annoyingly verbose line:

BigDecimal dec = new BigDecimal("1.1 22132131235346" );

Quinn Kirsch wrote:

| I understand why these problems have all occurred in the
| past with our friends C and C++, but I thought that these
| type of problems were supposed to be corrected Java or
| C#. I thought these languages were made to get rid
| mistakes like mispointed pointers and uninitialized
| memory, but I guess that they did not get it all.

- --
Ray Hsieh (Ray Djajadinata) [SCJP, SCWCD]
ray underscore usenet at yahoo dot com
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (MingW32)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQE/na8rwEwccQ4rWPg RAqbjAJ44MIgMND AZgFHw6iqVbs3yX VsCmACfbvY2
eGF9e5LZkSY3gHn 4BK9Ib7I=
=ovx0
-----END PGP SIGNATURE-----

Nov 15 '05 #7
Ray Hsieh (Ray Djajadinata) <ch***@my.signa ture.com> wrote:
Oh, trust me, Java doesn't fix that problem either. What do you think
they added java.math.BigDe cimal for? ;) In fact, C#'s solution to this
(decimal) is much nicer IMO than Java's since you don't have to say

(a.add(b)).subt ract(c);

to say a + b - c. Not to mention the annoyingly verbose line:

BigDecimal dec = new BigDecimal("1.1 22132131235346" );


BigDecimal and decimal are somewhat different though - BigDecimal has
*arbitrary* precision, rather than the fixed precision of decimal. It
would be nice if both languages had the decimal type and both libraries
had the BigDecimal type (and BigInteger), but there we go...

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #8
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Yeah, true that... although personally I have never needed the arbitrary
~ precision of BigDecimal--I just need something that will compute right
(unlike float or double), so C#'s decimal is a good enough solution for me.

| It would be nice if both languages had the decimal type and both
| libraries had the BigDecimal type (and BigInteger), but there we go...

Yes indeed. It would be the correct thing to do too, since the Big Ones
are meant to take care of the arbitrary-precision part of the deal (the
fact that BigDecimal computes correctly, IMHO, is a "bonus". I totally
agree with you that Java should have its own decimal type.).

This, plus the fact that it is a class, and located in java.math instead
of java.lang, makes it way less obvious than it should be, and every
once in a while you'll see otherwise experienced Java programmers doing
stuff like funds transfer/withdrawal using double :)

- --
Ray Hsieh (Ray Djajadinata) [SCJP, SCWCD]
ray underscore usenet at yahoo dot com
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (MingW32)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQE/nj9jwEwccQ4rWPg RAmzJAJwMtRqx26 TUEBZLcOcjdISJ5 B/glACfdNz3
Zs0RtG13WVaT5tw INR4DfgM=
=uPjI
-----END PGP SIGNATURE-----

Nov 15 '05 #9

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

Similar topics

17
6135
by: John Bentley | last post by:
John Bentley: INTRO The phrase "decimal number" within a programming context is ambiguous. It could refer to the decimal datatype or the related but separate concept of a generic decimal number. "Decimal Number" sometimes serves to distinguish Base 10 numbers, eg "15", from Base 2 numbers, Eg "1111". At other times "Decimal Number" serves to differentiate a number from an integer. For the rest of this post I shall only use either...
5
1352
by: lamthierry | last post by:
Are the following two similar? float a = 1.0f; float a = static_cast<float>(1.0); If they are, which one is preferrable? Thanks Thierry
12
13635
by: 6tc1 | last post by:
Hi all, I just discovered a rounding error that occurs in C#. I'm sure this is an old issue, but it is new to me and resulted in a fair amount of time trying to track down the issue. Basically put the following code into your C# app: float testFloat2 = (int) (4.2f * (float)100); Console.Out.WriteLine("1: "+testFloat2); and the result will be 419
5
5273
by: Thorsten | last post by:
Hi everyone, I am rather new to C# and have a problem that will probably seem trivial to most of you... but I hope you can still help me nevertheless.. Via the comport, I read the result of a digital scale... the result is sent as a string like "+0000.23kg", representing the weight in kilograms. In order to work with the returned value, I need to use it as a float or decimal or double... I tried casting it via Convert, via...
8
1418
by: buzzweetman | last post by:
Can someone explain this: void Test() { short x = 10; float y = 9.9f; float a = (float)x; a *= y; short b = (short)a; // I get 99. Good!
17
2216
by: sophia.agnes | last post by:
Hi , I was going through peter van der linden's book Expert C programming, in this book there is a section named "How and why to cast" the author then says as follows (float) 3 - it's a type conversion and the actual bits change. if you say (float) 3.0 it is a type disambiguation,and the compiler can plant the correct bits in the first place.some people say that
32
2373
by: alex.j.k2 | last post by:
Hello all, I have "PRECISION" defined in the preprocessor code and it could be int, float or double, but I do not know in the code what it is. Now if I want to assign zero to a "PRECISION" variable, which of the following lines are correct:
22
2761
by: Bill Reid | last post by:
I just noticed that my "improved" version of sscanf() doesn't assign floating point numbers properly if the variable assigned to is declared as a "float" rather than a "double". (This never cropped up before, since I rarely use "float"s for anything, and hardly ever use the function for floating-point numbers in the first place; I just was messing around testing it for all cases and noticed a problem.) Anyway, it is declared and I...
17
6788
by: D'Arcy J.M. Cain | last post by:
I'm not sure I follow this logic. Can someone explain why float and integer can be compared with each other and decimal can be compared to integer but decimal can't be compared to float? True True False This seems to break the rule that if A is equal to B and B is equal to C then A is equal to C.
0
8445
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
8356
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
8871
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...
0
8781
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
6198
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5664
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4198
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1776
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.