473,395 Members | 1,631 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,395 software developers and data experts.

Integer division in c++ as opposed to real division

I have the following details
double x= 1.5
double y= -1.5
int m= 20
int n= 4

my question is 5 * x – n / 5 at which what would n / 5 equal to, I think its zero since its integer division? or would the 5 be considered a real number?
Sep 28 '13 #1
2 5684
stdq
94 64KB
If n is an integer, and 5 is an integer literal, the result is 5 / 4 without the floating-point part, that is, the result is 1.20 with the 0.20 part discarded - in other words, the result is 1. If you want the result to be 1.20, you can do this (note the use of 5.0 instead of 5):

Expand|Select|Wrap|Line Numbers
  1. n / 5.0

or this:

Expand|Select|Wrap|Line Numbers
  1. ( ( double ) n ) / 5

or even this:

Expand|Select|Wrap|Line Numbers
  1. n / ( ( double ) 5 )

( double ) is a cast operator. It is being used to create a floating-point temporary copy of its operand, which is n or 5, depending on your choice.

Remember: in C, a division with both operands being integers results in an integer (this is called integer division, I think). You can follow one of the approaches I mentioned above so that when of the operands of / is a floating-point value, resulting in floating-point division.

Best,
stdq
Sep 29 '13 #2
weaknessforcats
9,208 Expert Mod 8TB
Firstly, the posted code won't compile.

After it compiles, you process 5 * x where x is a double. So 5 is promoted to double and the result is double. That would be 7.5.

Next you do n/5. Multiplication and division are done before addition and subtraction. n/5 is really 4/5, which is 0 because 5 goes into 4 zero times. (integer division).

Next you do 7.5 - 0. In this case 0 is promoted to double. And there is the final answer: 7.5.
Sep 29 '13 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: SAN CAZIANO | last post by:
how can i insert only a real number (an integer with only one decimal separator) in a edit field perhaps in onchange event I have to test if the decimal separator in the text is >0 I simply...
17
by: henrytcy | last post by:
Hi, How can I get a digit from integer for example, 12311, one by one for comparision? Thanks!
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...
10
by: Mike S | last post by:
Does anyone know the logic behind why in VB.NET the result of a floating-point division ('/') is -rounded- on being converted to an integer type, such as with statements like Dim x As Integer =...
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: Jason Huang | last post by:
Hi, In my C# Net 2.0, in a DetailsView I have a row TestStatus, and the output will be interger 0, 1, where the 0 presents Pass, 1 presents Fail. What do I need to do so that the output will...
2
Jezternz
by: Jezternz | last post by:
Okay basicly, I have a division inside a division. <html><body> <div id="container"> <div id="header">Main image</div> </div> </body> </html> and I want the container to grow as the the...
30
by: =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?= | last post by:
Let's say we had a simple function for returning the amount of days in a month: unsigned DaysInMonth(unsigned const month) { switch (month) { case 8: case 3: case 5:
4
by: Astley Le Jasper | last post by:
I've been getting errors recently when using pysqlite. I've declared the table columns as real numbers to 2 decimal places (I'm dealing with money), but when doing division on two numbers that...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.