Using this code I have been trying to get a number between them
I am using a double so I can use negative numbers
double minX = plugin.getConfig().getDouble("location.min.x");
double maxX = plugin.getConfig().getDouble("location.max.x");
double randomX = random.nextDouble(maxX-minX) + minX;
But I get a
The method nextDouble() in the type Random is not applicable for the arguments (double)
But if I set them to ints it works perfectly fine.
nextDouble(),...Returns the next pseudorandom, uniformly
distributed double value between 0.0 and 1.0 from this random number
generator's sequence.
So change it to:
double randomX = random.nextDouble()*(maxX - minX) + minX;
The method: Random.nextDouble(), like Random.nextBoolean() and a few other methods of the Random class, does not take any upper bound arguments. A common workaround/solution is the one provided by flakes above, ie:
double randomX = random.nextDouble()*(maxX - minX) + minX;
Related
Is there a Java operator that would create a value equal to count but in fewer lines?
double theta = //Some value;
int count = 0;
while (theta > 0) {
theta -= pi * (1.0 / 8.0);
count += 1;
}
You've just implemented division by repeated subtraction.
So, if you had actual real numbers in Java, you could do ...
int result = (int) (theta / (Math.PI / 8)) + 1;
... to get the same result. However due to repeated rounding errors in your code, that has many more steps than a simple division, the answers will diverge a bit. The results will be the same for thetas up to around 55 million.
The rounding errors are there because float and double are not accurate representations of real numbers.
See also: Is floating point math broken?
Assuming theta > 0, something like this should work:
int count = (int)Math.ceil (theta/(Math.PI * (1.0 / 8.0)));
or
int count = (int)Math.ceil (theta*8/Math.PI);
How about this: Math.ceil( theta/(pi * (1.0 / 8.0)))?
I'm using my weight as a value of 180 to find my weight in lbs on planets other than earth with a formula to find weight using mass and surface gravity. The problem I'm facing is that whenever I call this method, the weight value returns as 180 all 8 times the method is called. In my code I have weight being calculated with (mass * roundedSG)/433... It shouldn't return as 180. I don't know how to fix this, I've been trying for a couple hours. The for loop is in a different method, I'm doing this for home work by the way, any help would be appreciated to just try and fix this problem of mine. Thanks!
public static double weight(double sGravity, double w)
{
double roundedSG = sGravity / 10;
double mass = (w * 433.59237)/roundedSG;
double weight = (mass * roundedSG)/433.59237;
return weight;
}
for(int i = 0; i < planetSurfaceGravity.length; i++)
{
weightOnPlanets[i] = weight(planetSurfaceGravity[i], weightLbs);
System.out.println(weightOnPlanets[i]);
}
}
I think your math in weight() is incorrect. It looks like weight is actually returning the following:
(w * 433/roundedSG)*roundedSG/433.
The 433s and roundedSGs cancel, and you're just returning w, which I'm guessing is 180?
I renamed your variables and added a constant which makes the code as written a bit clearer:
private static final double GRAMS_PER_POUND = 433.59237;
public static double weight(double sGravity, double weightLbs)
{
double roundedSG = sGravity / 10;
double mass = (weightLbs * GRAMS_PER_POUND)/roundedSG;
double weight = (mass * roundedSG)/GRAMS_PER_POUND;
return weight;
}
So your mass on earth, which is converted to grams, is being divided by the gravity of whatever planet you're checking. Change the mass calculation to be
double mass = weightLbs * GRAMS_PER_POUND;
Then the weight calculation should be ok as is, returning the weight on the other planet converted back from grams to pounds.
You weight method calculation are incorrect. the result of it returns only w which you pass as weightLbs (= 180).
Correct Weight Formula is:
weight = (mass*gravity);
Change from:
double weight = (mass * roundedSG)/433.59237;
To:
double weight = mass * roundedSG;
i seen to be having a problem with the line:
int xPos = ((x / maxX) * X_AXIS_LENGTH) + X_AXIS_OFFSET; =
for testing purposes i have assigned:
int x = 10;
int maxX = 52;
but when used in this calculation x / maxX gives me 0 instead of 0.19!
http://s12.postimage.org/uawn8b6l9/image.png
You're doing integer division here:
x / maxX
Integer division will truncate the fractional part.
Cast one of the parameters to floating-point to fix it:
(double)x / maxX
You might also want to store the whole thing into a double instead of int:
double xPos = (((double)x / maxX) * X_AXIS_LENGTH) + X_AXIS_OFFSET;
Both of the operands for / are integers, so it's performing integer arithmetic. Options:
Use floating point arithmetic:
double xPos = (((double) x / maxX) * X_AXIS_LENGTH) + X_AXIS_OFFSET;
Multiply before the division, and you can still do everything in integer arithmetic, although you'll need to beware of overflow:
int xPos = ((x * X_AXIS_LENGTH) / maxX) + X_AXIS_OFFSET;
Integer division did it to you.
If you expect a floating point value, do it this way:
double ratio = ((double)x)/maxX;
That int type all the way though is a problem. I'd put more thought into that. Where do you want to truncate the fractional part?
xPos should be declared as double, not an integer. Otherwise, the data after the decimal point is truncated.
I want to get two ints, one divided by the other to get a decimal or percentage.
How can I get a percentage or decimal of these two ints?
(I'm not sure if it is right.. I'm probably way off...)
for example:
int correct = 25;
int questionNum = 100;
float percent = correct/questionNum *100;
This is how I thought I could do it, but it didn't work... I want to make the decimal (if there is one) into a percent out of 100 for example in this case it is %25. any ideas anyone?
Here is the correct code (thanks to Salvatore Previti!):
float correct = 25;
float questionNum = 100;
float percent = (correct * 100.0f) / questionNum;
(btw, I am making a project using this for a quiz checking program that is why I need the percentage or decimal)
If you don't add .0f it will be treated like it is an integer, and an integer division is a lot different from a floating point division indeed :)
float percent = (n * 100.0f) / v;
If you need an integer out of this you can of course cast the float or the double again in integer.
int percent = (int)((n * 100.0f) / v);
If you know your n value is less than 21474836 (that is (2 ^ 31 / 100)), you can do all using integer operations.
int percent = (n * 100) / v;
If you get NaN is because wathever you do you cannot divide for zero of course... it doesn't make sense.
Two options:
Do the division after the multiplication:
int n = 25;
int v = 100;
int percent = n * 100 / v;
Convert an int to a float before dividing
int n = 25;
int v = 100;
float percent = n * 100f / v;
//Or:
// float percent = (float) n * 100 / v;
// float percent = n * 100 / (float) v;
One of them has to be a float going in. One possible way of ensuring that is:
float percent = (float) n/v * 100;
Otherwise, you're doing integer division, which truncates the numbers. Also, you should be using double unless there's a good reason for the float.
The next issue you'll run into is that some of your percentages might look like 24.9999999999999% instead of 25%. This is due to precision loss in floating point representation. You'll have to decide how to deal with that, too. Options include a DecimalFormat to "fix" the formatting or BigDecimal to represent exact values.
float percent = (n / (v * 1.0f)) *100
I have two integers x and y. I need to calculate x/y and as outcome I would like to get float. For example as an outcome of 3/2 I would like to have 1.5. I thought that easiest (or the only) way to do it is to convert x and y into float type. Unfortunately, I cannot find an easy way to do it. Could you please help me with that?
You just need to cast at least one of the operands to a float:
float z = (float) x / y;
or
float z = x / (float) y;
or (unnecessary)
float z = (float) x / (float) y;
// The integer I want to convert
int myInt = 100;
// Casting of integer to float
float newFloat = (float) myInt
You shouldn't use float unless you have to. In 99% of cases, double is a better choice.
int x = 1111111111;
int y = 10000;
float f = (float) x / y;
double d = (double) x / y;
System.out.println("f= "+f);
System.out.println("d= "+d);
prints
f= 111111.12
d= 111111.1111
Following #Matt's comment.
float has very little precision (6-7 digits) and shows significant rounding error fairly easily. double has another 9 digits of accuracy. The cost of using double instead of float is notional in 99% of cases however the cost of a subtle bug due to rounding error is much higher. For this reason, many developers recommend not using floating point at all and strongly recommend BigDecimal.
However I find that double can be used in most cases provided sensible rounding is used.
In this case, int x has 32-bit precision whereas float has a 24-bit precision, even dividing by 1 could have a rounding error. double on the other hand has 53-bit of precision which is more than enough to get a reasonably accurate result.
You just need to transfer the first value to float, before it gets involved in further computations:
float z = x * 1.0 / y;
Here is how you can do it :
public static void main(String[] args) {
// TODO Auto-generated method stub
int x = 3;
int y = 2;
Float fX = new Float(x);
float res = fX.floatValue()/y;
System.out.println("res = "+res);
}
See you !
Sameer:
float l = new Float(x/y)
will not work, as it will compute integer division of x and y first, then construct a float from it.
float result = (float) x / (float) y;
Is semantically the best candidate.