Solved

1/2+3/2

Posted on 2000-04-09
1
158 Views
Last Modified: 2010-04-02
double i;
i=1/2+3/2;
cout<<i;

can you explain why the o/p is 1?
0
Comment
Question by:skundu
1 Comment
 
LVL 22

Accepted Solution

by:
nietod earned 5 total points
ID: 2698281
the probkem is that you are doing the math with integer values which will be truncated.   So for example 1/2 truncates to an integer value of 0 and 3/2 (1.5) truncates to a value of 1.  so you get

i = 0 + 1;

which is 1.

Try specifyihg floating point numbers instead, like

i=1.0/2.0+3.0/2.0;
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now