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.
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.
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".
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With