473,385 Members | 1,312 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Division problem

Jon
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

Oct 13 '07 #1
2 1604
On 13 Oct, 19:12, Jon <JonMYa...@gmail.comwrote:
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.
Oct 13 '07 #2
On Oct 13, 8:12 pm, Jon <JonMYa...@gmail.comwrote:
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 :-)

Oct 13 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Imbaud Pierre | last post by:
integer division and modulo gives different results in c and python, when negative numbers are involved. take gdb as a widely available c interpreter print -2 /3 0 for c, -1 for python. more...
15
by: joel | last post by:
I have a table which I want to update by dividing one field into another. The update runs with no errors, but the results come out as only a positive integer number. The datatype for the result...
6
by: bart van deun | last post by:
Hello, i have a problem with a division by zero fault in my report trough a query... this is the expression in the query: Name: Sum(IIF(=0,0,/)) what is wrong with this expression?
8
by: nineteen | last post by:
for example: 16/5: 3 -------- 5)16 15 -------- 1 the user input the dividend and divisor,the program should display the upright formula like the example. and , the program must use recursive...
17
by: seb.haase | last post by:
Hi, Is it true that that "Python 3000" is dead ? Honestly I think that e.g. changing 5/2 to be 2.5 (instead of 2) would just break to much code :-( On the otherhand I'm using Python as "Matlab...
22
by: jamestuck21 | last post by:
Hi, I'm trying to work out a binary division problem 1100 / 101010101010111 Here is what I have so far but I'm not sure if I'm doing it correctly and I'm suppose to continue the division...
2
by: kermit | last post by:
For a long time,, There has been a discussion of trueFor division versus integer division in Python. I myslef prefer that / be used for integer division since almost always, I want the...
13
by: jamesonang | last post by:
Supposed unsigned int(32 bits) is the largest number that computer can represent with a single variable. Now, i have a big integer ( less than 64 bit, but great than 32 bit) . i represent it by...
1
by: youjay | last post by:
I've been out of perl for a while, so I am starting from scratch. I have a small applet which scans a set of directories, getting information from some files in each one, and displaying selected...
16
by: spl | last post by:
To increase the performance, how to change the / operator with bitwise operators? for ex: 25/5, 225/25 or 25/3 or any division, but I am not bothered of any remainder.
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.