Why does % give different answers when an arithmetic expression has floating points? - java

I recently started learning java and this question keeps on bugging me..
class Example{
public static void main(String args[]){
double d;
d=4.1 % 1.1;
System.out.println("4.1%1.1 : "+d);
d=5.5 % 1.1;
System.out.println("5.5%1.1 : "+d);
}
}
the output for the above program is
4.1%1.1 : 0.7999999999999994 and
5.5%1.1 : 1.0999999999999996
but when we do these calculations by hand, we get 0.8 for the first one and 0 for the second one... why does this happen??
% gives the correct answer when integers are used in the expression..
class Example{
public static void main(String args[]){
int x;
x=10%17;
System.out.println("10%17 : "+x); **//prints 10**
}
}
can anyone pl explain what exactly happens here?

Related

division in java programming

The same formula gives different answers to 2296 and 1500, when the expected answer in both cases is 100. Please explain this behavior. I'm quite surprised by this simple thing. Initially I thought this must be due to operator precedence but I cannot understand this.
program with 2296 :
public class testpercent {
public static void main(String args[]) {
System.out.println("first formula ===>"+(2296 * 100 )/2296);
System.out.println("alternate formula =====>" + (2296/(2296/100)));
}
}
output:
first formula ===>100
alternate formula =====>104
same program with 1500:
public class testpercent {
public static void main(String args[]) {
System.out.println("first formula for ===>"+(1500 * 100 )/1500);
System.out.println("alternate formula for =====>" + (1500/(1500/100)));
}
}
output:
first formula ===>100
alternate formula =====>100
Program with 2296 first does (2296/100) giving 22.96 and drops the remainder of 96. Then does 2296/22 giving 104.
Program with 1500 just does the same but has no remainder and ends up with the "correct" answer.
If you want to get the output right using the alternate formula try using data types like double or float.

Why does double parameterised function accepts float and not vice versa?

I have encountered something strange today. The below code compiles unexpectedly and runs fine.
public class Test {
public static void func(double d) {
System.out.print("d : " + d);
}
public static void main(String[] args) {
float f = 10.0f;
func(f); // output: d : 10.0
}
}
But this one gives compilation error
public class Test {
public static void func(float f) {
System.out.print("f : " + f);
}
public static void main(String[] args) {
double d = 10.0d;
func(d);
}
}
Can somebody please explain this behaviour ?
Type promotion from float to double is safe as no data is lost and all float 4 bytes can fit into double 8 byes.
However the opposite, from double to float, always truncates the data as double 8 bytes can't fit into float 4 bytes. Compiler guards against doing this truncation accidentally by forcing the programmer to manually specify the type conversion.
double(8 byte) is a bigger data type than float(4 byte) so you can store float(4 byte) in double(8 byte) but you can't double in float. If you try to do that you'll get Possible loss of precision error.
So the following will give error.
float f = 120.55;
While this one don't
double d = 120.44f;

Why calculating percentage from double variables return 0.00

The following code will return 0.00 no matter what the argument variables are.
public double updateConjustion(double numberOfAuditors, double numberOfTotalAuditors){
double conjustion = ((numberOfAuditors/numberOfTotalAuditors)*100);
return conjustion;
}
Following code snippet seems to work for me.
public class Test{
public static void main(String []args){
System.out.println(updateConjustion(1.0, 200.0));
System.out.println(updateConjustion(0.5, 5000.0));
System.out.println(updateConjustion(0.99, 3000.0));
System.out.println(updateConjustion(1.0, 3000.0));
System.out.println(updateConjustion(1, 50));
}
public static double updateConjustion(double numberOfAuditors, double numberOfTotalAuditors){
double conjustion = ((numberOfAuditors/numberOfTotalAuditors)*100.0);
return conjustion;
}
}
And here is the output:
0.5
0.01
0.033
0.03333333333333333
2.0
__UPDATE__
So I don't think anything is wrong with the method you provided. You should look elsewhere to find the root of the problem. Possible places to look for are:
How you use this method
How you use the result of that method
If you share these areas with us, we could provide more help.

Wrong output calculating the power

I've the below code.
import java.util.ArrayList;
import java.math.*;
import java.util.Arrays;
import java.util.Collections;
public class Dummy {
public static void main(String args[]) throws Exception {
int n=12;
double val=(3+Math.sqrt(5));
double ne=Math.pow(val, n);
String new2=String.valueOf(ne);
System.out.println(ne);
String[] new1=new2.split("\\.");
if(new1[0].length()>3){
new1[0]=new1[0].substring(Math.max(new1[0].length() - 4, 0));
if(new1[0].length()<3){
new1[0]=("0").concat(new1[0]);
}
else{
new1[0]=new1[0];
}
}
else if(new1[0].length()<2){
new1[0]=("00").concat(new1[0]);
}
else if(new1[0].length()<1){
new1[0]=("000").concat(new1[0]);
}
else if(new1[0].length()<3){
new1[0]=("0").concat(new1[0]);
}
System.out.println(new1[0]);
}
}
here i'm trying to calculate sum of 3 with root 5 and whole to power of 12
(3+sqrt(5))^12
when i do it the result i get is 4.246814719604947E8 but in real the answer is to be 424681471.960494. please let me know where am i going wrong.
It's in the scientific notation.
If you don't want it in this notation, you can try
public static void main(String[] args) {
Double d = Math.pow(3+Math.sqrt(5),12);
System.out.println(d); //4.246814719604947E8
System.out.println(new BigDecimal(d).toPlainString()); //424681471.960494697093963623046875
}
Use this
System.out.println(String.format("%f", ne));
Did you notice the E8 in your answer? Then the answer is correct :)
The answer you are getting is correct.
4.246814719604947E8 means 4.246814719604947 times 10^8. If you move the decimal point 8 places to the right you see the answer you expect.

Java: Syntax error on token "}"? [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
Attempting to write some code to convert feet to meters and meters to feet and achieve the table shown below. This is my first java program and I have no idea why I'm getting syntax errors involving "}".
Feet Meters| Meters Feet
1.0 0.305 | 20.0 65.574
2.0 0.61 | 25.0 81.967
…
9.0 2.745 | 60.0 196.721
10.0 3.05 | 65.0 213.115
Here's what I have...
public class Hmwk {
public static void main(String[] args){
public static double footToMeter(double foot){
return 0.305 * foot;
}
public static double meterToFoot(double meter){
return 3.279 * meter;
}
for (double i = 1.0; i <11; i++){
System.out.printf(i+footToMeter(i)+"|"+(i*5+15)+meterToFoot(i*5+15));}
}}
Any and all help is much appreciated.
Here's a small tip, if you don't like indenting code, then coding might not be for you.
This may sound harsh, but I sincerely believe that before one writes his/her first program, one needs to know what code structure is all about. Unfortunately, schools and courses tend to hide or ignore this fact completely. I know, I've seen this back at secondary school - telling your teacher to properly format code isn't nice believe me.
public class Hmwk {
public static double footToMeter(double foot){
return 0.305 * foot;
}
public static double meterToFoot(double meter){
return 3.279 * meter;
}
public static void main(String[] args){
for (double i = 1.0; i <11; i++){
System.out.printf(i + footToMeter(i) + "|" + (i*5+15) + meterToFoot(i*5+15));
}
}
}
In Java, you cannot have methods inside methods.
Indenting code means you can find your problems easily.
Brackets are meant to stay on separate lines, except the opening brackets (depends on taste)
You have a lot of braces in incorrect positions. Most notably, your footToMeter and meterToFoot are declared inside of your main method, which is incorrect. Here is your code with correct brace placement:
public class Hmwk
{
public static void main(String[] args)
{
for (double i = 1.0; i <11; i++)
{
System.out.printf(i+footToMeter(i)+"|"+(i*5+15)+meterToFoot(i*5+15));
}
}
public static double footToMeter(double foot)
{
return 0.305 * foot;
}
public static double meterToFoot(double meter)
{
return 3.279 * meter;
}
}
As a general rule, methods cannot be nested in Java. This means that no method can be declared inside of another.
No closing brace in the void main. Put '}' before 'public static double footToMeter' and delete closing bracket after closing bracket from for

Categories