This question already has answers here:
Division of integers in Java [duplicate]
(7 answers)
Closed 8 years ago.
Hey guys I'm a new Java learner. I have a problem with this code:
I dont see why it prints only the value of a, and not of ((9/5)*a+32)
for example if a = 50 , ((9/5)*a+32) = 82 but when I compile this it prints a=50 and ((9/5)*a+32)=82.
Scanner tt = new Scanner(System.in);
a = tt.nextInt();
System.out.println(a + " degrees °C match " + ((9/5)*a+32) + "°F");
Can someone explain this to me?!
9/5 is being done using integer maths. In integer, 9/5 is 1.
Try:
(9*a)/5+32
or:
(int)((9.0/5)*a+32)
try using
1.80 * a + 32
or
9 / 5.0 * a + 32
what happens in your code is that you are dividing an integer by integer and
integer / integer = integer
it leads to precision loss..so you have to convert it to float or double type.
You are performing integer division, I believe you wanted
System.out.println(a + " degrees °C match "
+ ((((double) 9) / 5) * a + 32) + "°F");
Related
This question already has answers here:
Float and double datatype in Java
(9 answers)
Closed 4 years ago.
To my knowledge, they both do the same thing, which is generate a value between 0.0 and 1.0, right?
One return a float, one return a double.
Just try this:
Random random = new Random();
System.out.println("nextFloat: " + random.nextFloat() + ", nextDouble: " + random.nextDouble());
that returns:
nextFloat: 0.9613963, nextDouble: 0.9364254125546306
I am having a hard time figuring out the answer to a homework assignment for Programming 1 class. The assignment is prompt a user for input (up to 4 bits) in binary, and convert it to the decimal equivalent. Using loops, conditional statements, ParseInt, and anything other than the modulus operator and other math operators are not allowed.
I am having trouble with the mathematical aspect, I think once I understand how to use the modulus operator to answer the question I would be able to write the code for it.
I have searched and have not been able to find anything that was able to help.
You should be getting the number values of each position and add them using the power of 2 to get back the original number.
double num = 1110;
double ones = Math.floor(num % 10);
double tens = Math.floor(num/10 % 10);
double hundreds = Math.floor(num/100 % 10);
double thousands = Math.floor(num %10000 /1000);
double tenThousands = Math.floor(num / 10000 % 10);
double original = (ones * 1) +
(tens * 2) +
(hundreds * 4) +
(thousands * 8);
System.out.println(num);
System.out.println("ones: " +ones);
System.out.println("tens: " +tens);
System.out.println("hundreds: " +hundreds);
System.out.println("thousands: " + thousands);
System.out.println("original number : " + original);
This question already has answers here:
Multiplication operation in Java is resulting in negative value
(4 answers)
Closed 6 years ago.
Relatively new to Java and possibly some stupid question. Here is the code:
long a = 3232235521L;
long b = 192 * 16777216 + 168 * 65536 + 0 * 256 + 1;
System.out.println("a="+a);
System.out.println("b="+b);
The output:
a=3232235521
b=-1062731775
According to Java documentation max value for long 2^63-1 and that is: 9223372036854775807. So for b, there is no overflow, so why b isn't 3232235521?
In line 2 of your code, all your operands are integers, which is why the results of the operations will also be an integer.
Since the result(3,232,235,521) will not fit inside an integer(max value being 2^31 - 1), this results in an integer overflow, which is why you are getting the negative result.
So, you will need to use Long literals to get an accurate result. Change Line 2 to the below code.
long b = 192L * 16777216L + 168L * 65536L + 0L * 256L + 1L;
The above code should give you the correct output.
You are using integer primitives during the math and it's only converted at the end, after the integer overflow. You might want to use 192L * 16777216L + 168L * 65536L + 0L * 256L + 1L;
You are only doing the conversion to long on the assignment - up until that point everything is an integer and that is why you are seeing an overflow halfway through your calculation.
The code should be:
long b = 192l * 16777216l + 168l * 65536l + 0l * 256l + 1l;
This question already has answers here:
Why does the division of two integers return 0.0 in Java? [duplicate]
(6 answers)
Closed 8 years ago.
So this is a portion of the code I'm using, we were only supposed to begin by fixing some logical errors because of wrong answers. I found a couple of syntax errors as well that wouldn't let the compiler finish up...I keep getting "0" for fToC...
//declaration statements
final int BOILING_IN_F = 212;
int fToC;
string output;
//there is other code between that has nothing
//to do with outputting the boiling point.
fToC = (5/9) * (BOILING_IN_F - 32);
output = BOILING_IN_F + " in Fahrenheit is " + fToC
+ " in Celsius.";
System.out.println(output); //this is outputting 0 for fToC
System.out.println();
Answers were useful, except I needed to declare fToC as a double before compiling and running the program. Thank you.
You are performing Java's integer division with 5/9, which must result in another integer -- 0, so the result is 0. Use double literals to force floating-point math.
fToC = (5.0/9.0) * (BOILING_IN_F - 32);
You'll need fToC to be a double (and change the typo string output to be String output).
(5/9) is 0 due to int division. Try (5.0/9)
The full expression should be :
fToC = (5.0/9) * (BOILING_IN_F - 32);
(5/9) You're diving two integers, so this is truncated, so it equals 0. And you're multiplying that 0 with another number.
Use instead (5.0f/9.0f) to precise the fact that your number are actually decimal numbers.
This question already has answers here:
Integer division: How do you produce a double?
(11 answers)
Closed 7 years ago.
I am dividing two int values, and im expecting to get a double one. but it works very strange, it has the correct values just before division but it doesnt give the right answer.
public void Analyse() {
for (FlowPacket fp : this.flow.GetAll()) {
if (fp.direction==1){
this.sentPackets++;
this.sentLength = this.sentLength + fp.packetLength;
}
else{
this.receivedPackets++;
this.receivedLength = this.receivedLength + fp.packetLength;
}
}
if(this.receivedPackets==0)
this.receivedPackets = 1;
}
public double CalcRatio() {
return (this.sentPackets/this.receivedPackets);
}
----------------------------main--------------------------------
System.out.print("Sent packets: " + analyser.getTotalSentPackets() + " , ");
System.out.print("Received packets: " + analyser.getTotalReceivedPackets() + " , ");
System.out.print("ratio: " + analyser.CalcRatio() + " , ");
----------------------------outout------------------------------
Sent packets: 2694 , Received packets: 5753 , ratio: 0
(double)this.sentPackets/this.receivedPackets
... should fix it.
The result of the division is cast to a double AFTER integer division (with rounding down) is performed. Cast one of the integers to a double BEFORE dividing so that double division occurs.
When dividing an int by an int the answer will be an int. Therefor it will cut off any remainder there may be in the answer.
In order to get a double answer you must cast one of the ints to a double.
Cast at least one of the ints to (double) before dividing.
Need to cast to double...
public double CalcRatio() {
return ( (double) this.sentPackets/ (double) this.receivedPackets);
}
It is not only Java-specific behaviour. It works the same way in .NET, too.