Solved

1/2+3/2

Posted on 2000-04-09
1
161 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
typedef std::deque<BYTE> ByteVector is broken in vs2012 23 87
Exception thrown at 0x00007FFD5BC81F28 7 44
FMX TCameraComponent Problem 2 70
Beginner to Unreal Engine 4 5 80
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
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.

773 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