3

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 + " ");

cookie23
  • 61
  • 1
  • 6

2 Answers2

3

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));
billjamesdev
  • 14,554
  • 6
  • 53
  • 76
  • I tried what you suggested but the qamp displayed something like 0.18000000000002 and qamp is 0.13999999999 @Bill James – cookie23 Feb 24 '13 at 02:47
  • Ok, is that wrong? What are the values for trial[1], Pycoor[1], and Qycoor[1] and what is the result of the FINALBOXWIDTH call? – billjamesdev Feb 24 '13 at 02:49
  • What can I possibly do to make it just 0.18 and 0.14? – cookie23 Feb 24 '13 at 02:49
  • It's not wrong. I just want to just have 2 decimal places? 0.180000000002 doesn't look good. :( – cookie23 Feb 24 '13 at 02:50
  • If 0.18 and 0.14 is more what you were expecting, that's ok. You can change them when you DISPLAY them, not in the variable. You can use something like a Formatter. I'll edit the above – billjamesdev Feb 24 '13 at 02:51
  • I am kinda new to Java so I am not familiar with Formatter @Bill James – cookie23 Feb 24 '13 at 02:54
  • @cookie23 Remember you can always go to the docs to learn more! http://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html and http://docs.oracle.com/javase/7/docs/api/java/text/DecimalFormat.html – ajp15243 Feb 24 '13 at 02:55
  • @cookie23 You can click the "edited X time ago" link at the bottom of the answer post to see revision history on that answer. Same goes for questions. – ajp15243 Feb 24 '13 at 03:00
  • I added a DecimalFormat example to print pamp and qamp rounded to 2 places. Just reload the page – billjamesdev Feb 24 '13 at 03:00
  • I don't know what to use hahaha! – cookie23 Feb 24 '13 at 03:00
  • Where should I put that part? I put that below the qamp = .... but it doesn't work. – cookie23 Feb 24 '13 at 03:05
  • Define... "doesn't work"... I have no idea where you were "seeing" the 0.18000000002 value. the call to df.format(pamp) returns a String containing "0.18". I don't know what else I can do to make this more clear. – billjamesdev Feb 24 '13 at 03:06
  • Well, I changed the print's to println's, so you can see each value on a separate line in the output, but until you describe what IS happening, it's hard to know what the problem is. – billjamesdev Feb 24 '13 at 03:09
  • Sorry. Should I put it inside the if statement where I calculated for pamp and qamp? – cookie23 Feb 24 '13 at 03:09
-1

This may be because of the datatypes that you are using.
1. Make sure the data types of all the variables involved in the calculation are either float or double (or any other data type that handles decimals well such as Decimal, BigDecimal etc.)
2. Whenever using float values in a formula it is required to specify them with an f. So your formula should be

pamp = (( (trial[1] - Pycoor[i]) / FINALBOXWIDTH(bitmap_Source))  * 0.1f);
qamp = (( (Qycoor[i] - trial[i]) / FINALBOXWIDTH(bitmap_Source)) *0.1f);
Anurag Joshi
  • 235
  • 1
  • 4
  • 17
  • Umm, if it's an integer math issue, "declaring" 0.1 as float is too late, and unnecessary, since it's already a floating-point number. – billjamesdev Feb 24 '13 at 02:48