i am trying to do some calculations in Java,
but for some reason a simple calculation in 1 or 2 lines of code gives me the wrong answer,
while on the other hand if i do it in 3 steps it works flawlessly.
i know that's it not that bad to do something in a couple more steps,
but why use extra text if it can be shortened??
Could someone give me a pointer if my math is wrong??
this is the 1 line code
percent1 = (((totaloutput1Int - Total1Int) / totaloutput1Int) * 100);
also = (((2232 - 1590) / 2232) * 100)
and this is the multiple steps code which does work.
percent1step1 = (totaloutput1Int - Total1Int);
percent1step2 = ((percent1step1 / totaloutput1Int)* 100);
percent1Tv.setText(String.valueOf(percent1step2));
Change totaloutput1Int and Total1Int from int to double and everything will work fine.
In the 1st method, int/int leads to rounding off of the value. Which leads to a different result.
You need to convert some of your variables to a double to get an accurate answer. int / int is going to give you an int Take a look at this question
So as this is tagged Android, I'm assuming you are using Android Studio. One of it's great features is in-lining (also available in most modern IDEs).
Take this:
float percent1step1 = (totaloutput1Int - Total1Int);
float percent1step2 = ((percent1step1 / totaloutput1Int)* 100);
If you rightclick percent1step1 and select "refactor->inline" android studio will so this:
float percent1step2 = ((((float)(totaloutput1Int - Total1Int)) / totaloutput1Int)* 100);
So it shows you how to achieve things inline, without multiple lines. In this case the result is convert the int from the subtraction in to a float.
Related
I have this equation in java,
double BER = (Erf.erfc(Math.sqrt(3 * CodedEb_No) * Math.sin(Math.PI/8)))/3;
Erf.erfc is from org.apache.commons.math3.special.Erf
I don't know what is CodedEb_No, but BER is 1E-7. How can I calculate CodedEb_No?
I have a hint that I need to use erfcInv() from org.apache.commons.math3.special.Erf. But, since the rest of the values are also part of erfc function, I am bit confused on how to approach this. Can someone help me with this?
I think this is more of a maths than programming question after a very quick look and not testing, I guess you want to rearrange this function to make CodedEb_No the subject. I think that would be
CodedEb_No = Math.pow(Erf.erfcInv(BER * 3) / Math.sin(Math.PI/8), 2) / 3
You need to test this carefully!
I'm very new to Java and stackoverflow so I'm sorry if I seem ignorant but I wrote this program to multiply two numbers together using the Russian Peasant multiplication algorithm. The complete program includes far more operations and is hundreds of lines of code but I only included what I thought was necessary for this particular method. I have a test harness so I know that all the submethods work correctly. The problem that I'm struggling with though is the 3rd line where I'm adding factor1 to the product. The values add correctly but then when factor1 is multiplied by 2 in the 5th line then the value that was added to product in the 3rd line also gets doubled resulting in an incorrect product value. How can I make sure that when factor 1 is doubled that it doesn't carry backwards to the product term?
while (Long.parseLong(factor2.toString()) >= 1) {
if (factor2.bigIntArray[0] % 2 == 1) {
product = product.add(factor1);
}
factor1 = factor1.multiplyByTwo();
factor2 = factor2.divideByTwo();
}
I think in your method multiplyByTwo you use code
`datamember=datamember*2;`
rather than that try doing this
return new FactorClass(datamember*2);
so it doesnt change the added value.
it would be better if u could show the mulTiplyByTwo method code since that is where your actually are getting the changed value.
I am trying to divide two integers values to get a float value.. and I always get the value 0.0.. already tried to cast the values to float and no chance anyway, here is the code:
float othersFloat = (float) others;
float totalPixelsFloat = (float) totalPixels;
// this variables have the values:
// othersFloat : 621347.0
// totalPixelsFloat : 654336.0
// then I do the divison like this:
float percentage_white_on_screen = (float) othersFloat / totalPixelsFloat;
//But I get the value 0.0
Can someone help me?
There is nothing wrong with the code you have shown us. If the inputs are given by the comments, then the result should not be 0.0.
So if it is then either:
the actual inputs do not have the values that you think that they do,
the actual output value is different to what you think it is; e.g. you are printing a different variable ... or something after this code is changing it, or
that is not the code you are executing it; e.g. you've not recompiled it and the code you are running no longer matches the source code.
If this does not help you find the real cause of the problem, you will need to write an SSCCE ... so that other people can actually reproduce your problem for themselves.
I've been working on this for a few days. After getting ridiculous double errors, it seemed using BigDecimal was the way to go. Here's the code: http://pastebin.com/rpbNJnHH
I've never had to use BigDecimal before, so I was hoping someone here could give it a look over and make sure there weren't any stupid mistakes. I've gone through and checked as much as I could be sure of though.
Edit: Sorry, this is vague. So, I was using the double primitive type, but of course doing something like for(double i = 0; i < 1; i += .05) will give imprecise values for i (you'll get things like .0499999999 or .249999999999). So I switched to BigDecimal. However, I never used BD before, so I was hoping someone could look over my code and make sure I was using it correctly (i.e. I wasn't losing precision somewhere). Thanks
I am not sure how this code even compiles.
for (MyPoint point : prevU.keySet()) {
BigDecimal x = point.getX();
BigDecimal t = point.getT();
}
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.BLUE);
g2.fillOval(x, t, 1, 1);
x and t have to be int values and in scope.
I recommend you start with something very simple which compiles and runs correctly, and then add to it slowly. If you try to change too much at once you are likely to end with an overwhelming mess.
Rather than using double or BigDecimal, I would use int values because this is the type you need at the end of the day and unless your screen has more than 2 billion pixels across or down, you don't need long, double or BigDecimal.
I'm new to Java, and I'm using Processing to make some data visualizations. I'm getting this strange error in my code though, was wondering if anyone could help me out. It seems the Xspacing float keeps getting set to Infinity, however when I print out the expression it gets set to the proper value gets printed...
float Xspacing = (endX-(width*.04) - startX)/ values;
println((endX-(width*.04) - startX)/ values);
println(Xspacing);
Result is:
49.0
Infinity
Any help would be appreciated!
Sorry, I wrote this out very quickly and omitted some pretty necessary info:
49.0 IS what is should be. All other types are floats, besides values which is an integer.
The code DOES compile, and println is build into Processing, which is the framework (correct term?) that I'm using. It is basically a function that prints to the console in the Processing GUI.
Xspacing was intended to be data for my class "Graph," however when I define the variable within a public function "drawBasic" everything works fine. Now I am just curious....
Using System.out.println(0 yields the same results. Initial values or variables are:
float startX = 120.00001
float endX = 740.0
int values = 12
width is an integer (although not explicit) that is set to 800
The odd thing seems to be that within a function definition this works fine, its only when I try to define it within the class that it doesn't work...
Your code couldn't be like that because a number *.04 creates a double, and that would mean you'd need to cast the expression into a float.
For your code to compile it would have to be something like
float Xspacing = (float)((endX-(width*.04) - startX)/ values);
println((endX-(width*.04) - startX)/ values);
println(Xspacing);
Now, on the result. If your code had, for example:
System.out.println(3/0);
Java would give you a java.lang.ArithmeticException: / by zero
However, if you have
System.out.println(3f/0);
Then Java will give you "Infinity". Why? http://grouper.ieee.org/groups/754/
Try this:
float Xspacing = (endX-(width*.04) - startX)/ values;
println((float)((endX-(width*.04) - startX)/ values));
println(Xspacing);
float Xspacing = (endX-(width*.04) - startX)/ values;
Even assuming the variables are floats that line does not compile, because of the 0.4 double literal.
Also 'println' is not a standalone method, so you must have written your own.
What is your actual code?
you forget a ) and you should've put System.out.println(xspacing);
fyi you can also just type syso and ctrl spacebar and it will print out the print statement for you.