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.
Related
I am trying to find the result of log(10^k) , where k is big number like 10000. For example :
BigDecimal first = BigDecimal.TEN.pow(10000);
double result = Math.log(first.doubleValue());
However "result" becomes Infinity , however on wolphram approximates it to 23025.85.Any suggestion how to find the result? As a result the number with the first two digits after the decimal point are enough for me.
Use the fact that
log(10^k) = k*log(10)
So:
System.out.println(10000 * Math.log(10));
Prints:
23025.850929940458
The problem you are likely having, is that Wolphram is able to either hold the powered value or it is doing the log operation first.
When running this like your example, you will have an extremely large number that goes past the maximum value for a BigDecimal, which should result in an error or an "infinity", because it overflows the capability of the data type, I would suggest doing the operation the other way arround, perhaphs process the log first on a base 1 value for example and only then multiply it by whatever powered number you are tying to use.
See, there is a simple property of logarithms that you can use:
log(x^y) = y*log(x)
So what you can do is:
double y = y*log(x);
System.out.println(Math.round(y));
Hope this helps!
i just try to program pong with the use of java swing components.
My problem ist, that i need to store the coordinates of my JButton (my Paddle) in variables to be able to manipulate the position of the Button when moving.
I tried doing it that way:
int posP1_x= paddel1.getLocation().getX(); //Error
When compiling it says, that there is a lossy conversion from double to int. (But the retrun value of getX should be int and in the Point-Class the values are also stored as ints). When i try declaring posP1_x as double and print the variables value on the console, it always prints 0.0. But when i print paddel1.getLocation().getX() directly, it works...
double posP1_x= paddel1.getLocation().getX(); //Works
System.out.println(paddel1.getLocation().getX()); //Prints double value eg 110.0
System.out.println(posP1_x); //Prints double value with 0. --> 0.0
What could be the solution to save JButton Coordinates in Variables.
Thank you and have a nice day
Don't use getX(). Use .x. That's it. For example:
// either
int posP1_x= (int) paddel1.getLocation().getX();
// or
int posP1_x= paddel1.getLocation().x;
More importantly, look at the relevant API before posting here. If you'd simply look at the Point API, you'd have your answer.
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.
Im working on a homework assignment for my intro to computer science class and we are are inputting basic commands to get the percentage of people who drink a certain kind of energy drink. We used JOptionPane to make text boxes and you can input the amount of people and the computer has a set percentage to get the output. My problem is i set up my variables as doubles and my answers are very long decimals. I want to convert the answers to Ints so i can get whole numbers. I have tried to do this through casting but i keep getting the error message" EnergyDrink.java:14: error: variable citrusEnergyDrinkers might not have been initialized". What can i do?
This can't be solved without code. The error is not due to any problem with the conversion, but simply as the compiler-error says:
variable citrusEnergyDrinkers might not have been initialized
This means that the variable might not hold a value at the time you attempt to convert it, which results in undefined behaviour, which java-designers didn't allow for a reason.
The problem is as the error-message tells: citrusEnergyDrinkers gets its value inside some try-catch-block or a block that is only run under certain conditions, like if. One way to work around this would be to simply initialize citrusEnergyDrinkers as 0:
double citrusEnergyDrinkers = 0;.
Note though that this might produce incorrect results depending upon what happens when the value isn't set in case the above mentioned block of code isn't entered/breaks off before setting a value.
For the conversion:
Math.round(citrusEnergyDrinkers) is most likely preferable to a simple cast to int, since double most of the time has some imprecision due to the way it's stored in memory and round will actually round the value, while a cast will simply remove the frictional part. For example:
(int) 0.75 //produces 0
Math.round(0.75) //produces 1
You could multiply the double by 100 and then cast to an int:
double d = .77583495;
int perc = (int) Math.round( d );
I prefer to not cast like that, but it works.
Good luck.
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.