Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
1.80/5 (2 votes)
See more:
I am trying to get water flow with this formula but when I run my app I get 64.52 and I should get 92.71. If I run the same formula in vb.net it works fine. Can sum one please take a look and see what I'm not doing right.

P.S. I'm just starting to learn Android.

Thanks.

Android Code
C#
public void buttonOnClick6(View v) {

        try {

            double B1 = 4      //Double.parseDouble(((EditText)findViewById(R.id.editText7)).getText().toString());
            double Y1 = 3      //Double.parseDouble(((EditText)findViewById(R.id.editText8)).getText().toString());
            double Z1 = 2      //Double.parseDouble(((EditText)findViewById(R.id.editText9)).getText().toString());
            double N1 = 0.012  //Double.parseDouble(((EditText)findViewById(R.id.editText10)).getText().toString());
            double S1 = 0.0003 //Double.parseDouble(((EditText)findViewById(R.id.editText11)).getText().toString());

            double A1 = (B1 * Y1) + Z1 * pow(Y1, 2);//tbB * tbY;
            double P1 = B1 + (2 * Y1) * pow((1 + pow(Z1, 2)), 0.5);//tbB + (2 * tbY);
            double R1 = A1 / P1;

            double CFS = ((1.49 / N1) * A1 * pow(R1, (2/3)) * pow(S1, 0.5));
            double SEC1 = (CFS / A1);

            TextView textElement3 = (TextView) findViewById(R.id.tb006);
            textElement3.setText("Cross-Sect. Area, A = " + String.format("%.2f",A1));

            TextView textElement4 = (TextView) findViewById(R.id.tb007);
            textElement4.setText("Wetted Perimeter, P = " + String.format("%.2f",P1));

            TextView textElement5 = (TextView) findViewById(R.id.tb008);
            textElement5.setText("Hydraulic Radius, R = " + String.format("%.2f",R1));

            TextView textElement1 = (TextView) findViewById(R.id.tbCFS1);
            textElement1.setText("CFS: " + String.format("%.2f",CFS));

            TextView textElement2 = (TextView) findViewById(R.id.tbSEC1);
            textElement2.setText("FT/SEC: " + String.format("%.3f",SEC1));

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


VB.NET Code this work fine
VB
Dim tbB1 As Double = 4
Dim tbY1 As Double = 3
Dim tbZ1 As Double = 2
Dim tbN1 As Double = 0.012
Dim tbS1 As Double = 0.0003

Dim A1 As Double = (tbB1 * tbY1) + tbZ1 * Pow(tbY1, 2)
Dim P1 As Double = tbB1 + (2 * tbY1) * Pow((1 + Pow(tbZ1, 2)), 0.5)
Dim R1 As Double = A1 / P1

Dim CFS1 As Double = (1.49 / tbN1) * A1 * Pow(R1, (2 / 3)) * Pow(tbS1, 0.5)
Dim FT_SEC1 As Double = CFS1 / A1

lbA.Content = "Cross-Sect. Area, A = " & A1
lbP.Content = "Wetted Perimeter, P = " & P1
lbR.Content = "Hydraulic Radius, R = " & R1

lbCFS.Content = "CFS: " & CFS1
lbFTSEC.Content = "FT/SEC: " & FT_SEC1
Posted
Comments
[no name] 18-Feb-15 11:29am    
I don't know android, but my first Suggestion is: Whenever you use a double number write it as a double to avoid side effects. E.g. instead of Pow(R1, (2/3)) write Pow (R1, 2.0/3.0). This because I think to remember the the first one -depending on the language/environement- always means Pow(R1,0)....
Bruno
Sergey Alexandrovich Kryukov 18-Feb-15 11:34am    
Use the debugger. Even if you want to get 42.
Look at the previous comment. 2/3 computes to 0.
—SA
phil.o 18-Feb-15 11:38am    
Without having read other comments, I would also have gone for a problem with the 2 / 3 integer divison leading to zero.

1 solution

Encouraged by the comments of SA and phil.o, I try to give an answer.

Write any double constant as double, means don't write x= 4, better use x= 4.0.

Some notes besides:
a.) Try to reduce the useless use of "()" where it is not needed. Multiplication before Addition is still valid ;)
b.) Pow(5, 2/3) vs. Pow(5, 2.0/3.0) you can easy check the difference in VS c#
Bruno
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900