sebap123
sebap123

Reputation: 2685

Unexpected 0 as a result of mean calculation

As a part of my thread I want to calculate the mean value of 30 readings. To calculate it I am using formula

enter image description here

This is the code of run() method of my thread (the variables are defined before in thread - I've just put them to see the types of variables used):

//Thread's variables
        float[] values;
        String[] str1;
        String[] str2;
        int counter = 0;
        int calibrationCounter = 0;
        StringBuilder strBuilder;
        ReadingsUpdateData updater;
        String msg;
        float[] calibrationValues;
public void run() {
            while (true) {
                try{
                    msg = inputList.poll();
                } catch(NoSuchElementException nse){
                    continue;
                }
                if (msg == null) {
                    continue;
                }
                String[] msgArray = msg.split("!");
                for (String m : msgArray) {
                    if (m.length() == 0) {
                        continue;
                    }
                    if(m.charAt(0)!='A'){
                        strBuilder.append(m);
                        continue;
                    } else {
                        str1 = strBuilder.toString().split(":");
                        if (str1.length != 2) {
                            if(str1.length>2){
                                strBuilder.delete(0,strBuilder.length());
                                continue;
                            }
                            strBuilder.append(m);
                            continue;
                        }
                        if (!str1[0].equals("ANG")) {
                            strBuilder.delete(0,strBuilder.length());
                            continue;
                        }
                        str2 = str1[1].split(",");
                        if (str2.length != 3) {
                            if(str2.length >3){
                                strBuilder.delete(0,strBuilder.length());
                                strBuilder.append(m);
                                continue;
                            }
                            strBuilder.append(m);
                            continue;
                        }
                        try {
                            if(readingsCalibration) {
                                if(calibrationCounter<30) {
                                    Log.d(LOG_TAG,calibrationValues[0] + " = (1/(" +calibrationCounter +"+1))*("+calibrationValues[0] + "*"+calibrationCounter +"+"+str2[0]+"))");
                                    calibrationValues[0] = (1/(calibrationCounter + 1))*
                                            (calibrationValues[0]*calibrationCounter+Float.parseFloat(str2[0]));
                                    calibrationValues[1] = (1/(calibrationCounter + 1))*
                                            (calibrationValues[1]*calibrationCounter+Float.parseFloat(str2[1]));
                                    calibrationValues[2] = (1/(calibrationCounter + 1))*
                                            (calibrationValues[2]*calibrationCounter+Float.parseFloat(str2[2]));
                                    calibrationCounter++;
                                } else {
                                    readingsCalibration = false;
                                    calibrationCounter = 0;
                                }
                            } else {
                                values[0] = Float.parseFloat(str2[0]) - calibrationValues[0];//x
                                values[1] = Float.parseFloat(str2[1]) - calibrationValues[1];//y
                                values[2] = Float.parseFloat(str2[2]) - calibrationValues[2];//z
                                updater.setData(values);
                                EventBus.getDefault().post(updater);
                            }
                        } catch (NullPointerException npe) {

                        } catch (NumberFormatException nfe) {

                        }
                        strBuilder.delete(0,strBuilder.length());
                        strBuilder.append(m);
                    }
                }
            }

        }

Unfortunately, I am getting all the time only zeros as a result. Only at a first run I get some value but next one and each after gives 0. I was thinking about some problem with type casting but then I wouldn't get also first value.

Upvotes: 1

Views: 32

Answers (1)

Mureinik
Mureinik

Reputation: 311808

Since you're dividing two ints, you're performing integer division, i.e., keeping only the whole part of the result.

to get a floating point result, it would suffice to define one of the operands as a double. e.g.:

double calibrationCounter = 0.0;

Upvotes: 2

Related Questions