I really can't understand why this expression gives me a NaN error only when
acceleration.module - Ambient.friction = 0
:
double x = (speed.module * speed.direction.x()) + ((acceleration.module-Ambient.friction) * acceleration.direction.x())*time;
Moreover if I add pharentesis like these:
double x = ((speed.module * speed.direction.x()) + ((acceleration.module-Ambient.friction) * acceleration.direction.x()))*time;
it gives me a NaN error again.
In the first, the second value is multipled by time.
double x = (speed.module * speed.direction.x()) + ((acceleration.module-Ambient.friction) * acceleration.direction.x())*time;
For the other, the whole thing is multiplied by time:
double x = ((speed.module * speed.direction.x()) + ((acceleration.module-Ambient.friction) * acceleration.direction.x()))*time;
This makes me think you are overflowing the double values in the second one before you multiple with time. With double overflow you get the magic values Double.POSITIVE_INFINITY or Double.NEGATIVE_INFINITY
Most likely you are multiplting time with Double.POSITIVE_INFINITY or Double.NEGATIVE_INFINITY and the result is NAN.
Change the calculations to a series of steps. With each step, check the result for overflow, by looking for Double.POSITIVE_INFINITY or Double.NEGATIVE_INFINITY. That will tell you where things go off the rails. You are almost certainly overflowing somewhere, but we cannot see the numbers you are handling so I cannot tell you exactly where.
My (educated) guess is the nan is already in the direction because it will be indeterminate when module is zero
Related
Working on a Java class, its making me crazy because this expression is evaluating to zero, I need it to evaluate to a double, then round it down to the nearest int. So what Im trying to get is for days to be a whole number of days, yet when I run it through java it evaluates to 0. When I run it through my calculator it evaluates to the correct value. I would love a fix and an explanation to why this what I already have isn't working.
public int getEventDays(){
//variables
double daysCalc;
int days;
//logic
if (getStatus().equals("filling")){
//this is indented less to fit everything on one line, its not this way in
//the fractions are for unit conversion
daysCalc= Math.floor(((capacity-storage)/(inflow-outflow))*(43560)*(1/3600)*(1/24));
days = (int)daysCalc;
}
else if (getStatus().equals("emptying")){
//this is indented less to fit everything
//the fractions are for unit conversion
daysCalc=Math.floor(((storage-0)/(outflow-inflow))*(43560)*(1/3600)*(1/24));
days = (int)daysCalc;
}
else{
days = -1;
}
return days;
}
Change your code to this :
daysCalc = Math.floor(((storage-0)/(outflow-inflow))*(43560)*(1.0/3600)*(1.0/24));
Explanation:
The right hand expression is returning an integer value. In your case, 1/3600 is rounded to 0, similar to the case of 1/24.
Now by using 1.0 instead of 1, it is giving the unrounded float value of 1/3600.
Your problem is connected with the order of operations within your expression. The parentheses around 1/3600 and 1/24 cause these expressions to be evaluated first - and since each of these divisions has an expression of integer type on either side of the division, it's treated as an integer division. In other words, 1/3600 and 1/24 are both evaluated as integers, to give a result of zero. This means that your arithmetic includes a couple of multiplications by zero, which is why your result is zero.
The simplest fix is to understand that multiplying by the reciprocal of some number is the same as dividing by that number. In other words, you could simplify the calculation to
daysCalc = Math.floor( storage / ( outflow - inflow ) * 43560 / 3600 / 24 );
which will give the correct result, provided storage, outflow and inflow are not all integers.
On the other hand, if storage, outflow and inflow are all integers, then you'll need to make sure that the first division is also not treated as an integer division. You could do this by writing
daysCalc = Math.floor((double) storage / ( outflow - inflow ) * 43560 / 3600 / 24 );
which forces the division to be done with floating point arithmetic; and thereafter, each one of the divisions is done in floating point.
This is the first code I've ever written. Here's a part of my code which is meant to calculate the side lengths of a cube and tetrahedron after the user gives a value for the volume, however The output is incorrect. I'm pretty sure I have the equations correct, maybe I'm using the Math.pow method incorrectly?
System.out.println("Now, please enter a volume for side or diameter calculation, then press ENTER: ");
volume = input.nextDouble();
cubeSide = Math.pow(volume, (1/3));
sphereDiameter = Math.pow(volume / PI * 6, (1/3));
tetSide = SQRT_2 * Math.pow(3 * volume, (1/3));
System.out.println("");
System.out.println("The Side of your CUBE is : " + cubeSide);
System.out.println("");
System.out.println("The Diameter of your SPHERE is : " + sphereDiameter);
System.out.println("");
System.out.println("The Side of your TETRAHEDRON is : " + tetSide);
Any ideas on how to get correct outputs?
1/3 is 0 - when both dividend and divisor are integral, / performs integral division. You want 1.0 / 3 or 1 / 3.0 or 1.0 / 3.0, which evaluate to 0.3333333-ish.
Your question is an instance of this one Division of integers in Java
Bassically, you need to cast the 1/3 part of your Math.pow() to double, because if you don't do that for default it will take the result as an Integer (always 0).
For example:
double volume = 15.34;
double fraction = (double) 1/3;
double cubeSide = Math.pow(volume,fraction);
System.out.println(cubeSide);
Output is
2.4847066359757295
Otherwise output is always 1.0. Which is the result of any number rised to the zero.
As stated in your comment, when the input is:
1000
the output should be a whole:
10
But actually its:
9.999999999999998
The simplest solution to that could be just:
float roundedValue = Math.round(cubeSide);
And say: that's not my problem. But we want to understand what that's happening. As most things in this world, you are not the first one to face this problem. Let's do some research and find that there in StackOverFlow it have been asked:
Floating point arithmetic not producing exact results
Is floating point math broken?
In the first link, its suggested to read What Every Computer Scientist Should Know About Floating-Point Arithmetic.
I wont repeat what those wise people whom know a lot more than me said, so I highly recommend to you to read the above links.
I've been reviewing my Java for a class I've taken for the whole year and haven't been doing well on. They have a separate review book called "Be Prepared." I want to see if I get the math behind this line.
result = (1 / 2) * n * (n + 1); // result is 0.0
The thing is, this is basic basic basic. I need to know if I'm actually getting this. It looks like my like (1/2) is 0.5. That cast to an int is 0. That's why the whole thing is 0.0.
Am I right?
This book is how you get ready for the AP test. Anyone done eimacs who can help?
(1 / 2) will return zero. both are integer and as per integer calculation it will return zero.
Try
result = (1.0 / 2) * n * (n + 1);
Please have a look at :
Division of integers in Java
How do math equations work in Java?
In Java the result of each operation is decided by the higher type involved in calculation. It doesn't matter in what type are you assigning the result.
for e.g
double d = 10/3;
the value of d will be 3.0 only not 3.33.
Yes, and note that 999 / 1000 will also return 0. It's truncation, not rounding down.
I have two integer values, x and total. I am trying to find the percentage of x in total as an integer. This is how I am doing it right now:
percentage = (int)((x*100)/total);
The percentage must be an integer. When I do this it always rounds the decimal point down. Is there a simple way to calculate the percentage as an integer so it rounds up if the decimal is .5 or higher?
Use Math.round(x * 100.0/total). But note that this returns a long, so a simple cast to int will be required.
I put 100.0 to force it to use floating point arithmetic prior to the rounding.
(int)Math.round(100.0 / total * x);
should work.
Just use Math.round(100.0 / total * x);
Why not use Math.round((x*100)/total);
Use the standard math library in Java.
percentage = Math.round(*your expression here*);
You can add 0.5 for positive values (round to infitity)
percentage = (int)(x * 100.0 / total + 0.5);
This is faster than using Math.round, but possibly not as clear.
BTW: 100.0 / total * x might not give the same result as x * 100.0 / total as the order of evaluation can change the result for floating point.
Simplest Method -
Question : How to round up value of a decimal number(eg : int n = 4.57)
Answer : Math.round(n);
Output : 5 .
I am trying to get percentage but the result is error, i have expression as:
Uper=(Upcount/total)*100;
where Uper is float while Upcount and total is integer i am getting the result Uper=0.
An int divided by an int will result in an int. That could be 0. Multiply 0 * 100, convert to float, and the result is still 0.0. You need at least one of the operands to be floating point before the division will give a floating point result.
Try:
Uper = ((float)Upcount/(float)total)*100.0;
The extra (float) is me being paranoid that this line might be modified in the future without fully understanding the floating-point requirement. The 100.0 is to be explicit about what you want -- a floating point result.
Perhaps changing Upcount or total to float would make more sense.
the division of 2 integers will always result in an integer which is 0 in your case.
To solve this, use the following code:
Uper = ((Double) Upcount) / total * 100
Casting at least 1 member to Double or Float will get the result you want