Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Precision Issues with Java (& VB)...

Status
Not open for further replies.

baden

Programmer
Feb 6, 2002
125
US
Explain why Java and VB for that matter don't return 0....

Something to do with storing the sum first and how it is stored????

------------- Java -----------------
class artest
{
public static void main(String[] args)
{
float x = 150.01f;
float y = 10.01f;
float z = x + y - 160.02f;

System.out.println("Z: "+ z);

if (z==0)
System.out.println("works");
else
System.out.println("doesn't work");

}
}
----------------------------

------------ VB : --------------

Option Explicit

Private Sub Form_Load()
Dim x As Double
Dim y As Double
Dim z As Double

x = 150.01
y = 10.01
z = x + y - 160.02


Print z
If z = 0 Then
MsgBox "This really works: " & z
Else
MsgBox "this sucks: " & z
End If

Text1.Text = z


End Sub
 
classic floating point problem, even in C++. The code below displays "breaks" when you run it.

Code:
float x = 150.01f;
float y = 10.01f;
float z = x + y - 160.02f;

if( z == 0.0f)
	cout << &quot;works&quot; << endl;
else
	cout << &quot;breaks&quot; << endl;

if you want to learn more about how/why google things like &quot;floating point math&quot; etc. There are tons of articles/papers on the subject.

-pete
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top