Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

showing wrong value from equation

Tags:

android

math

i am trying to implement following equation but not showing exact value.

Suppose the value of data1 to data9 are:5,1,1,1,2,1,3,1,1 and the value of y will be 2. but showing null.original equation link:https://www.researchgate.net/publication/303232034_Mathematical_Model_Development_to_Detect_Breast_Cancer_Using_Multigene_Genetic_Programming

 double term3 =Math.cos(Math.log10(data7))/(data6+data7-6.419);



double y=0.05321*Math.log10(Math.exp(data2-1.0*Math.sin(Math.cos(data8))+Math.cos(data6)-(1.0*data6)/(data1*data3*data9*Math.sin(data8)*Math.tanh(data8))))-1.573*Math.sin(Math.sqrt(data4+data6+term3))
            -0.07468*Math.sin(Math.exp(data2*data9))
            -0.9181*Math.log10(2.0*data4+data6+Math.sin(Math.tanh(data7)+5.626*data4*data9)+Math.sin(Math.sin(Math.sin(data8)))
            +Math.sin(Math.sin(2.0*data6-7.128))+(Math.cos(data4)/(data6+data7-6.649))+(Math.cos(data4)/(data6+data7-6.634))+(Math.cos(data4)/(data4*(2.0*data6-1.479))))
            +0.2919*Math.sin(Math.sin(Math.cos(data2+data5+data8)/data2+data7-6.76))
            -0.07468*Math.sin(Math.sqrt(2.0*data4+data7-1.0*Math.sqrt(data6)))
            -0.07468*Math.sin(data6)-((5.381*Math.pow(10, 15)*Math.cos(Math.sqrt(data2*Math.pow(data3, 2))))/(7.206*Math.pow(10, 16)*data3+7.206*Math.pow(10, 16)*data5-4.934*Math.pow(10, 17)))
            -((5.381*Math.pow(10, 15)*Math.cos(Math.sqrt(data1*data3*data4)))/(7.206*Math.pow(10, 16)*data5+Math.sqrt(Math.tanh(data7))*(data7-1.0*data4+7.206*Math.pow(10, 16)*Math.cos(data6))-4.836*Math.pow(10, 17)))
            +((5.258*Math.pow(10, 15)*Math.cos(data4))/(1.801*Math.pow(10, 16)*data2+1.801*Math.pow(10, 16)*data7-1.2*Math.pow(10, 17)))
            +(0.4014*Math.cos(data6+data7)+Math.sqrt(data7+5.446))/((data5+Math.log10(data1)-6.585)*(Math.log10(data7)-1.0*Math.tanh(data5)-5.646*data4*data9)
            +Math.sqrt(Math.tanh(data8))*(data7-1.0*data4+Math.cos(data6))+data2*Math.pow(data4, 2)*Math.cos(data2)*Math.cos(data6))-0.003903*data1*Math.cos(Math.sqrt(data2+data4+data6-1.0*Math.sqrt(data4)))*(
            3.057*data1+2.0*data2-1.0*data4+data6+data7+data9+Math.cos(6.478*data6)+Math.cos(data6)-1.0*Math.sqrt(data1*data3*data6)-6.419)+((0.2919*Math.cos(Math.sqrt(data1*data3*data6)))/(2.0*data6-7.218)*(data5+data7-6.76))+5.235;

the value of y must be either 2 or 4 .but it is not intering in the if else condition

int x= (int) y;

       if(x==2 )
       {
           condition="B";
           Toast.makeText(getApplicationContext(),"value of x is "+x,Toast.LENGTH_LONG).show();
       }
        else if(x==4)
       {
           condition="M";
           Toast.makeText(getApplicationContext(),"value of x is "+x,Toast.LENGTH_LONG).show();
       }

cann't understand where is the problem....help please...

like image 944
nasif Avatar asked Sep 13 '26 21:09

nasif


1 Answers

Since y is a double, calculations are extremely unlikely to give exactly 2 or 4. What's probably happening is that y is a tiny bit smaller than 2 or 4 when the first condition fails.

Instead of casting y directly to an int, you could try using long x = Math.round(y);. Alternatively (and perhaps better), just check that Math.abs(y - 2) or Math.abs(y - 4) is smaller than some small threshold (e.g., .0001), so as to allow for possible rounding error.

like image 184
Ted Hopp Avatar answered Sep 16 '26 16:09

Ted Hopp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!