priyansh_max's blog

By priyansh_max, history, 11 months ago, In English

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

  • Vote: I like it
  • +3
  • Vote: I do not like it

»
11 months ago, hide # |
Rev. 3  
Vote: I like it 0 Vote: I do not like it

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