Rounded result even casting to float [closed] - java

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.

Related

Why does this hexadecimal value gets different decimal value? [closed]

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 3 years ago.
Improve this question
I want to dynamically set an integer variable using a hexadecimal value, but when I use Integer.parse(hexValue, 16) it gets a different value from setting as int a = 0x04A7D488
For example:
int a = 0x04A7D3B8;
System.out.println("a = " + a); // prints 78107576
int b = Integer.parseInt("04A7D3B8", 16);
System.out.println("b = " + b); // prints 78107784
Why do I get different values? How can I dynamically set variable a with value 0x04A7D3B8?
Note: I've discovered that this error is only happening with Java SDK 1.8.0_171.
Solution
I was trying to convert a negative integer value using Long.parseLong or Integer.parseInt, but the correct solution is using Integer.parseUnsignedInt("FD8914EC");
In my tests, the value FD8914EC was converting to -41347860 (declaring as long a = 0xFD8914EC) or 4253619436 (declaring as long b = Long.parseLong("FD8914EC", 16);), but you have always to use Integer.parseUnsignedInt (the result will be negative if the hexadecimal value starts with F).

Integer.parseInt giving java.lang.NumberFormatException in Java [closed]

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 6 years ago.
Improve this question
I have a character array as
char[] bitsString = new char[16];
bitsString = {'1','1','1','1','1','1','0','1','1','1','1','0','0','1','1','0'};
Then I converted it to the corresponding integer as follows:
int givenNumber = Integer.parseInt(new String(bitsString), 2);
The above logic works fine when number bitstring array length is less than 10. But when it is increased to 11 or more, it is showing me java.lang.NumberFormatException why ?
After some hit and trials I found that, it was the case since the 16 bit value is crossing the Integer limit.
But when it is changed from int to long
long givenNumber = Long.parseLong(new String(bitsString), 2);
it is working perfectly fine. Since in this case long has 64 bits length.

Float comparision [closed]

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)

How to subtract a number(int) from a character in java [closed]

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);

asin() not working properly in Java [closed]

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...

Categories

Resources