Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Value result is unintentionally rounding up?

So I this code, basically applying simple mathematical arithmetic to variables. rrdistance, qsdistance, heartrate give desirable values, while pamp and qamp don't. I think it rounds up? Supposedly, at i = 1, the values of trial[1] is 120, Pycoor[1] is 102, and Qycoor[1] is 134. FINALBOXWIDTH(bitmap_Source) is 10.

So the expected results of pamp = ((120-102)/10) * 0.1 = 0.18 while qamp = ((134-120)/10) * 0.1 = 0.14

I don't understand why they both display pamp = 0.1 and qamp = 0.1.

static int[] Pxcoor = new int[50];
static int[] Pycoor = new int[50];
static int[] Qxcoor = new int[50];
static int[] Qycoor = new int[50];
static int[] Rxcoor = new int[50];
static int[] Rycoor = new int[50];
static int[] Sxcoor = new int[50];
static int[] Sycoor = new int[50];
static int[] Txcoor = new int[50];
static int[] Tycoor = new int[50];
    static int[] trial = new int[450];

public static int FINALBOXWIDTH(Bitmap src) {  ...
}

private void StratBackgroundProcess() {

if (i >= 2) {
  rrdistance += (((Rxcoor[i] - Rxcoor[i - 1]) / FINALBOXWIDTH(bitmap_Source)) * 0.04); 
  //durations in seconds
  printerval += (((Rxcoor[i] - Pxcoor[i]) / FINALBOXWIDTH(bitmap_Source)) * 0.04);
  qsdistance += (((Sxcoor[i] - Qxcoor[i]) / FINALBOXWIDTH(bitmap_Source)) * 0.04);
  heartrate += (1500 / (rrdistance / 0.04)); 

  //amplitude in mV
  pamp = (( (trial[1] - Pycoor[i]) / FINALBOXWIDTH(bitmap_Source))  * 0.1);
  qamp = (( (Qycoor[i] - trial[i]) / FINALBOXWIDTH(bitmap_Source)) *0.1);


    }
}

Pamp = pamp; Qamp = qamp;

coordinate.setText("" + pamp + "," + qamp + " ");

like image 228
cookie23 Avatar asked Dec 18 '25 18:12

cookie23


1 Answers

The problem is almost certainly the declaration of the trial, Pycoor, Qycoor, and FINALBOXWIDTH items. If these are integers (and they almost certainly are from your results), then:

((120-102)/10) * 0.1 = (18/10) * 0.1 = 1 (note integer math rounds down) * 0.1 = 0.1

Now, I don't actually know the values in trial, etc. since you didn't provide them (well, you have since, though, and it validates my work), but try changing those two lines to:

  pamp = (( (trial[1] - Pycoor[i]) / (double)FINALBOXWIDTH(bitmap_Source))  * 0.1);
  qamp = (( (Qycoor[i] - trial[i]) / (double)FINALBOXWIDTH(bitmap_Source)) *0.1); 

Note I just forced the FINALBOXWIDTH to double type, which will force floating-point arithmetic.

Note, you'll get double values, which don't always display well. Try this:

DecimalFormat df = new DecimalFormat("#.##");
System.out.println(df.format(pamp));
System.out.println(df.format(qamp));
like image 183
billjamesdev Avatar answered Dec 20 '25 08:12

billjamesdev



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!