divide , maths and ceiling

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jon Vaughan

    divide , maths and ceiling

    Can someone tell me why the following statement gives me the result of 0 :

    Math.Ceiling( 1 / 12);

    Well actually the above is simplefied from :

    private const int LocationsPerPag e = 12;

    (int)System.Mat h.Ceiling(dsLoc ations.Tables[0].Rows.Count /
    LocationsPerPag e);


  • Marc Gravell

    #2
    Re: divide , maths and ceiling

    Because 1 / 12 is integer math, and evaluates to the integer zero (i.e. the
    whole number of 12s in 1 is zero - makes it easy to do full division via x /
    y and x % y [remainder]). Ceiling of zero is zero.

    Conversely, 1F / 12 will evaluate as a float to 0.083333... and then Ceiling
    is one.

    Marc


    Comment

    • Rick Lones

      #3
      Re: divide , maths and ceiling

      Jon Vaughan wrote:
      Can someone tell me why the following statement gives me the result of 0 :
      >
      Math.Ceiling( 1 / 12);
      >
      Well actually the above is simplefied from :
      >
      private const int LocationsPerPag e = 12;
      >
      (int)System.Mat h.Ceiling(dsLoc ations.Tables[0].Rows.Count /
      LocationsPerPag e);
      Sincce both operatnds of your division are integral types, the result of the
      divide is already zero before you pass it to the Ceiling() function. Cast the
      result to float and you will see your expected result.


      HTH,
      -rick-

      Comment

      • Phil

        #4
        Re: divide , maths and ceiling

        On Wed, 26 Jul 2006 10:29:35 GMT, "Jon Vaughan"
        <jonnyvaughan@h otmail.comwrote :
        >Can someone tell me why the following statement gives me the result of 0 :
        >
        >Math.Ceiling ( 1 / 12);
        >
        >Well actually the above is simplefied from :
        >
        >private const int LocationsPerPag e = 12;
        >
        >(int)System.Ma th.Ceiling(dsLo cations.Tables[0].Rows.Count /
        >LocationsPerPa ge);
        >

        1/12 is 0, this is an integer division. Ceiling(0) is just 0.
        Actually I had trouble reproducing your example, because the C#
        compiler complains of ambiguity in the calls to Math.Ceiling()


        Console.WriteLi ne(Math.Ceiling (1 / 12); // Won't compile.

        int result = 1 / 12;
        Console.WriteLi ne(Math.Ceiling ((double)result )); // Prints 0
        Console.WriteLi ne(Math.Ceiling (1.0 / 12.0)); // Prints 1



        Comment

        • tomasz.andrzejczak@gazeta.pl

          #5
          Re: divide , maths and ceiling

          Hi.

          Math.Ceiling needs Double or Decimal
          Instead of
          System.Math.Cei ling(dsLocation s.Tables[0].Rows.Count /
          LocationsPerPag e)
          try:
          System.Math.Cei ling((Double)ds Locations.Table s[0].Rows.Count /
          (Double)Locatio nsPerPage)

          It should help ...
          tomasz.andrzejc zak@gazeta.pl


          Jon Vaughan napisal(a):
          Can someone tell me why the following statement gives me the result of 0 :
          >
          Math.Ceiling( 1 / 12);
          >
          Well actually the above is simplefied from :
          >
          private const int LocationsPerPag e = 12;
          >
          (int)System.Mat h.Ceiling(dsLoc ations.Tables[0].Rows.Count /
          LocationsPerPag e);

          Comment

          • Marc Gravell

            #6
            Re: divide , maths and ceiling

            A different type of solution; another way (probably more efficient as no
            FP-math needed) of doing this would be:

            int locations = dsLocations.Tab les[0].Rows.Count; // total count
            int pages = locations / LocationsPerPag e; // full pages
            if(locations % LocationsPerPag e 0) pages++; // trailing rows on last page

            Marc


            Comment

            • Vincent

              #7
              Re: divide , maths and ceiling


              Jon Vaughan wrote:
              Can someone tell me why the following statement gives me the result of 0 :
              >
              Math.Ceiling( 1 / 12);
              >
              Well actually the above is simplefied from :
              >
              private const int LocationsPerPag e = 12;
              >
              (int)System.Mat h.Ceiling(dsLoc ations.Tables[0].Rows.Count /
              LocationsPerPag e);
              You are dividing to integers, so the result is an (int). Normally, a
              conversion to int will just chop of anything after the decimal point.
              So (int1 / int2) is the same as (int)(int1 / int2). You need to convert
              both values to a floating point (e.g. a double) to make the
              mathematical operation a non-integer one.

              you should try:
              (int)System.Mat h.Ceiling((doub le)dsLocations. Tables[0].Rows.Count /
              (double)Locatio nsPerPage);

              Which should give you the result you are looking for in this case:
              Ceiling(1/12) == 1

              Cheers,
              Vincent.

              Comment

              Working...