473,406 Members | 2,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,406 software developers and data experts.

Simple Float Question

I made this code as an example of my question. The actual program I will be making is bigger but I made this just to test, and be able to show what I'm asking.
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6.     float average;
  7.     float average2;
  8.  
  9.     average=3.5;
  10.     average2=(7/2);
  11.  
  12.     cout << average << endl;
  13.     cout << average2 << endl;
  14.  
  15.     return 0;
  16. }
  17.  
The result is:
3.5
3
If average2 is a float and is being set equal to (7/2) why isn't it 3.5 instead of 3? I know for int values it drops off the remainder or decimal but if average2 is a float why won't it keep the decimal? It's probably something really easy or stupid I've forgotten.

Thanks.
Mar 10 '09 #1
4 1507
whodgson
542 512MB
You are promising that variable1 is a float but you initialise it with ints so it returns an int result.
Change 7 to 7.0 and 2 to 2.0
rgds
Mar 10 '09 #2
donbock
2,426 Expert 2GB
Expand|Select|Wrap|Line Numbers
  1. float average2 = (7/2);
Another way to explain it is that the compiler first evaluates the expression to the right of the equals sign. It sees one integer divided by another integer and therefore decides to use integer mathematics, yielding an integer result of 3 or 4. Then it tries to assign that value to the variable on the left side of the equals sign but finds a type mismatch, so it casts the integer result to float.

By the way, literal constants 3.5, 7.0, and 2.0 are doubles, that will end up being cast to floats in a similar process. Literal float constants are accomplished through an "f" suffix: 3.5f, 7.0f, and 2.0f.
Mar 10 '09 #3
Thank you guys, you did a good job explaining how I cannot get a decimal value from dividing 7/2but can with 7.0/2.0. I'm not sure how I can change an integer value to a double or float though. Here's a snippet of my code (the part that applies to my question)
Expand|Select|Wrap|Line Numbers
  1. if ((length+1)%2 ==0)
  2.     {
  3.         median= ((value[length/2]+value[length/2+1])/2);
  4.     }
  5.  
In this case value[] is an array of int values. If the total length of the array is EVEN you will need to take the average of the two middle numbers to find the median. In this case the code will end up being
median=(3+4)/2
Which returns a 3 as the median like we discussed above. My question now is how can I get around that, in order for it to return 3.5? I have to keep the array as an integer array too.

Thanks again
Mar 10 '09 #4
scruggsy
147 100+
As mentioned previously if one of the operands is a double then the other will be promoted to double as well.
Since you are using the literal (integer) value '2' just replace it with '2.0' to get the desired result.
Expand|Select|Wrap|Line Numbers
  1. median= ((value[length/2]+value[length/2+1]) / 2.0);
  2.  
If you were not using literal values then you would want to do an explicit cast of one of the operands to a double.
Mar 10 '09 #5

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

Similar topics

1
by: Rohan Shah | last post by:
i am looking for some simple matrix routines: vector*matrix, matrix*matrix etc...in the form of code like the following that multiplies two vectors: void vmult(double* aa, double* bb, double*...
8
by: SK | last post by:
Hi I am trying to write a simple C program with devc++ as the complier using the concept of arrays. I cannot get the program to compile without mutiple errors. If anyone has the time to help me...
17
by: RSH | last post by:
I am really trying to grasp the concept of OOP as it applies to C#. I am looking at trying to set up a simple Employee Class but I am having trouble conceptualizing what this class should look...
1
by: c_beginner | last post by:
yes, this is my how work question. Since I am lack in getting an assistance with my lab work I put this in this advance group. Sorry for the trouble I am making. Write a program to calculate the...
3
by: hantechs | last post by:
<html> <body> <p style="width:30%;">text1</p> <p style="float:left;">text2</p> </body> </html> The effect of this html code is : text1 and text2 each is on a line. My question is: Why text2...
5
by: frankb.mail | last post by:
Ok i'm new to C++ and am teaching myself i've hit a small block that I can't seem to get around I am trying to convert an array like std::string load; into a float array. The code looks...
1
by: Bl00dFox | last post by:
Hi I am making a simple program to calculate interest. At the beginning when the user has to pick 1 or 2 (to select simple or compound interest respectively), if the user enters a letter (eg, a)...
32
by: alex.j.k2 | last post by:
Hello all, I have "PRECISION" defined in the preprocessor code and it could be int, float or double, but I do not know in the code what it is. Now if I want to assign zero to a "PRECISION"...
0
by: gerry | last post by:
for anyone interested I did find a solution : .outer { float:left; border:solid 1px black; overflow:hidden: } .full {
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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.