Блог пользователя priyansh_max

Автор priyansh_max, история, 18 месяцев назад, По-английски

I am a bit confused here what's the difference? given--> (1 ≤ n ≤ 10^18)

Approach — 1 long min = n/6+(n % 6 > 0 ? 1 : 0);

Approach — 2 long min = (long)Math.ceil((n * 1.0)/6);

Approach — 1 passed all the test cases

Approach — 2 failed on large values of n

  • Проголосовать: нравится
  • +3
  • Проголосовать: не нравится

»
18 месяцев назад, скрыть # |
← Rev. 3  
Проголосовать: нравится 0 Проголосовать: не нравится

try

long n = (long)1e18 - 2
long min = 2 * (long)Math.ceil((n * 1.0)/2);

The result should be 1e18 — 2 but instead it will print 1e18

The issue is that there are some approximation errors in doubles/floats, they are not 100% accurate an infamous example is comparing 0.1 + 0.2 to 0.3

Approach 1 doesn't use floating points, that's why it works