1

I can't figure it out where I have done the mistake. Simply, I want to print the value of $i variable in the for loop. But it doesnt print anything. To ensure $fms, $tecsol and etc variables are printed and they have the values.Please see below.

0.6
1.8
2.7
2.5
2.5
2
0.5
3

Here is the code for for loop.

function sortCatergory(){

//assigning return values to variable , return value is an average which is =< 5 
echo $econ=func_Economics();
    echo '</br>';
echo $fms=func_FMS();echo '</br>';
echo $tecsol=func_Technology_Solutions();echo '</br>';
echo $math=func_Mathematics();echo '</br>';
echo $stat=func_Statistics();echo '</br>';
echo $quant=func_Quant_Equity();echo '</br>';
echo $gensoft=func_General_Software_Programming();echo '</br>';
echo $dataprod=func_Data_Products();echo '</br>';

    for($i=5; $i>0.1; $i=$i-.1){
    //echo $i.'</br>';

        if($econ==$i){
            echo $i.'</br>';
        }
        if($fms==$i){
            echo $i.'</br>';
        }
        if($tecsol==$i){
            echo $i.'</br>';
        }
        if($math==$i){
            echo $i.'</br>';
        }
        if($stat==$i){
            echo $i.'</br>';
        }
        if($quant==$i){
            echo $i.'</br>';
        }
        if($gensoft==$i){
            echo $i.'</br>';
        }
        if($dataprod==$i){
            echo $i.'</br>';
        }

    }

}

Here for echo $i.'<br>' print nothing. But I expect to print same result again when they are equal. Where I have done wrong?

3
  • Depending on how your values ($stat, $quant) etc are calculated, you might be experiencing floating-point precision problems. Commented Jun 13, 2013 at 13:34
  • i would first suggest to make the multiple ifs to a single if statement using OR (||) operator to increase readability. Commented Jun 13, 2013 at 13:43
  • Yes, I would do that,but what is the problem here actually? Commented Jun 13, 2013 at 13:46

6 Answers 6

1

I tried your code, this worked for me Pls try this if($econ==(string)$i) { echo $i.''; }

Sign up to request clarification or add additional context in comments.

3 Comments

yes it works. but do you have any idea, why do we need to cast to string?
there is odd behaviour in comparing double values
This is not the correct way to handle floating point number comparison issues.
0

You are working with floating numbers so you can't trust the == comparison. You could compare them using method

if (round($a - $b, 2) == 0) {

or

if (abs($a - $b) < 0.001) {

or

if (round($a, 2) == round($b, 2)) {

or

if ((int) ($a * 100) == (int) ($b * 100)) {

10 Comments

These numbers are printed before loop, I don't think it's a precision issue
Just try it. Number is rounded on output as well.
I return return round($Data_Prod/10,1); like this. therefore, I am ensure to round the result to one decimal place. Therfore, I decrease the value of $i varible by .1
Then add round($i, 1) instead of $i as well.
@Gedrox numbers are not rounded when casted to string
|
0

I'm not sure but it might have to do with floating point imprecision

Try using 'bccomp' and let us know what results you get.

Comments

0

My guess is that the numerics are your foe here.

Try the following: in the beginning cast your numbers.

Since you have two decimal values i would recommend:

 $i = (int) $i*100;
 $econ = (int) $econ*100;

same for the other vars. now you have integers that you can safely compare.

1 Comment

this is not ok with my requirment. I need to decrease the variable value by .1 . this is must.
0

You should read about floating comparsion.

From php manual:

Comparing floats

As noted in the warning above, testing floating point values for equality is problematic, due to the way that they are represented internally. However, there are ways to make comparisons of floating point values that work around these limitations.

To test floating point values for equality, an upper bound on the relative error due to rounding is used. This value is known as the machine epsilon, or unit roundoff, and is the smallest acceptable difference in calculations.

 $epsilon = 0.00001; // this is precision

 if(abs($econ-$i) < $epsilon){
        echo $i.'</br>';
 }

the above code will work OK

Comments

0

Your problem here looks like the values output by the functions (stored in variables) do not return the proper values.

Suggestions are:

1) Make sure to round the return values of the functions to 1 decimal place exactly.

2) Try printing the values of the variables each time by removing the if conditions to debug.

3 Comments

I tried to print the variable values inside the for loop and before if conditions. Every variable print it's value. yes in each function before return the values, I have rounded to one decimal place.
@menuwordpress try using the function gettype() and see what the types of the variables are.
it says double when I use gettype()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.