math need a number between -180 and 180 - java

I have a math problem.
In minecraft the yaw can be between -180 and 180 now I need to add 90 to the player his yaw. but if the yaw is 150 + 90 = 240 that is more than 180, how can I add 90 but if it passes 180 count further on -180.
Examples:
-150 + 90 = -60, No problem
0 + 90 = 90, No problem
150 + 90 = 240, Problem it needs to be -120

This is a classic example of modular arithmetic. You can use the remainder operator %.
int current = 150;
int input = 90;
int newCurrent = ((180 + current + input) % 360) - 180;
System.out.println("NEW: " + newCurrent);
The output is:
NEW: -120

You could solve it like this:
private static final int MIN = -180;
private static final int MAX = 180;
public static void main(String[] args) {
System.out.println(keepInRange(-150 + 90));
System.out.println(keepInRange(0 + 90));
System.out.println(keepInRange(150 + 90));
System.out.println(keepInRange(-150 - 90));
}
private static int keepInRange(final int value) {
if (value < MIN) {
/*
* subtract the negative number "MIN" from "value" and
* add the negative result to "MAX" (which is like subtracting the absolute value
* of "value" from "MAX)
*/
return MAX + (value - MIN);
}
if (value > MAX) {
/*
* subtract the number "MAX" from "value" and to get exceeding number
* add it to "MIN"
*/
return MIN + (value - MAX);
}
return value;
}
The output is:
-60
90
-120
120
According to your explanation, the last to values should be -120 and 120 and not -60 and 60 like you said in your example. The exceeding amount should be added to the "other" bound, and not from 0. 150 + 90 is 240, so it exceeds the MAX bound by 60. This then should be added to the MIN bound. The result is -120.
If this is not correct, then please update your question.

You can use the modulo operator for positive numbers, this looks like a division but gives you the rest instead of the quotient:
10/6 = 1,66666...
10%6 = 4
For you: ( 150 + 90 ) % 180 = 60

Related

How to add or subtract within a certain range

I am trying to add numbers but keep it within a range. If it oversteps or understeps the boundary it will circle back to the other boundary.
Examples:
Min: 10
Max: 50
20 + 10 = 30
20 + 30 = 50 (or 0 idrk)
20 + 31 = 1
40 + 20 = 10
40 + 70 = 10
40 - 10 = 30
10 - 30 = 30
10 - 20 = 40
10 - 70 = 40
I am looking for two functions, one to add, one to subtract. I don't really care which language but python or java is preferred. Thanks for any help!
# define the range
start = 0
end = 93
# this function accepts positive and negative numbers in x
def mover(current_place, x):
locate = current_place + x
locate = locate % (end-start) # if x > the range
if locate > end:
locate = locate - end
if locate < start:
locate = locate + end
return locate
if __name__ == "__main__":
current_place = 50
print(current_place) #50
current_place = mover(current_place, 10)
print(current_place) #60
current_place = mover(current_place, -20)
print(current_place) #40
current_place = mover(current_place, -50)
print(current_place) #83
current_place = mover(current_place, 220)
print(current_place) #24
I'm afraid we have to add more specific information to your question:
is the boundary inclusive or exclusive?
are we talking abount only interger numbers or floating numbers?
If you want to use your routine as library function for many purposes, it's maybe a good idea to define one end as inclusive and the other as exclusive.
The following (incomplete) Java program shows the direction of solution as well as the problem with boundaries: whenever the boundary is hit, it flips to other end of your value frame:
public class FramedAddSub {
int min;
int max;
int value;
public FramedAddSub(int min, int max, int value) {
this.min = min;
this.max = max;
this.value = value;
}
public FramedAddSub add(int toAdd) {
final int diff = max - min;
if (toAdd >= 0) {
// step 1: norm to zero
value -= min;
// step 2: rest of division
value = (value + toAdd) % diff;
// step 3: re-norm to old offset
value += min;
} else {
// step 1: norm to zero from other end
value -= max;
// step 2:
value = (value + toAdd) % diff;
// step 3: re-norm back
value += max;
}
return this;
}
public static void main(String[] args) {
FramedAddSub test = new FramedAddSub(20, 50, 20);
System.out.println("start: " + test.value);
test.add(10);
System.out.println("+10: " + test.value);
test.add(20);
System.out.println("+20: " + test.value);
test.add(1);
System.out.println("+1: " + test.value);
test.add(30);
System.out.println("+30 should just turn around circle: " + test.value);
test.add(-1);
System.out.println("-1: " + test.value);
test.add(-1);
System.out.println("-1: " + test.value);
test.add(-30);
System.out.println("-30 should just turn around circle: " + test.value);
}
}
Try this in python:
ans = abs(a + b) % 50
You need to pass a and b as negative numbers, if you want to do a substraction.
I had the same problem and I use this solution for integers.
( ( (X ± Y) - MaximumValue ) % ( MaximumValue - MinimumValue ) ) + MinimumValue
% : modulo/reminder operator
Update:
The expression above works perfectly on Excel, but according to Microsoft it calculates the modulo like this
MOD(n, d) = n - d*INT(n/d)
Combine this two we got
((X+Y)-Mx)-(Mx-Mn)*Math.floor(((X+Y)-Mx)/(Mx-Mn))+Mn
Mx: MaximumValue
Mn: MinimumValue

Split Number into equal periodic values

I have been given min and max number,
e.g: min = 10 and max = 1000 now i want to split this number into equal parts and in result i should get 10 numbers.
like: 100, 200, 300, 400, 500, 600, 700 , 800 , 900, 1000
this we can do by dividing max with number we want e.g: 1000/10 and add 100 every time.
but, there is one scenario in which we have been given :
min = 14 and max = 1113 in this case if we divide 1113/10 then we will get 111
and the range will come like 0 , 113, 226, 339 ...
but, i don't want to show them like this . It would be better if i can display 110 or 115 or 120 even numbers (round off) instead of 113.
Can anyone help me.
In your example, when min=14 and max=1113 you divide 1113/10 to get 111. If you want to round 111 down to 110, you can perform 111 - (111 % 10) to get 110. Then simply iterate 10 times and multiply the beginning number by the current iteration.
There is a similar post to this here: How to round *down* integers in Java?
Depending on what numbers you want to display (either 110 or 115 or 120) you can approach this in a similar way.
That is an algorithm for scaling a diagram; distributing ticks on an axis.
However 10 ticks is then an over-estimate.
private int[] ticks(int min, int max) {
int gap = (max - min) / 10;
// Round gap up to a nice value - best calculatory:
if (gap < 1) {
gap = 1;
} else if (gap < 5) {
gap = 5;
} else if (gap < 10) {
gap = 10;
} else if (gap < 50) {
gap = 50;
} else if (gap < 100) {
gap = 100;
} else if (gap < 250) {
gap = 250;
...
}
int minTick = (min/gap) * gap;
if (minTick > min) { // For negative numbers.
minTick -= gap;
}
int maxTick = (max/gap) * gap;
if (maxTick > min) { // Negative, and ceiling.
maxTick += gap;
}
int ngap = (maxTick - minTick) / gap;
int[] ticks = new int[ngaps + 1];
for (int i = 0; i <= ngaps; ++i) {
ticks[i] = minTick + i * gap;
}
return ticks;
}

Simple Coding Problems

In the country of Rahmania, the cost of mailing a letter is 40 sinas for letters up to 30 grams. Between 30 g and 50 g, it is 55 sinas. Between 50 g and 100 g, it is 70 sinas. Over 100 g, it costs 70 sinas plus an additional 25 sinas for each additional 50 g (or part thereof). For example, 101 grams would cost 70 + 25 = 95 sinas. 149 g would also cost 95 sinas, because both of these packages only use the first 50 g over 100 g. Write a program that prompts the user for a mass and then gives the cost of mailing a letter having that mass.
This is the question and the only part I don't know how to do is the last part with the additional 25 sinas for each additional 50g.
Can someone show me how to write this part of the code?
I use netbeans btw.
Scanner input = new Scanner(System.in);
double l;
l = input.nextInt();
x = l >= 100 / 50 * 25 + 70;
if (l <= 30) {
System.out.println("40 Sinas");
}
if (l > 30 && l < 50) {
System.out.println("55 Sinas");
}
if (l > 50 && l < 100) {
System.out.println("70 Sinas");
}
if (l >= 100) {
System.out.println("");
}
Here's the important part of the code. As you can see I need help on the last part.
A solution:
Scanner input = new Scanner(System.in);
int l; // it should be int if read int
l = input.nextInt();
if (l <= 30) {
System.out.println("40 Sinas");
}
if (l > 30 && l < 50) {
System.out.println("55 Sinas");
}
if (l > 50 && l < 100) {
System.out.println("70 Sinas");
}
if (l >= 100) {
System.out.println(((((l - 100) / 50) + 1) * 25) + 70);
}
I just want to point out, there is one major problem with Krayo's solution. If the weight value is exactly 100, using that calculation, you will in fact charge the customer an extra 25 sinas. This is caused by the + 1 portion of the calculation.
If the weight is 100, then the number of 50 gram intervals above 100 is 0 (weight - 100 = 0). Therefore, the customer shouldn't be charge anything, since the wording of the question says Over 100 g.... However, because you're adding 1, before performing the additional cost calculation, the customer gets dinged an extra 25 sinas needlessly.
Instead, in order to insure the correct amount for this, and all other cases, I would use the Math.ceil(double) method instead. This method will round the value up to the nearest whole number. For example, 5.2 becomes 6.0, and 5.0 remains 5.0.
So your calculation would look like this instead:
int cost = (int) (Math.ceil((weight - 100) / 50.0f) * 25) + 70;
Note that we need use a floating point value to ensure we end up with a decimal value. And since Math.ceil returns a double, we simply cast it back to an integer.
This is a sample program to demonstrate this:
public class CalculationTesting {
public static void main(String[] args){
int weight = 100;
int good_cost = (int) (Math.ceil((weight - 100) / 50.0f) * 25) + 70;
int bad_cost = ((((weight - 100) / 50) + 1) * 25) + 70;
System.out.println("Weight = " + weight + "g");
System.out.println("Good cost calculation = $" + good_cost);
System.out.println("Bad cost calculation = $" + bad_cost);
}
}
You can change the weight value to experiment.
Which outputs:
Weight = 100g
Good cost calculation = $70
Bad cost calculation = $95

Trying to round to nearest $5, $10, $50, and $100 when computing the next minimum bid in an auction

I'm working on a big program that utilizes a bunch of smaller methods to create a functioning auction system. I'm having trouble with this particular method where I have to raise the minimum bid for an item given the current or "now highest bid"
The instructions are as follows:
If a bid is accepted, then you need to raise the minBid on that item by 5% above the current bid,rounded up to the nearest $5 below $100.00,
rounded up to the nearest $10 between $100.01 and
$1000.00, rounded up to the nearest $50.00 between
$1000.01 and $10000.00, and rounded up to
the nearest $100 above $10000.00
My main issue is figuring out exactly I can round up by just 5 or just 10, depending on the amount of the bid. Here's my code so far, any help would be greatly appreciated :)
public class computeMinNextBid
{
public static double computeMinNextBid(double currentBid)
{
double minNextBid = (currentBid * 0.05) + currentBid;
if(minNextBid <= 100.00)
{
minNextBid = Math.round(minNextBid);
}
else if(minNextBid >= 100.01 && minNextBid <= 1000.00)
{
minNextBid = Math.round(minNextBid);
}
else if(minNext Bid >=1000.01 && <= 10000.00)
{
minNextBid = Math.round(minNextBid);
}
else(minNextBid >= 10000.01)
{
minNextBid = Math.round(minNextBid);
}
return minNextBid;
}
public static void main(String [] args)
{
double currentBid = Double.parseDouble(args[0]);
double minNextBid = computeMinNextBid(currentBid);
System.out.println("The next minimum bid is: " + minNextBid);
}
}
The general expression for rounding to N (using integer arithmetic) is:
x = (x + N/2)/N * N;
This rounds to the nearest multiple of N. For example, for x = 45 an N = 100, this yields 0 (i.e., it rounds 45 down to 0). For x = 55, this yields 100 (i.e., it rounds 55 up to 100).
To round down (also known as truncation), remove the "+ N/2" part:
x = x/N * N;
To round up to the nearest N, use:
x = (x + N-1)/N * N;
For example, for x = 35 and N = 100, this yields 100 (i.e., it rounds any x between 0 and 99 up to 100).

Strange: Cant get '1' from Math.random

I am using the following code, it generates numbers randomly but the problem is, I am not able to figure out why does not it generate the number 1
int ran, g, d, col, ran2;
double y = 1000 * (Double.parseDouble(t2.getText()));
int x = (int) y;
d = Integer.parseInt(anum.getText());
double c = 10;
int prevrandom = Integer.parseInt(lnum.getText());
lnum.setText("");
lnum.setVisible(true);
for (g = 0; g==0;) {
d = Integer.parseInt(anum.getText());
ran = (int) (Math.random() * (c)); // Random Number Creation Starts
if (ran > (c / 10)) {
g = 1;
ran2 = ((int) (Math.random() * 10)) % 2;
if (ran2 == 1) {
ran = ran * (-1);
}
d = d + ran;
if (d < 0) {
ran = ran * (-1);
d = d + (2 * ran);
}
int a = d - ran;
if(prevrandom==ran){
g=0;
}
if(g==1){
lnum.setText("" + ran);
}
}
}
I call this function(button) from somewhere. The problem comes when the sum ('a') becomes 4, according to my conditions it shouldn't allow any number other than 'one' and thus it goes into infinite loop.
I am talking about ran variable. Which I get after multiplying Math.random with 10^x where x is a positive integer.
Here ran2 is a number with value 1 or 0. As I multiply Math.Random with 10 which gives a 1 digit number and I mod it with 2.
THis is a 14 year old boy new to java. it would be greatful of people out here to help rather than discourage.
Look at the Javadoc:
Returns a double value with a positive sign, greater than or equal to
0.0 and less than 1.0. Returned values are chosen pseudorandomly with (approximately) uniform distribution from that range.
If you need integer random numbers, you might be better off with java.util.Random. To generate a random integer in the range a..b (inclusively), you can use
Random random=new Random();
int rnd=a+random.nextInt(b-a+1);
The problem lies in the code
if (ran > (c / 10)) {
The random number gets created which is even equal to one; but here due to the sign '>' it gets rejected.
Use '>=' instead.
ran = (int) (Math.random() * (c)); where c is from 10 to 10^x
This can be 1 as follows.
int c = 1000;
for (int i = 0; i < 1000; i++) {
int count = 0;
int ran;
do {
ran = (int) (Math.random() * (c)); // where c is from 10 to 10^x
count++;
} while (ran != 1);
System.out.println("count: " + count);
}
prints sometime like
count: 1756
count: 86
count: 839
count: 542
count: 365
....
count: 37
count: 2100
count: 825
count: 728
count: 1444
count: 1943
It returns 1 a thousand time in less than a second.

Categories

Resources