Hey guys i have problem making percentege of double number.
double pom = cumulativeProbability(GetZ(baby));
Log.d("Pom","" + pom);
double pom1 = round(cumulativeProbability(GetZ(baby)),2);
Log.d("Pom1", "" + pom1);
double pom2 = pom1 * 100;
Log.d("Pom2: ", "" + pom2);
Here are logs:
06-05 02:30:04.574 13208-13208/com.bojan.readfromcsv D/Pom﹕ 0.5821510290598115
06-05 02:30:04.584 13208-13208/com.bojan.readfromcsv D/Pom1﹕ 0.58
06-05 02:30:04.584 13208-13208/com.bojan.readfromcsv D/Pom2:﹕ 57.99999999999999
I want to round this given pom value into two decimals and that i get with this round function:
public static double round(double value, int places) {
if (places < 0) throw new IllegalArgumentException();
BigDecimal bd = new BigDecimal(value);
bd = bd.setScale(places, RoundingMode.HALF_UP);
return bd.doubleValue();
}
For final result pom2 I want to have 58 %.... Tnx
Hopefully this will help:
public class t {
public static void main(String args[]) {
double pom = 0.5821510290598115;
System.out.println("pom = "+pom);
double pom1 = Math.round(pom*100)/100.0;
System.out.println("pom1 = "+pom1);
System.out.println("pom2 = "+Math.round(pom1*100));
}
}
Which produces:
$ java t
pom = 0.5821510290598115
pom1 = 0.58
pom2 = 58
If you want to preserve some more digits in pom2, say 2, do this:
System.out.println("pom2 = "+Math.round(pom1*100*100)/100.0);
Try:
double pom2 = round(pom1 * 100, 0);
EDIT:
int pom2 = (int) round(pom1 * 100, 0);
I assume you aren't having to write your own round function, so why not use:
pom2 = (int) Math.round(pom1 * 100);
Related
I'm having difficulty even beginning to solve this problem. All examples that I have found are either too simple or way too complex to digest.
I want to to find the value S given a series of inputs. The function is univariate but non-linear. S will always be between -3 .. 3.
I would like to use the Apache Commons library, as I have had prior experience in other sections of that code.
For each time I want to solve my problem, I know the following information:
double R =250.0;
double om1 = 5.0;
double om2 = 15.0;
double th21 = 29.07965;
double th22 = 29.69008;
double D_obs = th21 - th22;
The actual values will change between solutions, but they are fixed for any one particular solution.
The value I want to find is:
double S = 0.0;
such that
double d1 = delta(R,om1,th21,S);
double d2 = delta(R,om2,th22,S);
double D_calc = d1 - d2;
have values to make
double minme = Math.abs(D_obs - D_calc);
a minimum, or alternately, solve
double minme = D_obs - D_calc;
where minme=0.
The function delta is defined as
public static double delta(double R, double om, double th2, double s)
{
if(Math.abs(s) <= 0.0001) //is the displacement == 0?
{
return 0.0;
}
else
{
return Math.toDegrees((-1*Cos(th2)*s-R*Sin(om)+Sqrt(-1*Math.pow(Cos(th2),2)*Math.pow(s,2)+2*Cos(th2)*Sin(om)*R*s-Math.pow(Cos(om),2)*Math.pow(R,2)+Math.pow(R,2)+2*Math.pow(s,2)))/(Sin(th2)*s));
}
}
where, for example, Cosis defined elsewhere as Math.cos(Math.toRadians(val))
Where/what can I read/do to get a start on this problem?
I found an answer I could work with: Newton-Raphson method using the Math.Commons library
The key code is
public static void main(String args[])
{
//setup all variables
final double R =(new Double(args[0])).doubleValue(); //=250.0;
final double om1 =(new Double(args[1])).doubleValue(); //= 5.0;
final double om2 =(new Double(args[2])).doubleValue(); //= 15.0;
final double th21=(new Double(args[3])).doubleValue(); //= 29.07965;
final double th22=(new Double(args[4])).doubleValue(); //= 29.69008;
final double D_obs = th21 - th22;
BisectionSolver solver = new BisectionSolver();
UnivariateFunction f = new UnivariateFunction()
{
public double value(double s) {
return ((delta(R,om1,th21,s)-delta(R,om2,th22,s)) - (D_obs));
}
};
System.out.printf("The speciment offset is %.3f mm.\n", solver.solve(1000, f, -3, 3));
}
I'm currently trying to add some JUnit tests to my pathdrawing system in order to check if the calculations are right...
Currently I have:
Class to Test(MapRouteDrawer.java):
protected static double[] getCoords(PolynomialSplineFunction curve, double[] index) {
final double detailLevel = 1.0;//Tweak this value in order to achieve good rendering results
final double defaultCoordSize = index[index.length - 1];
final double[] coords = new double[(int)Math.round(detailLevel * defaultCoordSize) + 1];
final double stepSize = curve.getKnots()[curve.getKnots().length - 1] / coords.length;
double curValue = 0;
for (int i = 0; i < coords.length; i ++) {
coords[i] = curve.value(curValue);//gets y value of specified x value
curValue += stepSize;
}
return coords;
}
protected static double[] getIndex(Point[] points) {
final double[] index = new double[points.length];
if(index.length > 0){
index[0] = 0;
}
for (int i = 1; i < points.length; i++) {
index[i] = index[i - 1] + Math.sqrt(points[i - 1].distance(points[i]));
}
return index;
}
TestClass:
private Point[] dummyPoints = new Point[]{new Point(0,0), new Point(100,0), new Point(0,100)};//Some Points for Testing
//This returns a so called index - the squareroot distance between addedn on top of each other
private double[] dummyIndex = MapRouteDrawer.getIndex(dummyPoints);
#Test
public void testCurve(){
final double[] testYValues = new double[]{20, 40, 90};
final PolynomialSplineFunction testFunction = new SplineInterpolator().interpolate(dummyIndex, testYValues);//get a Spline-Interpolated curve
final double[] coords = MapRouteDrawer.getCoords(testFunction, dummyIndex);//Calls the function to test
final double factor = testFunction.getKnots()[testFunction.getKnots().length - 1] / coords.length;
assertEquals(testYValues[0] * factor, coords[(int)Math.round(dummyIndex[0])], 1);//Check if the coordinates are equal
assertEquals(testYValues[1] * factor, coords[(int)Math.round(dummyIndex[1])], 1);
assertEquals(testYValues[2] * factor, coords[(int)Math.round(dummyIndex[2])], 1);
}
This is working fine, but if you are familiar with JUnit you may notice the delta of 1 which is neccesary in order for this Test to work...
What I'm trying to achieve is this:
#Test
public void testCurve(){
final double[] testYValues = new double[]{20, 40, 90};
final PolynomialSplineFunction testFunction = new SplineInterpolator().interpolate(dummyIndex, testYValues);//get a Spline-Interpolated curve
final double[] coords = MapRouteDrawer.getCoords(testFunction, dummyIndex);//Calls the function to test
final double factor;//Unknown... should be dynamic, so it does not matter which detail level I chose
assertEquals(testYValues[0], coords[(int)Math.round(dummyIndex[0])] * factor, 0);//Check if the coordinates are equal
assertEquals(testYValues[1], coords[(int)Math.round(dummyIndex[1])] * factor, 0);//e.g. coords[(int)Math.round(dummyIndex[0])] * factor == 20 etc.
assertEquals(testYValues[2], coords[(int)Math.round(dummyIndex[2])] * factor, 0);
}
So that the first value in assertEquals() is 20, 40, 90 etc. and not a weird 21.39587576787686 (or similar) number and the delta is 0 (or 0.01 if there is no other way) and not 1 which I'm currently using
Because I'm using a detail level in my getCoords() method, which should be able to be adjusted without having to change the test, there is a need to make the "factor" in my test related to the coords-size.
How would I calculate the factor in order for the Test to succeed?
Any help is would be very much appreciated
I am writing ESRI geometry compression algorithm in JAVA, I am using this link.
Using instructions mentioned in provided link, this is my current code:
public static void main(String[] args) {
String dirPoints = "-118.356654545455,34.1146;-118.356436363636,34.1143272727273;-118.356418181818,34.1142363636364;-118.356490909091,34.1137636363636";
String compressedGeometry = "";
double xPointPrev = 0.0;
double yPointPrev = 0.0;
int coefficient = 55000;
String coefficient_32 = Integer.toString(coefficient, 32);
compressedGeometry = coefficient_32 + compressedGeometry;
String[] path_XY = dirPoints.split(";");
for (int i = 0, leni = path_XY.length; i < leni; i++) {
String[] xy = path_XY[i].split(",");
double pointX = Double.parseDouble(xy[0].trim());
double pointY = Double.parseDouble(xy[1].trim());
int xDifference = (int) Math.round(coefficient * (pointX - xPointPrev));
int yDifference = (int) Math.round(coefficient * (pointY - yPointPrev));
String xDifference_32 = Integer.toString(xDifference, 32);
compressedGeometry += xDifference_32;
String yDifference_32 = Integer.toString(yDifference, 32);
compressedGeometry += yDifference_32;
xPointPrev = pointX;
yPointPrev = pointY;
}
System.out.println(compressedGeometry);
}
Expected output: "+1lmo-66l1f+1p8af+c-f+1-5-4-q"
But I am getting this: "1lmo-66l1g1p8afc-f1-5-4-q"
What am I missing? Any help is highly appreciated.
Thanks,
According to Integer.toString()
"If the first argument is not negative, no sign character appears in the result."
Therefore I think you need to add the "+".
(And I think the f-g there is their typo).
I am trying to create a visual grid of this http://www.ibm.com/developerworks/library/j-coordconvert/ -Military Grid Reference System. I have the latitude/longitude to UTM and also to MGRS...which ar
17 T 330649 4689666
17TLG3064989666
But when going from MGRS to latitude I get the following:
[D#18e3f02a
public class CoordinateConversion {
public static void main(String args[]) {
CoordinateConversion test = new CoordinateConversion();
CoordinateConversion test2 = new CoordinateConversion();
test.latLon2UTM(35.58, 82.56);
System.out.println(test.latLon2UTM(42.340837, -83.055821));
System.out.println();
test2.latLon2UTM(35.58, 82.56);
System.out.println(test2.latLon2MGRUTM(42.340837, -83.055821));
CoordinateConversion test3 = new CoordinateConversion();
test3.latLon2UTM(35.58, 82.56);
//System.out.print(test3.mgrutm2LatLong(42.340837, -83.055821));
//System.out.println(test3.mgrutm2LatLong("02CNR0634657742"));
MGRUTM2LatLon mg = new MGRUTM2LatLon();
//mg.convertMGRUTMToLatLong("02CNR0634657742");
String MGRUTM = "17TLG3064989666";
System.out.println(mg.convertMGRUTMToLatLong(MGRUTM));
//for loop to be developed
}
public double[] utm2LatLon(String UTM) {
UTM2LatLon c = new UTM2LatLon();
return c.convertUTMToLatLong(UTM);
}
public double[] mgrutm2LatLon(String MGRUTM) {
MGRUTM2LatLon c = new MGRUTM2LatLon();
return c.convertMGRUTMToLatLong(MGRUTM);
}
}
and from this class:
public double[] convertMGRUTMToLatLong(String mgrutm) {
double[] latlon = {0.0, 0.0};
// 02CNR0634657742
int zone = Integer.parseInt(mgrutm.substring(0, 2));
String latZone = mgrutm.substring(2, 3);
String digraph1 = mgrutm.substring(3, 4);
String digraph2 = mgrutm.substring(4, 5);
easting = Double.parseDouble(mgrutm.substring(5, 10));
northing = Double.parseDouble(mgrutm.substring(10, 15));
LatZones lz = new LatZones();
double latZoneDegree = lz.getLatZoneDegree(latZone);
double a1 = latZoneDegree * 40000000 / 360.0;
double a2 = 2000000 * Math.floor(a1 / 2000000.0);
Digraphs digraphs = new Digraphs();
double digraph2Index = digraphs.getDigraph2Index(digraph2);
double startindexEquator = 1;
if ((1 + zone % 2) == 1) {
startindexEquator = 6;
}
double a3 = a2 + (digraph2Index - startindexEquator) * 100000;
if (a3 <= 0) {
a3 = 10000000 + a3;
}
northing = a3 + northing;
zoneCM = -183 + 6 * zone;
double digraph1Index = digraphs.getDigraph1Index(digraph1);
int a5 = 1 + zone % 3;
double[] a6 = {16, 0, 8};
double a7 = 100000 * (digraph1Index - a6[a5 - 1]);
easting = easting + a7;
setVariables();
double latitude = 0;
latitude = 180 * (phi1 - fact1 * (fact2 + fact3 + fact4)) / Math.PI;
if (latZoneDegree < 0) {
latitude = 90 - latitude;
}
double d = _a2 * 180 / Math.PI;
double longitude = zoneCM - d;
if (getHemisphere(latZone).equals("S")) {
latitude = -latitude;
}
latlon[0] = latitude;
latlon[1] = longitude;
return latlon;
}
I am trying not to get into a large library where I will have to learn things that may be time consuming.
So I am trying to loop so I go east (easting) and north (northing) and cannot get past the point where I have one point - latitude/longitude.
Hope I have asked my question clearly without stating too much.
Any help will be appreciated.
Thanks,
-Terry
Your result from convertMGRUTMToLatLong() is an array of doubles, and by default, arrays are converted to String in a rather unreadable format in Java. That's where the [D#18e3f02a comes from. Try System.out.println(Arrays.toString(mg.convertMGRUTMToLatLong(MGRUTM))); and you'll get a more readable output.
In the method convertMGRUTMToLatLong(String s) you are returning an array latlon (i.e an object).
It returns its hashcode which is probably u dont want.
You want to print the array values. So in your main method you replace below line;
System.out.println(mg.convertMGRUTMToLatLong(MGRUTM));
with
double[] a = mg.convertMGRUTMToLatLong(MGRUTM) ;
System.out.println(a[0]+" " + a[1] );
Hope that helps!
So i have this code
BigDecimal bd = new BigDecimal(i);
bd = bd.round(new MathContext(6));
double meters = bd.doubleValue();
double km = bd.doubleValue()*0.001;
double cm = bd.doubleValue()*100;
double mm = bd.doubleValue()*1000;
double miles = bd.doubleValue()*0.000621371192;
double inches = bd.doubleValue()*39.3700787;
double feet = bd.doubleValue()*3.2808399;
double yards = bd.doubleValue()*1.0936133;
double points = bd.doubleValue()*2834.64567;
So i found an example on here that told me to do it like so
BigDecimal bd = new BigDecimal(i);
bd = bd.round(new MathContext(6));
double meters = bd.doubleValue();
and thats what i tried, and it round the meters number however the inches, feet, yards and so on numbers dont get rounded. am i doing this wrong? what would be the right way to go about this?
EDIT: okay so i found something that has worked in case anyone else ever has a problem with it
instead of using :
double points = bd.doubleValue()*2834.64567;
i did this:
BigDecimal points = new BigDecimal(i*2834.64567);
points = points.round(new MathContext(6));
You can use BigDecimal;
For example:
double d = 15.3343243;
BigDecimal bd = new BigDecimal(d);
bd = bd.setScale(2, BigDecimal.ROUND_CEILING);
bd = new BigDecimal(0.001 * bd.doubleValue());
d = bd.doubleValue();
System.out.println(d);
or without BigDecimal objects:
System.out.println((double)((int) (d * 1000)) / 1000);