Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is multiplying by 0.0000001 the same as dividing by 10000000?

In Java, is multiplying a double by 0.0000001 the same as dividing it by 10000000? My intuition is that there could be a difference because 0.0000001 cannot be represented exactly in a double.

like image 270
Frank Kusters Avatar asked Dec 15 '14 15:12

Frank Kusters


People also ask

Why is multiplying by 0.01 the same as dividing by 100?

This is because there are 10 tenths in a whole. Dividing by 0.01 is the same as multiplying by 100. This is because 0.01 is one hundredth and there are a hundred hundredths in a whole.

Is dividing by zero the same as multiplying by infinity?

TO SAY that "any number divided by zero is infinity" is not quite correct. Considering normal arithmetic, it is not possible to divide by zero. This is because "dividing by x" is really just a shorthand way of saying "calculating the amount which gives the original when multiplied by x".

What is the difference between dividing and multiplying decimals by the power of 10?

Multiplying a decimal by 10 moves the decimal point one place to the right. Dividing a decimal by 10 moves the decimal point one place to the left.


1 Answers

No it's not the same for the reason you mentioned. Here's an example:

double x = 894913.3;
System.out.println(x * 0.0000001);    // prints 0.08949133
System.out.println(x / 10000000);     // prints 0.08949133000000001

Using a BigDecimal, we can see the difference between the two values:

System.out.println(new BigDecimal(0.0000001));
System.out.println(new BigDecimal((double)10000000));

Ouput:

9.99999999999999954748111825886258685613938723690807819366455078125E-8
10000000
like image 111
M A Avatar answered Oct 10 '22 01:10

M A