Division problem

Discussion in 'ASP .Net' started by Jon, Oct 13, 2007.

  1. Jon

    Jon Guest

    Hello all,

    I'm trying to work out why the following always returns 0:

    ulong b = (750 / 1128);

    I've tried other types, not just ulong but always with the same
    results, 0;

    Am I missing something? It been a long day!

    Thanks

    Jon
     
    Jon, Oct 13, 2007
    #1
    1. Advertising

  2. Jon

    Phil H Guest

    On 13 Oct, 19:12, Jon <> wrote:
    > Hello all,
    >
    > I'm trying to work out why the following always returns 0:
    >
    > ulong b = (750 / 1128);
    >
    > I've tried other types, not just ulong but always with the same
    > results, 0;
    >
    > Am I missing something? It been a long day!
    >
    > Thanks
    >
    > Jon


    John

    ulong is an integer data type (unsigned 64 bit) which cannot represent
    the fraction 750/1120

    If you are working with non-integral numbers you need to work with
    types such as float, single, double etc.

    However, one word of warning. Even when an expression is being
    assigned to a non-integral numeric type the result is evaluated is
    though it is an integer if all the terms themselves are integers. The
    expression (750 / 1128) will be treated as an integer because 750,
    1128 are themselves integers, and since 0 < (750/1120) < 1 it is
    rounded down to 0

    Try this instead:

    float b = 750.0F / 1128.0F

    I guarantee it will not be zero.
     
    Phil H, Oct 13, 2007
    #2
    1. Advertising

  3. On Oct 13, 8:12 pm, Jon <> wrote:
    > Hello all,
    >
    > I'm trying to work out why the following always returns 0:
    >
    > ulong b = (750 / 1128);
    >
    > I've tried other types, not just ulong but always with the same
    > results, 0;
    >
    > Am I missing something? It been a long day!
    >
    > Thanks
    >
    > Jon


    ulong is an integer and 750 / 1128 equals 0.66 - the integer part is 0

    If you need to round it up, use Math.Round, for example

    ulong b = Convert.ToUInt64(Math.Round(750D / 1128D)); // =1 :)
     
    Alexey Smirnov, Oct 13, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. manzur

    Bigdecimal division problem

    manzur, Feb 23, 2006, in forum: Java
    Replies:
    5
    Views:
    8,143
    Patricia Shanahan
    Feb 23, 2006
  2. jamestuck21

    Binary Division Problem Help

    jamestuck21, Nov 30, 2006, in forum: C Programming
    Replies:
    22
    Views:
    1,739
    Al Balmer
    Dec 1, 2006
  3. division problem

    , Dec 22, 2006, in forum: C Programming
    Replies:
    13
    Views:
    772
  4. Replies:
    94
    Views:
    4,519
    ┬Ča\\/b
    Feb 9, 2007
  5. marcstuart

    Simple List division problem

    marcstuart, Jan 12, 2008, in forum: Python
    Replies:
    12
    Views:
    476
    Pierre Quentel
    Jan 14, 2008
Loading...

Share This Page