0

Why this calculation remainder is not giving me 0.0 as the result?

I tried in Java and Javascript and the result is always the same:

double number = 270.48;
double divisor = 90.16;

System.out.println( (number % divisor) );

The result is always 2.8421709430404007E-14 but it should be 0.0

Francisco Souza
  • 806
  • 15
  • 38
  • Same reason that 1 / 3 won't give you a number that is exactly equals to a third of the unit. Computers are not capable to represent the mathematical continuous "real" numbers, and instead provide approximations of calculations on them. The number you obtained is very small, a rather good approximation to zero. That's what you get on computers when trying to use decimal numbers in place of whole numbers. – kumesana Aug 14 '19 at 12:31
  • Probably shouldn't ask for an answer in two completely different languages. I recommend sticking to one, especially because this is not Javascript code. At least post both if you want information on both. – Nexevis Aug 14 '19 at 12:31
  • Floating points. – briosheje Aug 14 '19 at 12:33
  • BigDecimal class in java – Rohit Suthar Aug 14 '19 at 12:34

0 Answers0