Weird Math problem

  • Thread starter Thread starter Bill Nguyen
  • Start date Start date
B

Bill Nguyen

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")

txtWfCost.Text = Format(mRackc - mDisc + mWfEC, "0.#####0")

txtFactorCost.Text = Format(mRackc - (mDisc + mFactorAdj), "0.#####0")



the discount mDisc was added to the total with the - operator!

Your help is greatly appreciated.



Bill

--------



Private Sub txtDiscount_TextChanged(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles txtDiscount.TextChanged

Dim mDisc, mRackc, mJaEc, mWfEC, mFactorAdj As Double

If IsNumeric(txtDiscount.Text) = True Then

mDisc = txtDiscount.Text

Else

mDisc = 0

End If

If IsNumeric(txtRackCost.Text) = True Then

mRackc = txtRackCost.Text

Else

mRackc = 0

End If

If IsNumeric(txtJacoEC.Text) = True Then

mJaEc = txtJacoEC.Text

Else

mJaEc = 0

End If

If IsNumeric(txtWfEC.Text) = True Then

mWfEC = txtWfEC.Text

Else

mWfEC = 0

End If

If IsNumeric(txtFactorAdjust.Text) = True Then

mFactorAdj = txtFactorAdjust.Text

Else

mFactorAdj = 0

End If

txtJacoCost.Text = Format(mRackc - (mDisc + mJaEc), "0.#####0")

txtWfCost.Text = Format(mRackc - mDisc + mWfEC, "0.#####0")

txtFactorCost.Text = Format(mRackc - (mDisc + mFactorAdj), "0.#####0")



End Sub
 
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")

txtWfCost.Text = Format(mRackc - mDisc + mWfEC, "0.#####0")

txtFactorCost.Text = Format(mRackc - (mDisc + mFactorAdj), "0.#####0")

the discount mDisc was added to the total with the - operator!

Your help is greatly appreciated.

Bill

--------

Private Sub txtDiscount_TextChanged(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles txtDiscount.TextChanged

Dim mDisc, mRackc, mJaEc, mWfEC, mFactorAdj As Double

If IsNumeric(txtDiscount.Text) = True Then

mDisc = txtDiscount.Text

Else

mDisc = 0

End If

If IsNumeric(txtRackCost.Text) = True Then

mRackc = txtRackCost.Text

Else

mRackc = 0

End If

If IsNumeric(txtJacoEC.Text) = True Then

mJaEc = txtJacoEC.Text

Else

mJaEc = 0

End If

If IsNumeric(txtWfEC.Text) = True Then

mWfEC = txtWfEC.Text

Else

mWfEC = 0

End If

If IsNumeric(txtFactorAdjust.Text) = True Then

mFactorAdj = txtFactorAdjust.Text

Else

mFactorAdj = 0

End If

txtJacoCost.Text = Format(mRackc - (mDisc + mJaEc), "0.#####0")

txtWfCost.Text = Format(mRackc - mDisc + mWfEC, "0.#####0")

txtFactorCost.Text = Format(mRackc - (mDisc + mFactorAdj), "0.#####0")

End Sub

What was the value of mDisc? If it is a negative value, then it will
add... - a - equals a plus:

1 - -2 =
1 + +2 = 3
 
Tom;
It's a positive value.
I tested repeatedly.
I also have a similar problem with a bit value of 1 (true) is shown as -1 in
VS2005

Thanks

Bill

-----------
Below is my VS version info:


Microsoft Visual Studio 2005
Version 8.0.50727.762 (SP.050727-7600)
Microsoft .NET Framework
Version 2.0.50727

Installed Edition: Professional

Microsoft Visual Basic 2005 77626-009-0000007-41468
Microsoft Visual Basic 2005

Microsoft Visual C# 2005 77626-009-0000007-41468
Microsoft Visual C# 2005

Microsoft Visual C++ 2005 77626-009-0000007-41468
Microsoft Visual C++ 2005

Microsoft Visual J# 2005 77626-009-0000007-41468
Microsoft Visual J# 2005

Microsoft Visual Web Developer 2005 77626-009-0000007-41468
Microsoft Visual Web Developer 2005

Microsoft Web Application Projects 2005 77626-009-0000007-41468
Microsoft Web Application Projects 2005
Version 8.0.50727.762

Crystal Reports AAC60-G0CSA4B-V7000AY
Crystal Reports for Visual Studio 2005


Microsoft Visual Studio 2005 Professional Edition - ENU Service Pack 1
(KB926601)
This service pack is for Microsoft Visual Studio 2005 Professional Edition -
ENU.
If you later install a more recent service pack, this service pack will be
uninstalled automatically.
For more information, visit http://support.microsoft.com/kb/926601

Security Update for Microsoft Visual Studio 2005 Professional Edition - ENU
(KB937061)
This Security Update is for Microsoft Visual Studio 2005 Professional
Edition - ENU.
If you later install a more recent service pack, this Security Update will
be uninstalled automatically.
For more information, visit http://support.microsoft.com/kb/937061

SQL Server Analysis Services
Microsoft SQL Server Analysis Services Designer
Version 9.00.3042.00

SQL Server Integration Services
Microsoft SQL Server Integration Services Designer
Version 9.00.2047.00

SQL Server Reporting Services
Microsoft SQL Server Reporting Services Designers
Version 9.00.2047.00
 
Bill Nguyen said:
Tom;
It's a positive value.
I tested repeatedly.

Please show us the exact line where the problem occurs and all the values in
all variables in the line.
I also have a similar problem with a bit value of 1 (true) is shown
as -1 in VS2005

You should switch Option Strict On



Armin
 
VB uses -1 for True for compatibility with older versions of VB, but
converts it to 1 when passing it to other .NET languages.

Mark Lincoln
 
In these two expressions:

txtJacoCost.Text = Format(mRackc - (mDisc + mJaEc), "0.#####0")
txtFactorCost.Text = Format(mRackc - (mDisc + mFactorAdj),
"0.#####0")

you're subtracting the sum of mDisc and another value from mRackc. In
this expression:

txtWfCost.Text = Format(mRackc - mDisc + mWfEC, "0.#####0")

you're subtracting mDisc from mRackc, then adding mWfEC to the total.
I have no data with which I can test this, but I suspect you are
either missing a couple of parentheses in the last expression, or you
have too many in the first two.

Mark Lincoln
 
Tom;
It's a positive value.
I tested repeatedly.
I also have a similar problem with a bit value of 1 (true) is shown as -1 in
VS2005

Thanks

Bill


Bill,

First of all, I can tell you do not have option strict turned on.
Which is probably a bad idea, but that doesn't seem to be the root of
your problem:

' numbers i tested
mRackc = 100.0
mDisc = 5
mJaEc = 100.0
mWfEC = 100.0
mFactorAdj = 100.0

txtJacoCost.Text = Format(mRackc - (mDisc + mJaEc), "0.#####0")
100 - (5 + 100) = 100 - 105 = -5


txtWfCost.Text = Format(mRackc - mDisc + mWfEC, "0.#####0")
100 - 5 + 100 = 95 + 100 = 195

txtFactorCost.Text = Format(mRackc - (mDisc + mFactorAdj),
"0.#####0")
100 - (5 + 100) = 100 - 105 = -5

As far as I can tell, things are working correctly. What do you
expect the answers to be?
 
Bill Nguyen said:
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")

txtWfCost.Text = Format(mRackc - mDisc + mWfEC, "0.#####0")

txtFactorCost.Text = Format(mRackc - (mDisc + mFactorAdj), "0.#####0")



the discount mDisc was added to the total with the - operator!

Your help is greatly appreciated.



Bill

--------


In the first and the third equations, if mJaEc or mFactorAdj are greater
than mDisc, then you will end up with a negative value inside the
parenthesis. If that happens, you will end up subtracting a negative number
from mRackc - which becomes an addition operation.

As for the second equation...

If you have a typo and accidentally left the parenthesis out, then the same
will apply to mWfEC.

Todd
 
Whoever told you that load of claptrap!!! It's information like that gives
people the wrong idea of how something works.


BASIC (of which VB is a derivative) defines False as 0 (zero) but True is
the result of a bitwise NOT of False (i.e, True = Not False).

It is only when you attempt to cast a Boolean to another numeric type that
you get a result of -1 for True which is demonstrated by:

Dim _boolean As Boolean = False

Dim _integer As Integer = Not Convert.ToInt32(_boolean)

Console.Writeline(_integer)


VB makes no conversions whatsover when you pass a Boolean to something
written in another .NET language.

If any conversion is required then then some form of 'middle-man' is invoked
to carry out any such conversions.
 
Yes, you're correct: Casting a Boolean produces -1 for True in VB.
However, .NET defines True as 1. VB passes CInt(True) as 1 to
the .NET runtime and -1 within VB. Again, it's -1 within VB for
compatibility with older versions.

The information comes from Francisco Balena's book. I don't have it
in front of me, so I might be a bit hazy on the details. If you
really want me to, I'll quote him verbatim when I get the chance.

Mark Lincoln
 
Thanks all of you for helping me on this "weird math problem" thread.

1. It's stupid me. I put a wrong operator in a different sub and tried to
fix it in another sub! The formula is working properly.

2. Regarding the -1 as true, it has caused problem for me probably because I
did not set Strict = ON. When I did, the project gave me tons of errors that
I will have to fix line by line, so I don't know if it's a good thing to
switch it on.

Thanks again

Bill
 
It is an Extremely Good Thing to have STRICT ON. Yes it can be a pain
when you first turn it on, but some of those tons of errors you get
are real errors that need to be fixed.
 
I have to agree with Jack; Option Strict is the way to go. It can
keep you from making errors that result in posting to newsgroups for
answers. :)
 
Yes, you're correct: Casting a Boolean produces -1 for True in VB.
However, .NET defines True as 1. VB passes CInt(True) as 1 to
the .NET runtime and -1 within VB. Again, it's -1 within VB for
compatibility with older versions.

Yep - it was one of the stupid changes made in the Beta2 rollback
debacle. Before Beta2 VB.NET defined True = 1, but in a misguided
effort to maintain "compatability" they changed the behavior of
Boolean to the confusing mess it is today. The fact is, no one should
even care what the numeric value of a Boolean is - you should NEVER be
treating it as a number.
 
Sometimes multiple errors can be fixed by changing the type of a
property, method argument or function return value. Other times each
one needs to be changed. Without knowing what the errors are I can't
tell.
 
Hear, hear! I can't remember where I first saw that sentiment, but
I've never counted on the numeric value of a Boolean ever since.

Mark Lincoln
 
Back
Top