473,498 Members | 1,842 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

VB.Net Maths Bug?? Please Help.

Hello All,

In VB.Net 2003 why is this the result to a simple maths calculation?

3.53 - 3.52 = 0.00999999046325684

Its not just this one either try this:

1.001 - 0.001 = 0.999999999999989

Coding example:
Dim Calc1 As Double = 3.53F
Dim Calc2 As Double = 3.52F
Msgbox(Calc1 - Calc2)

Why? Please help

TBS
Nov 22 '05 #1
5 1816
Welcome to floating point arithmetic.

Use the Decimal datatype to avoid this.

"Scott Gunn" <ne**@thebigspider.co.uk> wrote in message
news:O9****************@TK2MSFTNGP09.phx.gbl...
Hello All,

In VB.Net 2003 why is this the result to a simple maths calculation?

3.53 - 3.52 = 0.00999999046325684

Its not just this one either try this:

1.001 - 0.001 = 0.999999999999989

Coding example:
Dim Calc1 As Double = 3.53F
Dim Calc2 As Double = 3.52F
Msgbox(Calc1 - Calc2)

Why? Please help

TBS

Nov 22 '05 #2
Hi Scott,

"Scott Gunn" <ne**@thebigspider.co.uk> wrote in message
news:O9****************@TK2MSFTNGP09.phx.gbl...
Hello All,

In VB.Net 2003 why is this the result to a simple maths calculation?

3.53 - 3.52 = 0.00999999046325684

Its not just this one either try this:

1.001 - 0.001 = 0.999999999999989

Coding example:
Dim Calc1 As Double = 3.53F
Dim Calc2 As Double = 3.52F
Msgbox(Calc1 - Calc2)

Why? Please help

TBS You are seeing the expected result of an operation on two numbers that
cannot be exactly represented in the IEEE format for floating point numbers,
whether single precision or double precision. It is not a bug. Go to this
site http://docs.sun.com/source/806-3568/ncg_goldberg.html and other similar
explanations of the limitations of limited precision math.

Charles

Nov 22 '05 #3
Crikey...

I didn't realise maths was so complicated. This page is for Einstine!
http://docs.sun.com/source/806-3568/ncg_goldberg.html

From now on I'll use:
Dim Calc1 As Decimal = 3.53D
Dim Calc2 As Decimal = 3.52D

and cdec instead of cdbl

Many Thanks
TBS

"Scott Gunn" <ne**@thebigspider.co.uk> wrote in message
news:O9****************@TK2MSFTNGP09.phx.gbl...
Hello All,

In VB.Net 2003 why is this the result to a simple maths calculation?

3.53 - 3.52 = 0.00999999046325684

Its not just this one either try this:

1.001 - 0.001 = 0.999999999999989

Coding example:
Dim Calc1 As Double = 3.53F
Dim Calc2 As Double = 3.52F
Msgbox(Calc1 - Calc2)

Why? Please help

TBS

Nov 22 '05 #4
Welcome to the world of computers!

Computers do arithmetic in binary. The numbers 0.1, 0.01, etc., have the
same problem in binary that 1/3 does in decimal: no matter how many digits
you have, you can never get them exact. (1/3 is close to 0.3333, closer to
0.33333, and so forth.)

..NET Framework provides the Decimal data type for exact calculations with
multiples of 1/10 and 1/100.
Nov 22 '05 #5
Scott Gunn <ne**@thebigspider.co.uk> wrote:
I didn't realise maths was so complicated. This page is for Einstine!
http://docs.sun.com/source/806-3568/ncg_goldberg.html

From now on I'll use:
Dim Calc1 As Decimal = 3.53D
Dim Calc2 As Decimal = 3.52D

and cdec instead of cdbl


Using decimal only shifts the problem around a bit.

See http://www.pobox.com/~skeet/csharp/floatingpoint.html for another
description. It's not really that hard.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 22 '05 #6

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

Similar topics

5
419
by: Scott Gunn | last post by:
Hello All, In VB.Net 2003 why is this the result to a simple maths calculation? 3.53 - 3.52 = 0.00999999046325684 Its not just this one either try this: 1.001 - 0.001 = 0.999999999999989
5
1745
by: Ibys | last post by:
Hi, i am just starting to learn javascript, so i am probably doing something very simple wrong. i have read a lot of articles on maths in java, but cant find anything simple enough for my problem. I...
1
1721
by: Orbie | last post by:
Hi All, I'm new to VB.NET and i'm looking for some help with my Windows Form. I need to check if a Commodity entered into (TextBox1.Text) already exists on my table before i insert it. I'm having...
0
7126
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
7005
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
7168
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
7210
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...
0
7381
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
4595
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...
0
3096
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...
0
3087
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
659
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.