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

Basic calculation returns incorrect values

We're having a lot of problems getting visual studio to calculate even the
most basic arithmetic correctly.

For example:

Dim MyValue AS Double = 2.155 * 100

This should give me 215.5 right? Wrong. It gives me 215.499999999999999997

Change it to 1.155, it works. 3.155 it works. 2.155 it doesn't work.
We've found several values that don't seem to work correctly. I'm staggered
at this, we're trying to develop accounts based systems and yet visual
studio can't even multiply an item by 100 correctly?! It's not like I'm
doing anything complicated!

Has anyone else come across this and is there some kind of hot fix or patch
that can be applied to sort this out?

We've already been having problems with the round function in .net 1.1 and
had to write our own. The above problem occurs in 1.1 and 2.

Thanks in advance...
Oct 9 '06 #1
3 1372
Dale Reed wrote:
Dim MyValue AS Double = 2.155 * 100

This should give me 215.5 right? Wrong. It gives me
215.499999999999999997
Welcome to the "wonderful" world of floating point computing.

For more information on the reasons for the above-mentioned problem, please
see:

http://www.lahey.com/float.htm

or

http://docs.sun.com/source/806-3568/ncg_goldberg.html

Ebbe
Oct 9 '06 #2
I doubt there's a hot fix for it; the problem, in essence, is that .Net
doesn't know that (215.5 * 100) is an "easier" sum than (214.9871 *
99.31048) - because for a computer, it isn't.

If the latter was wrong by 0.0000000000000003 you'd be hard pressed to
notice. You only notice the specific case because humans think in base
10.

That's an aside though, and not very useful. If you need to know that
your numbers are accurate, avoid floating points. Stick to longs and
divide 'em at the end. That's generally how you are recommended to
store money values in databases, too, for exactly the same reason -
floating point arithmetic is not 100% accurate, and accounting
departments are.
Dale Reed wrote:
We're having a lot of problems getting visual studio to calculate even the
most basic arithmetic correctly.

For example:

Dim MyValue AS Double = 2.155 * 100

This should give me 215.5 right? Wrong. It gives me 215.499999999999999997

Change it to 1.155, it works. 3.155 it works. 2.155 it doesn't work.
We've found several values that don't seem to work correctly. I'm staggered
at this, we're trying to develop accounts based systems and yet visual
studio can't even multiply an item by 100 correctly?! It's not like I'm
doing anything complicated!

Has anyone else come across this and is there some kind of hot fix or patch
that can be applied to sort this out?

We've already been having problems with the round function in .net 1.1 and
had to write our own. The above problem occurs in 1.1 and 2.

Thanks in advance...
Oct 9 '06 #3
why trivial calc.exe in windows calculate 2.155*100 right but .NET doesn't?

"Flinky Wisty Pomm" <Pa********@gmail.comwrote in message
news:11*********************@c28g2000cwb.googlegro ups.com...
>I doubt there's a hot fix for it; the problem, in essence, is that .Net
doesn't know that (215.5 * 100) is an "easier" sum than (214.9871 *
99.31048) - because for a computer, it isn't.

If the latter was wrong by 0.0000000000000003 you'd be hard pressed to
notice. You only notice the specific case because humans think in base
10.

That's an aside though, and not very useful. If you need to know that
your numbers are accurate, avoid floating points. Stick to longs and
divide 'em at the end. That's generally how you are recommended to
store money values in databases, too, for exactly the same reason -
floating point arithmetic is not 100% accurate, and accounting
departments are.
Dale Reed wrote:
>We're having a lot of problems getting visual studio to calculate even
the
most basic arithmetic correctly.

For example:

Dim MyValue AS Double = 2.155 * 100

This should give me 215.5 right? Wrong. It gives me
215.499999999999999997

Change it to 1.155, it works. 3.155 it works. 2.155 it doesn't work.
We've found several values that don't seem to work correctly. I'm
staggered
at this, we're trying to develop accounts based systems and yet visual
studio can't even multiply an item by 100 correctly?! It's not like I'm
doing anything complicated!

Has anyone else come across this and is there some kind of hot fix or
patch
that can be applied to sort this out?

We've already been having problems with the round function in .net 1.1
and
had to write our own. The above problem occurs in 1.1 and 2.

Thanks in advance...

Oct 9 '06 #4

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

Similar topics

5
by: Michael Hagstrom | last post by:
Good Day - I have written a form which collects input for a fare calculation. The form collects the following: 1) Departure or Destination Group (1,2,or 3) 2) Number of Adults 3) Number of...
10
by: Michael G | last post by:
double A = floor((2447297.0 - 122.1)/365.25); i get differing results. It varies between 6699 and 6700. The correct answer is 6699. The 6700 result really throws the entire group of calculations...
9
by: BaWork | last post by:
I'm using the following code to calculate order amount in a payment form for Authorize.net: (Assume book_copies = 1 and ship_country = United States) book_copies = request ("book_copies")...
6
by: jimfortune | last post by:
In: http://groups.google.com/group/comp.databases.ms-access/msg/60d7faa790c65db1 james.ea...@gmail.com said: :Ok, I've answered the first half of my own question. From a MSDN :article, I...
4
by: vg-mail | last post by:
Hello all, I have identical design for form and report but I am getting calculation error on form and everything is OK on report. The form and report are build up on SQL statement. The...
28
by: beach.dk | last post by:
Hi, I'm trying to implement a simple hash algorith called rs_hash in javascript, but I cannot get a correct result. In c the code looks like this:
1
by: `Zidane Tribal | last post by:
it would appear that using the command 'crc("data")' from the String::CRC returns incorrect results (although, they are at least consistently incorrect). for example, this script..... ...
11
by: CrostonScottish | last post by:
I am having a few difficulties calculating values in my report with the sql code that i am using. It is made up of two parts: 1: (This bit is ok) A text box has the control source set as :...
2
by: Will | last post by:
Hi, I need to handle blank values in a query calculation. I have 636 records at the moment but when I sum over these records the blank fields are not returned. I have looked around here and on...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: 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
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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.