+ 1

How to use mod(%) with doubles (java)?

I have the doubles d,c2. I want to do the following: c2 = (d2*1000)%1000; But it gives me an error of possibility of loss of data... Please help me! https://code.sololearn.com/c3SW5DMSm39a/?ref=app

26th May 2019, 8:56 PM
Giorgos
Giorgos - avatar
4 Answers
+ 3
line 113: int c2; d2 is a double. I am not sure what your code does so I can not tell which version is better. a) double c2; b) c2 = (int)(d2 * 1000) / 1000;
26th May 2019, 9:31 PM
Denise Roßberg
Denise Roßberg - avatar
+ 2
Btw: You should be familiar with double precision. I never used modulus operator with doubles. But I think it causes rounding problems.
26th May 2019, 9:33 PM
Denise Roßberg
Denise Roßberg - avatar
+ 1
Thanks Denise! Thanks a lot!
26th May 2019, 9:39 PM
Giorgos
Giorgos - avatar
+ 1
Giorgos Your welcome :)
26th May 2019, 9:43 PM
Denise Roßberg
Denise Roßberg - avatar