Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Division problem

Reply
Thread Tools

Division problem

 
 
Jon
Guest
Posts: n/a
 
      10-13-2007
Hello all,

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

ulong b = (750 / 112;

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

 
Reply With Quote
 
 
 
 
Phil H
Guest
Posts: n/a
 
      10-13-2007
On 13 Oct, 19:12, Jon <JonMYa...@gmail.com> wrote:
> Hello all,
>
> I'm trying to work out why the following always returns 0:
>
> ulong b = (750 / 112;
>
> 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 / 112 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.


 
Reply With Quote
 
 
 
 
Alexey Smirnov
Guest
Posts: n/a
 
      10-13-2007
On Oct 13, 8:12 pm, Jon <JonMYa...@gmail.com> wrote:
> Hello all,
>
> I'm trying to work out why the following always returns 0:
>
> ulong b = (750 / 112;
>
> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Simple List division problem marcstuart Python 12 01-14-2008 09:05 PM
division by 7 without using division operator krypto.wizard@gmail.com C Programming 94 02-09-2007 06:57 AM
division problem jamesonang@gmail.com C Programming 13 12-27-2006 01:34 AM
Binary Division Problem Help jamestuck21 C Programming 22 12-01-2006 08:50 PM
Bigdecimal division problem manzur Java 5 02-23-2006 04:03 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57