473,563 Members | 2,897 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.0099999904632 5684

Its not just this one either try this:

1.001 - 0.001 = 0.9999999999999 89

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 1822
Welcome to floating point arithmetic.

Use the Decimal datatype to avoid this.

"Scott Gunn" <ne**@thebigspi der.co.uk> wrote in message
news:O9******** ********@TK2MSF TNGP09.phx.gbl. ..
Hello All,

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

3.53 - 3.52 = 0.0099999904632 5684

Its not just this one either try this:

1.001 - 0.001 = 0.9999999999999 89

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**@thebigspi der.co.uk> wrote in message
news:O9******** ********@TK2MSF TNGP09.phx.gbl. ..
Hello All,

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

3.53 - 3.52 = 0.0099999904632 5684

Its not just this one either try this:

1.001 - 0.001 = 0.9999999999999 89

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**@thebigspi der.co.uk> wrote in message
news:O9******** ********@TK2MSF TNGP09.phx.gbl. ..
Hello All,

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

3.53 - 3.52 = 0.0099999904632 5684

Its not just this one either try this:

1.001 - 0.001 = 0.9999999999999 89

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**@thebigspi der.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.co m>
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
1750
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 am looking for how to get an IF statement to give me the correct output. I am trying to get it to give me a result for a weeks pay, taking that the...
1
1733
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 issues checking if the number of rows returned from my Select is equal 0? Also i'm wondering should i be checking for an exception and open/closing my...
0
7665
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...
0
7888
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. ...
0
8106
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...
0
7950
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...
1
5484
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...
0
3643
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...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2082
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
924
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...

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.