The ceil trap (Beginners Only Topics)

Revision en1, by ivplay, 2015-11-20 22:49:00
  • I fell into a very interesting situation today , while solving CF — 599D.
  • say long long a = 1000000000000000363,b= 91
  • I wrote long long c = ceil(a*1.0/b) thinking (long * double) / double should be perfect double and ceil(double) — should work out.
  • I was expecting correct answer, c = 10989010989010993, but it produced c= 10989010989010994 , 1 more than the actual — result!!!! Damn to the precision issue.
  • Lesson :

    • Option 1 : Study lot lot and lot about your compiler and how it manages precision while both implicit and explicit cast.
    • Option 2 : Use your own ceil function like this
  • template inline T ceil(T numerator ,T denominator)
  • {
    • return (numerator+denominator-1)/denominator;
  • }
  • Moral : Avoid fraction as much as you can.

Tags fractions, ceil, math

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English ivplay 2015-11-20 22:49:00 849 Initial revision (published)