473,395 Members | 1,797 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.

Problem! (Easy for most of you i'm sure)

I'm a C++ newbie, just been starting out today... I really can't get my head
around this...
*****************

int main()
{
int one = 1;
int number = 43;

double result = one / number;
double result2 = static_cast<double>(one / number);

cout << result << endl << result2;

return 0;
}

*****************

OUTPUT:

0
0

*****************
I dont understand!!! :-( please help

thanks!
Jul 19 '05 #1
4 2476

"Eurig Jones" <eu***@one47.com> wrote in message news:bo*******************@news.demon.co.uk...
int one = 1;
int number = 43;
These are two integers.

double result = one / number;
Ignore the stuff to the left of the = sign for now. The expression
one/number
has value zero, becaue integer divide throws away the fractional values.
1/43 is 0, 42/43 is 0, 43/43 is 1.

So in effect you have:
double result = 0;

The int zero is converted to double and initializers result. You have a double value of
zero.
double result2 = static_cast<double>(one / number);


The same thing happens here. The sub expression one/number is still 0.
double result2 = static_cast<double>(0);
The static cast converts it to double, but it's still zero.

If you want to do a floating point divide, you must convert at least one of the
operands to a floating point type (if you convert only one, the compiler will
automatically convert the other to match).

double result = static_cast<double>(one) / number;

for example. This convets the 1 to a double. The compiler now does the divide,
converting number to double to match (there are a list of "usual artithmatic conversions"
that gets applied to operators when the types of the operands don't match, generally
the least precise of the two is converted to the more precise).

The result of this expression is the 1.0/43 or about .023...
Jul 19 '05 #2
hi!
double result = one / number;
double result2 = static_cast<double>(one / number);


if i'm not completely wrong, the divisions are calculated using integer
division. both operands are integer so integer div. is used (and in the
second case, the result!! is casted to double....)
you must convert (at least) one operand to the precision you want the
calculation to take place in, the the second operand is implicitly converted
to the greater type.
(though i think it's better and better style to cast both to double)

double result = static_cast<double>(one) / static_cast<double>(number);

should do the trick.

regards,
sev
Jul 19 '05 #3
> int main()
{
int one = 1;
int number = 43;

double result = one / number;
double result2 = static_cast<double>(one / number);

cout << result << endl << result2;

return 0;
}

*****************

OUTPUT:

0
0

*****************


The output is correct! The two variables "one" and "number" are integer, so
the result of "one/number" ist
integer too (0). And this you are converting to double which ist remaining
0.

Try the following code:

int main()
{
int one = 1;
int number = 43;

double result = (double)one / (double)number;

cout << result << endl << result2;

return 0;
}

If one of the operands is of the type "double", the operation is a floating
point operation.

Ralf

http://www.cplusplus-kurse.de


Jul 19 '05 #4
> I'm a C++ newbie, just been starting out today... I really can't get my
head
around this... I dont understand!!! :-( please help

thanks!


Also look up the "usual rules of arithmetic conversion" (in addition to what
others have now said).
Jul 19 '05 #5

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

Similar topics

0
by: Bruce Davis | last post by:
I'm having a problem on windows (both 2000 and XP) with a multi-threaded tkinter gui application. The problem appears to be a deadlock condition when a child thread pops up a Pmw dialog window in...
11
by: Kostatus | last post by:
I have a virtual function in a base class, which is then overwritten by a function of the same name in a publically derived class. When I call the function using a pointer to the derived class...
0
by: Refky Wahib | last post by:
Hi I need Technical Support I finished a Great project using .Net and SQL Server and .Net Mobile Control My Business case is to implement this Program to accept about 1 Million concurrent...
9
by: Sudesh Sawant | last post by:
Hello, We have an application which communicates using remoting. There is a server which is a Windows Service. The server exposes an object which is a singleton. The client is a Web Application...
117
by: Peter Olcott | last post by:
www.halting-problem.com
18
by: Ian Stanley | last post by:
Hi, Continuing my strcat segmentation fault posting- I have a problem which occurs when appending two sting literals using strcat. I have tried to fix it by writing my own function that does the...
28
by: Jon Davis | last post by:
If I have a class with a virtual method, and a child class that overrides the virtual method, and then I create an instance of the child class AS A base class... BaseClass bc = new ChildClass();...
2
by: Mike Collins | last post by:
I cannot get the correct drop down list value from a drop down I have on my web form. I get the initial value that was loaded in the list. It was asked by someone else what the autopostback was...
0
by: =?Utf-8?B?am8uZWw=?= | last post by:
Hello All, I am developing an Input Methop (IM) for PocketPC / Windows Mobile (PPC/WM). On some devices the IM will not start. The IM appears in the IM-List but when it is selected from the...
1
by: sherifbk | last post by:
Problem description ============== - I have 4 clients and 1 server (SQL server) - 3 clients are Monitoring console 1 client is operation console - Monitoring console collects some data from...
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: 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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.