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

How do I divide integers that equal float C?

7
I tried the program below to divide integers by integers (a, n, h etc. below) that equal C, but it does not divide correctly. Someone told me that "float C=float();" should have an epsilon somewhere. The computer language is C++. Can you tell me how to program it so it will divide properly?

#include<iostream>
#include<string>
#include<iomanip>
#include<math.h>
#include<stdio.h>
using namespace std;
int main()
{
//Start Coding Here
//Declare And Initialize The Variables
int a=int();
int h=int();
float C=float();
int n=int();
int b=int();
int q=int();
Mar 23 '10 #1

✓ answered by manontheedge

it appears you should start with the basics, such has data types ( like int, float, char, ... ). Anyway, what you should be doing would look something like this ( notice the low number of include files at the top ...

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include<iostream>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. int a(0); 
  8. int h(0);
  9. float C(0.0);
  10.  
  11. // then to get a float value out, you would have to "cast" each integer to a float
  12. // you do this so that you will get a decimal answer.
  13. // if you divide two "int" variables, you will return an "int"
  14. // but if you tell the compiler that one ( or both ) is a "float", you'll get a float result
  15.  
  16. C = (float)a / (float) h;
  17.  
  18. // or you could do
  19.  
  20. C = (float)a / h;
  21.  
  22.  

4 22992
jkmyoung
2,057 Expert 2GB
? Can't you just check if the divisor is 0?
Mar 24 '10 #2
manontheedge
175 100+
it appears you should start with the basics, such has data types ( like int, float, char, ... ). Anyway, what you should be doing would look something like this ( notice the low number of include files at the top ...

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include<iostream>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. int a(0); 
  8. int h(0);
  9. float C(0.0);
  10.  
  11. // then to get a float value out, you would have to "cast" each integer to a float
  12. // you do this so that you will get a decimal answer.
  13. // if you divide two "int" variables, you will return an "int"
  14. // but if you tell the compiler that one ( or both ) is a "float", you'll get a float result
  15.  
  16. C = (float)a / (float) h;
  17.  
  18. // or you could do
  19.  
  20. C = (float)a / h;
  21.  
  22.  
Mar 24 '10 #3
weaknessforcats
9,208 Expert Mod 8TB
C = (float)a / (float) h;
The variables a and h are ints. ints have no decimal portion. That is if you divide 7 by 2, the result is 3. Meaning there are 3 twos
in 7. The result is not 3.5 becuse there is no such thing as half a 2 where an int is concerned.

The rules of significant fihures also state that a result cannot be more accurate than the coarsest of its components. That is, you cannot make thinks more accurate by tacking on decimal places. For 3.5 to be the result you have to start with 7.0 / 2.0. Fir 3.50 to be the result you have to start with 7.00/2.00

Further integers are a subset of real values. In the integer subset there are no values that are portions of integers. Integers by definition are the counting numbers, their additive inverses and the identity element.

Do try to avoid mixing integers and floating point.
Mar 24 '10 #4
bsmath
7
Thank you for your answer.
Mar 26 '10 #5

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

Similar topics

1
by: Josh Phillips | last post by:
I have written the following query and even though I used CAST if the number is less then 1 it show up as 0. Here is the query: select cast(count(SafetyTrainingYN) AS FLOAT) from ICPCDATA...
3
by: Ryan | last post by:
I have the following line in a select statement which comes up with a divide by zero error. CAST(CASE Splinter_Status WHEN 'SUR' THEN 0 ELSE CASE WHEN Sacrifice>=1 THEN...
9
by: Veerle | last post by:
Hi, I would like to divide the whole of my html page in 4 equal rectangles using only div tags in my html. The result should look a bit like this site:...
8
by: Jonathan Fielder | last post by:
Hi, I have a 32 bit integer value and I wish to find the single precision floating point value that is closest to but less than or equal to the integer. I also have a similar case where I need...
23
by: junky_fellow | last post by:
I have a question related to pointer/integer conversion. After reading various threads on this newsgroup I found that pointer/integer conversion is not portable and may lead to undefined results...
5
by: per.nordlow | last post by:
Hey there! I want to multiply/divide (shift) a float with variable containing a power-of-two value: 2, 4, 8, 16, .... Is there such a function in the C standard library or the glibc library...
1
by: freedsbi | last post by:
i need to copy N unsigned short number that live in an N-dimensional array onto an equal size array of floats (the original array - unsigned short - is returned by the cfitsio library). can i use...
6
by: Johs | last post by:
I have: int a = 1; int b = 2; double c = a/b; Is it somehow possible to divide these two integers and get the result as a double 0.5? Or do they both have to be declared as doubles?
40
by: Spiros Bousbouras | last post by:
Do you have an example of an implementation where sizeof(short int) does not divide sizeof(int) or sizeof(int) does not divide sizeof(long int) or sizeof(long int) does not divide sizeof(long long...
14
by: Default User | last post by:
Hi, If I have three 64 bit integers and I want to do this operation on them: x*y/z Lets say that what we are multiplying by (y) is offset by what we are dividing by (z) so that the final...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.