473,387 Members | 1,790 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,387 software developers and data experts.

weird math problem!!

OK...I have a form that has two text boxes. These two fields are being
filled from a file import, which works fine. I am then passing these
values into a custom class with both values typed as DOUBLE (IE:
double.parse(mytextbox1.text) ).

When I subtract these two values, I'm get an erroneous .00000000000002
being added to the result of the subtraction!!

The form fills the instance of the class...

myclass.value1 = double.parse(mytextbox1.text)
myclass.value2 = double.parse(mytextbox2.text)

....then in another procedure I'm trying to do the math...
Dim MyValue as double = myclass.value1 - myclass.value2

No matter what the source numbers, I always get a .00000000000002
added into the figure!!!! The class values are always correct, it's
just the subtraction that generates the incorect figure!!

This is driving me crazy!!!!!!

Anyone have any idea!?!?!?!?!?!?!?!

Oct 1 '07 #1
5 1366
Hi Todd

I found this URL which may help to explain things a bit. In summary -
the double, like float is a floating point representation of a number.
As such there is some error built into it. The article advocates using
Decimal data types when you want the precision. Your alternative is to
round the value. Not having done any work with arithmetic in .NET, I
don't know how.

This is well known with the float data type. If you need an example,
try to determine how many $0.1 items you can purchase with $1.00 (the
decimal places are important). The answer (using float data types) is
9.

http://techembassy.blogspot.com/2006...d-decimal.html

Hope this helps
Mike

Oct 1 '07 #2
thats expected behavior. the datatype double which is implemented as
twos complement, does not hold 1/100's accurately, similar to decimal
not holding thirds.

use the decimal datatype.

-- bruce (sqlwork.com)
Todd wrote:
OK...I have a form that has two text boxes. These two fields are being
filled from a file import, which works fine. I am then passing these
values into a custom class with both values typed as DOUBLE (IE:
double.parse(mytextbox1.text) ).

When I subtract these two values, I'm get an erroneous .00000000000002
being added to the result of the subtraction!!

The form fills the instance of the class...

myclass.value1 = double.parse(mytextbox1.text)
myclass.value2 = double.parse(mytextbox2.text)

...then in another procedure I'm trying to do the math...
Dim MyValue as double = myclass.value1 - myclass.value2

No matter what the source numbers, I always get a .00000000000002
added into the figure!!!! The class values are always correct, it's
just the subtraction that generates the incorect figure!!

This is driving me crazy!!!!!!

Anyone have any idea!?!?!?!?!?!?!?!
Oct 1 '07 #3
Use decimal data type.

"Todd" <te**@texter.org.ukwrote in message
news:11*********************@o80g2000hse.googlegro ups.com...
OK...I have a form that has two text boxes. These two fields are being
filled from a file import, which works fine. I am then passing these
values into a custom class with both values typed as DOUBLE (IE:
double.parse(mytextbox1.text) ).

When I subtract these two values, I'm get an erroneous .00000000000002
being added to the result of the subtraction!!

The form fills the instance of the class...

myclass.value1 = double.parse(mytextbox1.text)
myclass.value2 = double.parse(mytextbox2.text)

...then in another procedure I'm trying to do the math...
Dim MyValue as double = myclass.value1 - myclass.value2

No matter what the source numbers, I always get a .00000000000002
added into the figure!!!! The class values are always correct, it's
just the subtraction that generates the incorect figure!!

This is driving me crazy!!!!!!

Anyone have any idea!?!?!?!?!?!?!?!
Oct 2 '07 #4
On 1 Oct, 23:58, mike <m_hansf...@yahoo.com.auwrote:
....I found this URL which may help to explain things a bit. .....http://techembassy.blogspot.com/2006...le-double-and-...

Hope this helps
Mike

Thanks Mike, and to you all. This is my first app heavy on math. I can
not believe this is "expected behaviour". Then again, I just read the
article about Excel 2007 addition problem! Can this be called
"Microsoft Math"?

Oct 3 '07 #5
I can
not believe this is "expected behaviour". Then again, I just read the
article about Excel 2007 addition problem! Can this be called
"Microsoft Math"?
Hi Todd

This isn't a Microsoft thing. It's the IEEE or ISO (can't remember
which) specification. It's built into the standard for floating point
data types like float and double. Microsoft are obviously storing
their numeric data as floating point numbers as it takes less storage
than something more accurate.

Regards
Mike

Oct 16 '07 #6

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

Similar topics

1
by: qwweeeit | last post by:
Another math problem easier to solve by hand, but, IMHO, difficult to program in a concise way like the solution of Bill Mill (linalg_brute.py) to the problem of engsol. I appreciated very much...
11
by: neoromance | last post by:
I ran into a weird memory problem. my c++ code got a segmentation error at the execution. why I try to debug it with gdb, I found out it was due to an earlier memory problem. I then used both...
14
by: marcokrechting | last post by:
Hi All, I have a rather complex math problem concerning dates. I will try to explain my problem. I have a table with the fields SUBJECT (text), DUE DATE (date) and CHECKED (yes/no). In this...
1
by: Gas | last post by:
I am new to C#, I want to perfoem a calculation and I have a weird out come from the app. Here is the code double percentage = 0; percentage = ctrl1.Height / (ctrl1.Height+ ctrl2.Height) ...
4
by: nick | last post by:
I'm having a really weird problem. Take a look at this screenshot. http://users.aber.ac.uk/njb4/dotnetproblem1.jpg Notice the 4 watches at the bottom, _totalRecords, MyDataGrid.PageSize and...
16
by: Bill Nguyen | last post by:
I'm running into a very weird problem regarding subtraction. Subtraction behaves as if it's an addition in the below sub txtJacoCost.Text = Format(mRackc - (mDisc + mJaEc), "0.#####0") ...
5
by: BEES INC | last post by:
I've been awfully busy programming lately. My Django-based side project is coming along well and I hope to have it ready for use in a few weeks. Please don't ask more about it, that's really all I...
0
by: Gary Herron | last post by:
BEES INC wrote: Much simpler this way. This produces the number of whole start and the number of half stars: v = ... calculate the average ... whole = int(v+0.25) half =...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: 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:
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
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...

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.