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.