Java Double Multiplication explanation? [duplicate] - java

This question already has answers here:
Moving decimal places over in a double
(9 answers)
Closed 9 years ago.
I recently tested something out that I heard using the following code
public static void main(String[] args) {
double x = 4.35 * 100;
System.out.println(x);
}.
I am interested as to why this produces 434.99999999999994 rather than 435.0 . Thanks

When you type:
double x = 4.35;
x is not stored as-is. It is stored in a approaching form (probably 4.349999999 in this case).
If you want exact result, please use BigDecimal.
You can learn about the accuracy problems of floating-point technology.

Related

java double number division giving strange output [duplicate]

This question already has answers here:
Simple division in Java - is this a bug or a feature?
(5 answers)
Closed 7 years ago.
Found below output strange. Is it really?
double rate = 11/12; // outputs 0.0
double rate = 11.00/12; // outputs 0.916666667;
Why so much difference?
In the 1st case first division is done so an int divided by an int gives a integer i.e, 11/12=0 Then this integer is converted to double .ie, 0.0. In the 2nd case a double(11.00) is divided by a integer(12).The integer 12 is then automatically type casted to double as division should occur with similar types. This division gives a double value which is exact in reality(0.916666667)
See this link https://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html

calculating the power in Java [duplicate]

This question already has answers here:
Raising a number to a power in Java
(10 answers)
Closed 8 years ago.
I am trying to calculate the power as below but it is giving me 'bad operands type for binary operator '^'. I am guessing that it is a precedence issue but it still doesn't fix with inserting additional brackets
double pw = ((N - (df + 1))^2);
You should use java.lang.Math.pow(x,y)
Example: java.lang.Math.pow(2,3) returns 8
See this
http://www.tutorialspoint.com/java/lang/math_pow.htm
java.lang.Math.pow(double a, double b)
You can use static import for this.

Printing a simple double number in java [duplicate]

This question already has answers here:
Dividing error in java language
(2 answers)
Closed 8 years ago.
pls enlighten me why the heck is this not working ? :| it's just a simple output line , god i'm so frustated. I know it might be something very that i miss , but i can't figure it out.
double a = 155/124;
System.out.printf("%f\n", a);
it prints
1.00000
it's working just fine. Just remember 2 things:
you're using integers, the result of the calculation will be an integer
rounding errors
Well, you are dividing two ints without casting it to double explicitly.
Add (double) cast to variable a:
double a = (double) 155/124;
Or, you can create one of the numbers to be double, like:
double a = 155/124.0;
Hope this helps.
try this
double a = 155d / 124d ;
System.out.printf("%f\n", a);

Java Deduction gives wrong result [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 8 years ago.
In My Java code I'm trying to do following
double a=1769.58;
double b=986.58;
double c=a-b;
System.out.println("Result "+c);
This is retuning the result as 782.9999999999999. but it should be 783.00 what is wrong with this.how can I get correct value and what is the reason for this?
It's because computer can't stored exactly the good value of floating point.
See this response to learn more about this problem.
try this:
double a=1769.58;
double b=986.58;
int c=(int)a-(int)b;

Why do I receive unhelpful results for subtractions? [duplicate]

This question already has answers here:
Why do I see a double variable initialized to some value like 21.4 as 21.399999618530273?
(14 answers)
Closed 9 years ago.
double foo = 3;
double bar = 2.1;
System.out.println(foo - bar + "");
Output:
0.8999999999999999
Why? Is this some Java joke, which is not understandable for mere mortals?
It isn't a joke. It is floating-point precision error problem. The main gist of this problem is that floating points are represented in base 2 rather than 10, and that the precision of doubles is not arbitrary.
If you want precision, you can use BigDecimal class:

Categories

Resources