How do I divide integers that equal float C?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bsmath
    New Member
    • Mar 2010
    • 7

    How do I divide integers that equal float C?

    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<iostre am>
    #include<string >
    #include<iomani p>
    #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();
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #2
    ? Can't you just check if the divisor is 0?

    Comment

    • manontheedge
      New Member
      • Oct 2006
      • 175

      #3
      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 ...

      Code:
      #include<iostream>
      using namespace std;
      
      int main()
      {
      int a(0); 
      int h(0);
      float C(0.0);
      
      // then to get a float value out, you would have to "cast" each integer to a float
      // you do this so that you will get a decimal answer.
      // if you divide two "int" variables, you will return an "int"
      // but if you tell the compiler that one ( or both ) is a "float", you'll get a float result
      
      C = (float)a / (float) h;
      
      // or you could do
      
      C = (float)a / h;

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        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.

        Comment

        • bsmath
          New Member
          • Mar 2010
          • 7

          #5
          Thank you for your answer.

          Comment

          Working...