473,408 Members | 2,477 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,408 software developers and data experts.

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 12876
> 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.469500005245209 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 ConsoleApplication1 {
public class MainClass {

static void Main(string[] args) {
Console.WriteLine( " 0.4695f is {0:x}",
BitConverter.ToUInt32( BitConverter.GetBytes( 0.4695f), 0));
Console.WriteLine( "(double) 0.4695f is {0:x}",
BitConverter.ToUInt64( BitConverter.GetBytes( ((double)
0.4695f)), 0));
Console.WriteLine( " 0.4695d is {0:x}",
BitConverter.ToUInt64( BitConverter.GetBytes( 0.4695d), 0));
}
}
}
================================================== ==

The output I get is:

================================================== ==
0.4695f is 3ef0624e
(double) 0.4695f is 3fde0c49c0000000
0.4695d is 3fde0c49ba5e353f
================================================== ==

--
mikeb

Nov 15 '05 #3
Quinn Kirsch <an*******@discussions.microsoft.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.com>
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.469500005245209 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*******@discussions.microsoft.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.com>
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.BigDecimal 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)).subtract(c);

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

BigDecimal dec = new BigDecimal("1.122132131235346");

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/na8rwEwccQ4rWPgRAqbjAJ44MIgMNDAZgFHw6iqVbs3yXVsCmA CfbvY2
eGF9e5LZkSY3gHn4BK9Ib7I=
=ovx0
-----END PGP SIGNATURE-----

Nov 15 '05 #7
Ray Hsieh (Ray Djajadinata) <ch***@my.signature.com> wrote:
Oh, trust me, Java doesn't fix that problem either. What do you think
they added java.math.BigDecimal 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)).subtract(c);

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

BigDecimal dec = new BigDecimal("1.122132131235346");


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.com>
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/nj9jwEwccQ4rWPgRAmzJAJwMtRqx26TUEBZLcOcjdISJ5B/glACfdNz3
Zs0RtG13WVaT5twINR4DfgM=
=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
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....
5
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
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...
5
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...
8
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
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...
32
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"...
22
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...
17
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
0
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...

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.