unexpected results (double vs float vs int) - android

i'm preforming a calculation (converting gigabytes to bytes) but i'm not get the expected results..i believe the calculations and logic is correct but i think it's the way i'm storing my number. i've played with using int, double, float and can't get the right format. i've set the text using toString, valueOf and String.format.
i'm getting 1.0737418E9 instead of 1073741824
relative code below: Thanks!
double sumB;
double sumG;
// get values
Bytes = (EditText)findViewById(R.id.numBytes);
Gigs = (EditText)findViewById(R.id.numGigs);
//calculations
sumB = Double.parseDouble(Gigs.getText().toString()) * 1073741824; //couldn'f figure out how to use exponents
sumG = Double.parseDouble(Gigs.getText().toString()) * 1;
//set text
//Bytes.setText(Integer.toString(sumB));
// Bytes.setText(Double.toString(sumB)); //didnt work
// Bytes.setText(String.format("%.8s", sumB)); //didnt work
Bytes.setText(String.valueOf(sumB));
Gigs.setText(Double.toString(sumG));

Related

Square Root formula in Calculator

Ok I am having an issue trying to put a square root equation in a calculator. I am trying to figure out the expression builder. I know the expression builder takes the math operations of add, subtract, multiply, divide, equals and the parenthesis. What I am doing is trying to build the square root section. I have a simple Percent code to help with the square root.
In the Square root vs the Percent you see I am using binding. So here is the code for both.
On the square root is it possible to use the expression builder? I know there is no absolute formula for square root except for a number that is multipliable with itself like the number 4.
Sqrt(4) = 2
binding.btnSqrt.setOnClickListener {
var square = (tv_equation.text.toString().toDouble() / 2)
binding.tvResult.text = square.toString()
}
So in the event you a non square equation
sqrt(23) = 4.79
How would I simulate that as one function within the button. Can I use expression or would I need to use Kotlin.math
So between the two I divide by 100 on the percent. It works great.
binding.btnPercent.setOnClickListener {
var percentage = (tv_equation.text.toString().toDouble() / 100)
binding.tvResult.text = percentage.toString()
}
All my other buttons work fine and so I am just working on the square root before I can release this to google play.
You would need to use some form of a square root function.
AS you mentioned in your question Kotlin Sqrt Function is a very suitable choice
binding.btnSqrt.setOnClickListener {
if(!tv_equation.text.isNullOrEmpty){
var number = tv_equation.text.toString().toDouble()
binding.tvResult.text = sqrt(number).toString()
}
You can create a sqrt function using the Quake's first inverse square root.
Quake's first inverse square root.
Pseudo Code:
float InvSqrt(float x){
float xhalf = 0.5f * x;
int i = *(int*)&x; // store floating-point bits in integer
i = 0x5f3759df - (i >> 1); // initial guess for Newton's method
x = *(float*)&i; // convert new bits into float
x = x*(1.5f - xhalf*x*x); // One round of Newton's method
return x;
}
So the answer i want to add is a little more suitable with the previous answer. Maybe this will help some people in the future. This solution will also limit the decimal space to 4 decimals.
binding.btnSqrt.setOnClickListener{
val df = DecimalFormat("#.####")
if(!tv_equation.text.isNullOrEmpty())
{
val number = tv_equation.text.toString().toDouble()
binding.tvResult.text = df.format(sqrt(number))
}
}
you can adjust the val df = DecimalFormat("#.####") where the # to as many decimals as you would want.

getting result of decimal number

I am trying to divide two integer and get a decimal number
I am keep getting 0 result when I divide 10/29
I would like to get 0.34
my code is :
private int totalCount_Games;
private int totalCount_Hints_Statistics;
double avgHints;
avgHints=totalCount_Hints_Statistics/totalCount_Games;
In Java, when you divide two integers, the result is another integer.
In your case, 10/29 will result in 0 as you mentioned. If you want to get the results of these in floating digits, then change the above two integers to float or double.
In that case, the result for the above calculation will be 0.34.
PS: This is really basic. You should do more research in the official java site for documentation on datatypes.
The result of a int division is another int, rounded.
Casting int to double (or float) in the expression part will make the division occurs on doubles instead of int, note that this is different from casting the result from the int division to double.
int i = 5, b = 10;
double result = ((double)i)/((double)b);
result is 0.5
The above code will result in 0.0 as int/int will always be a int which is further type casted into double thus output is 0.0
Use the below code, Using big decimal for Rounding
double totalCount_Games_double = totalCount_Games;
double totalCount_Hints_Statistics_double = totalCount_Hints_Statistics;
double value = totalCount_Hints_Statistics/totalCount_Games_double;
BigDecimal bd = new BigDecimal(value);
bd = bd.setScale(2, RoundingMode.HALF_UP);

Android Floating point multiplication off by a factor 0.004739

Can anyone explain to me why the speed calculation is off?
It is a floating point calculation. I show (below) in the log print all the factors involved.
According to my hand calculator:
length: sqrt(3.497924^2 + (-1.134711)^2) = 3.67736881
speed: 1.05 * 3.67736881 = 3.86123725
this is off by a factor 4.739 10^-3 from the printed result 3.879624
The code:
float factor = yTouch(paddle, ballBounds);
float speed = SPEED_TRANSFER[level] * PointF.length(mSpeed.x, mSpeed.y);
double alpha = Math.PI/2;
double beta = Math.atan2(mSpeed.y, mSpeed.x);
double tau = -(2*alpha+beta);
Log.v("debug", String.format("speed(%f) mSpeed(%f,%f) transfer(%2.2f)
length(%f) Original tau(%3.2f)", speed, mSpeed.x, mSpeed.y,
SPEED_TRANSFER[level], PointF.length(mSpeed.x, mSpeed.y), tau/Math.PI));
The Log:
03-24 10:48:53.421: V/debug(3236): speed(3.879624) mSpeed(3.497924,-1.134711) transfer(1.05) length(3.677369) Original tau(-0.90)
Edit
I tried this, the direct multiplication is right.
Log.v("debug", "Direct multiplication:" + (1.05f * 3.677369f) );
03-24 11:49:08.687: V/debug(7644): Direct multiplication:3.8612373
Can you print more digits of SPEED_TRANSFER[level] in your log line? You're looking at an error in the 3rd decimal place, but only showing 2 places of one of the factors. If SPEED_TRANSFER[level] == 1.055 for instance the result is correct (although I would have expected this to print as 1.06 in your Log line, but that's another matter).

Android ExifInterface TAG_GPS_LATITUDE denom values

I am trying to use geo-tagging with my own camera application. What I am doing is getting my current geo-location as decimal value (Ex. Latitude = 6.8447375) and want to convert it in to DMS format in order to use public static final String TAG_GPS_LATITUDE field in the ExifInterface. According to the Android documentation I need to give denominators (as in documentation denom1, denom2, denom3 ) What are the correct values that I have to use for those denominators? Is there any standard method to calculate those denominators. When I use denom1=1, denom2=1 and denom3=1000 I get different location near my actual location. How can I increase the accuracy ?
Are you providing the correct numerator values to go with those denominators?
num1/denom1 = degrees
num2/denom2 = minutes
num3/denom3 = seconds
I've witnessed most cameras encode the values 1,1,1000 for the denominators.
Let's use your sample value and convert it into accurate rational values:
6.8447375 degrees
Here are the steps:
1) Take the whole part of the angle
num1 = 6 / denom1 = 1 --> 6 degrees
2) Multiply the fractional part by 60 and then take the whole part of that:
0.8447375 * 60 = 50.68425
num2 = 50 / denom2 = 1 --> 50 minutes
3) Subtract 6 deg 50' (6.833333333) from your original value = 0.0114041667, then multiply by 3600000 (3600 seconds per degree x 1000)
num3 = 41055 / denom3 = 1000 -> 41.055 seconds
Your position is now encoded as 6 deg, 50' 41.055"

how to use a double value in power function

i am trying to find the power of a value.But the problem is my exponent is a fractional value.power function does not suppporting any datatype other than int.
BigDecimal fd_returns_at_time_of_replace=(BigDecimal.valueOf(capitalDiff).multiply((BigDecimal.valueOf((long)constant1+.09)).pow(temp)));
here temp is a fractional value.given below is the eror message i am getting.
The method pow(int) in the type BigDecimal is not applicable for the arguments (double)
please anybody help me to do this.
BigDecimal.pow() only takes an int. To see a cool example of writing BigDecimal.pow() that accepts a double, see this question How to do a fractional power on BigDecimal in Java?
Common Sense
Consider you want to raise the number x to the power y
If both are integers:
for(int i=0 ; i<y ; i++)
answer = answer * x;
Problems are only when y is a decimal!
So we first change y to the form of y = n + 1/d
How to do that:
n = floor of y
d = 1 / (y - n) << integer
Now x^y = x^n * x^1/d
x to the power n is simple using the usual method
x to the power 1/d is simply the d th root of x
Note: You can increase the precision of your function by reducing the error factor induced by makind d an integer. How! 1/d can be multiplied by powers of 10.

Categories

Resources