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

Numeric Casting Bug?


I have just found a problem in a new Dot Net program I have written. At
it's most basic, if you step into a VB application so that you can run the
command window and type:

Debug.print 1.2-1

The return value is:

0.19999999999999996

However, if you type

Debug.print cdec(1.2)-cdec(1)

the return value is:

0.2 as expected.

Has anyone else come across this, or now of why this occurs?

Any help would be appreciated!

Mike
Nov 21 '05 #1
2 1002
"MikeSwann" <Mi*******@discussions.microsoft.com> schrieb:
I have just found a problem in a new Dot Net program I have written. At
it's most basic, if you step into a VB application so that you can run the
command window and type:

Debug.print 1.2-1

The return value is:

0.19999999999999996

However, if you type

Debug.print cdec(1.2)-cdec(1)

the return value is:

0.2 as expected.

Has anyone else come across this, or now of why this occurs?


That's caused by the floating point representation:

<URL:http://research.microsoft.com/~hollasch/cgindex/coding/ieeefloat.html>
<URL:http://www.math.grin.edu/~stone/courses/fundamentals/IEEE-reals.html>
<URL:http://support.microsoft.com/?scid=kb;[LN];42980>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #2
Mike,
In addition to Herfried's links, check out:
http://www.yoda.arachsys.com/csharp/floatingpoint.html

http://www.yoda.arachsys.com/csharp/decimal.html

Debug.print 1.2-1
1.2 & 1 are doubles, so you are using doubles (binary floating point) for
the calculation.
Debug.print cdec(1.2)-cdec(1)
You converted the doubles to Decimals, so you are using Decimal (decimal
floating point) for the calculation.

Instead of CDec(1.2) you could have written 1.2D, where 1.2D is a Decimal
literal, and 1.2 (or 1.2R) is a Double literal, 1.2F happens to be a Single
literal.

Hope this helps
Jay

"MikeSwann" <Mi*******@discussions.microsoft.com> wrote in message
news:AE**********************************@microsof t.com...
I have just found a problem in a new Dot Net program I have written. At
it's most basic, if you step into a VB application so that you can run the
command window and type:

Debug.print 1.2-1

The return value is:

0.19999999999999996

However, if you type

Debug.print cdec(1.2)-cdec(1)

the return value is:

0.2 as expected.

Has anyone else come across this, or now of why this occurs?

Any help would be appreciated!

Mike

Nov 21 '05 #3

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

Similar topics

0
by: Travis Oliphant | last post by:
Numarray is making great progress and is quite usable for many purposes. An idea that was championed by some is that the Numeric code base would stay static and be replaced entirely by Numarray. ...
3
by: Savas Ates | last post by:
i have 3 columns in sql server .. all of them are numeric value when a user enter my site i take the value of userid (numeric one) i assign it as session("userid ") after a query i take 2...
5
by: Pachydermitis | last post by:
Hi all, This has got to be easy, but it's got me going bald. How do I convert a Boolean to a numberic value? This value is going to a hidden field that is going to an SQL server. There has to be...
4
by: Jacob Jensen | last post by:
This question has probably been asked a million time, but here it comes again. I want to learn the difference between the three type cast operators: static_cast, reinterpret_cast, dynamic_cast. A...
3
by: Wim Roffil | last post by:
Hi, I am looking for some function that tests if a value is numeric. I could not find a function for this. At the moment I am using "WHERE CONV(myfield,10,10)!=0" but I suppose there must be...
35
by: ytrama | last post by:
Hi, I have read in one of old posting that don't cast of pointer which is returned by the malloc. I would like to know the reason. Thanks in advance, YTR
4
by: Daniel Billingsley | last post by:
I'm trying to figure out what happens when I cast a double with (int)... does it round or drop the decimal part? MS says: "When you convert from a double or float value to an integral type,...
2
by: Frank | last post by:
Hello, I am attempting to retrieve a numeric type froma a table. I get an invalid cast exception when I use the following code in my data layer: //The problem definitley occurs in the...
7
by: S. Lorétan | last post by:
Hi guys, Sorry for this stupid question, but I don't know why it isn't working. Here is my (example) code: namespace Test { class A { public string Label1; }
3
by: Ethan Strauss | last post by:
Hi, In C#.net 1.1, is there a simple way to determine if an arbitrary object can be cast as a number? How about if it can be cast as a int? The only ways I can find are to either try it and see if...
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
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...
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...
0
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...

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.