Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
in C language
float a=0.1;
if(a==.1)
printf("hello");
else
printf("123");
Output is 123
But for Java
float a=0.1F;
if(a==.1)
System.out.println("hello");
else
System.out.println("123");
Ans is hello.
Why?
Here comparison by == first converts float to double and than compares both value.
float real = 0.1f;
double real2 = real;
System.out.println(real2);
OUTPUT
0.10000000149011612
Now you can see when you convert float to double for value 0.1 you will not get the exact value here. Here system will convert your float value to double with extra precision in data.
Same thing happens here when you write if(a==.1) your a will be converted to something like 0.10000000149011612 and than compares with 0.1 which is already double and has exact value 0.1 and so it result to false and must print 123 and not hello that I am sure about.
You're comparing apples and oranges. In the Java case, you should be using
if (a == 0.1F)
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I don't know why java is rounding result. I used casting to float, I was adding '.0f'. Nothing want to work. I know that double is better for dividing but I don't need very precision result.
int A = Integer.parseInt(listBytesAnsw.get(2), 16); //ex. 18
int B = Integer.parseInt(listBytesAnsw.get(3), 16); //ex. 226
float rpm = (float) (A*255+B)/4; //Ans=1204 wrong, should be 1203.75
float rpm = (float) (A*255.0f+B)/4.0f; //dont work still 1204
The result you get is to be expected. After all:
255*226 = 4816
4816 / 4 = 1204
Of course, rounding and then casting would not work in case you have indeed a non-integer result. So look at the following code
System.out.println("(float)(7/4)=" + ((float)(7/4)));
System.out.println("(float)7/4=" + ((float)7/4));
System.out.println("(7+0.0f)/4=" + ((7+0.0f)/4));
System.out.println("7/(4+0.0f)=" + (7/(4+0.0f)));
results in
(float)(7/4)=1.0
(float)7/4=1.75
(7+0.0f)/4=1.75
7/(4+0.0f)=1.75
The first does not work as you found out. But either of the other solutions works.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Hi I'm trying to subtract an int from a char but I keep being told that the compiler "cannot convert from int to char".
I have tried changing the constant to a char but it didn't help.
is there any easy way to do this subtraction?
test[1] = characterArray[1] - ASCII_SUB;
Any help much would be appreciated.
The problem is that subtraction is never performed on char values in Java. Instead, both operands are promoted to int (via binary numeric promotion), and the result of the subtraction is an int as well. So you'll need to cast the result back to char:
test[1] = (char) (characterArray[1] - ASCII_SUB);
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
If I try to get the sin and sin-1 of 0.5 degree, I get virtually the same answer.
sin-1(0.5) should have been 30.
Log.d("XYZ", Math.sin(Math.toRadians(0.5)) +"___"+ Math.asin(Math.toRadians(0.5)));
Result: 0.008726535498373935___0.008726757025787037
Any idea what I am doing wrong?
One of the part of your code i.e. Math.asin(Math.toRadians(0.5)) is wrong....
0.5 is a numerical value you don't need to change it to Radians the correct one is.
Log.d("XYZ", Math.sin(Math.toRadians(0.5)) +"___"+ Math.toDegrees(Math.asin(0.5));
the function "Math.asin(0.5)" give value in radians so you need to convert it to degrees.
and it is not "sin-1 of 0.5 degrees" rather it is" sin-1 of 0.5" ..."degrees" must not come...
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
How to round up a decimal number to a whole number?
So for example when it is 1.25 rounds to 1 or -3.25 rounds to -3 etc
if you want decimal part do this .
double value = 1.25;
int i = (int)value;
if you want to round value , do this
Math.round(value);
Use Math.round(float number) method http://docs.oracle.com/javase/6/docs/api/java/lang/Math.html#round(float) if you want to round it. If you want to cut decimal part just cast it to int.
A small trick I've learned during the years is just adding the value 0.5 to the value you want to round. After you did that, you can round it to an integer.
It's a generic workaround, which will work with alot of programming languages.
I dont know much about Java, but I think you will find a round-Method in the API. The package should be Math.
Edit:\
To round UP you could just add 1 instead of the value 0.5.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I've array of doubles and I'm sorting it using Arrays.sort problem is that in the output array I've something like this(this is array output after reverse):
0.002385171991295645
...
9.914204103773398E-4
...
1.00139601969068E-4
9.975711760353395E-5
and so on, as we can see shortest number are at the top and longest are at the bottom. Numbers in given range e.g. E-4 are sorted good. I've even created a sample code to test this:
double l1 = 0.002385171991295645;
double l2 = 9.914204103773398E-4;
if(l1 > l2) {
System.out.println("TRUE");
} else {
System.out.println("FALSE");
}
And it gives me "TRUE", how to sort this array?
You probably don't know what's the meaning of that E-4 at the end.
9.914204103773398E-4
Is actually
9.914204103773398 * 10^-4
Which is smaller than 0.002385171991295645.
For more details, visit the JLS - 3.10.2. Floating-Point Literals.
You are getting confused by the scientific notation (also known as standard form).
9.914204103773398E-4 means 9.91... x 10^(-4), i.e. 0.0009914.... So this value is indeed smaller than 0.0023...
The "e" or "E" means "exponent," which denotes scientific notation.
Here's a useful example from the primitive data types tutorial on Oracle.com:
double d1 = 123.4;
double d2 = 1.234e2;
Those are both the same number. The "e" means, basically, 10^x (10 raised to the power of x) where the number that follows is x. So in the above, 1.234 * 10^2. 10^2 is 100, so 1.234 * 100 is 1.234.
So looking at one of your numbers, 9.914204103773398E-4, that's 9.914204103773398 * 10^-4. 10^-4 is 0.0001, so 9.914204103773398 * 0.0001, which is 0.0009914103773398.