Thread: Confused by behaviour

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    36

    Confused by behaviour

    Hey guys,

    I have a piece of code here that gives the wrong answer

    Code:
    int a = 5;
    float result;
    float b = 10;
    
    result = a / b;
    The problem was that the result would always be an integer. I cannot understand why this is the case? the variable 'result' is declared as floating point, so why does the integer value 'a' have any affect here? Do most compilers do this or is it only g++

    I managed to fix my code by type casting and adding (float) in front of the division. Is there a better, more elegant way of doing this?

    Code:
    int a = 5;
    float result;
    float b = 10;
    
    result = (float) a / b;
    Many thanks,
    James

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by rocketman50
    the variable 'result' is declared as floating point, so why does the integer value 'a' have any affect here?
    Well, you know that because of integer division, a / b has the result of 0. Clearly, you would not get 0.5 by assigning 0 to a float variable. Therefore, the answer must be to use floating point division, not integer division. Using a float variable to store the result is not enough.

    Quote Originally Posted by rocketman50
    I managed to fix my code by type casting and adding (float) in front of the division. Is there a better, more elegant way of doing this?
    That is a correct solution, though you could have used a C++ specific cast to make the cast more obvious:
    Code:
    result = static_cast<float>(a) / b;
    EDIT:
    Hold on, I just noticed that b is of type float. Therefore, floating point division should have been applied even without the cast. Are you sure that you observed the effects of integer division?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    You have some buggy compiler or you do not show the actual code

    dividing int var to float results in float

    Code:
    #include <iostream>
    
    
    int main(int argc, char* argv[])
    {
    	int a = 5;
    	float result;
    	float b = 10;
    
    	result = a / b;
    
    	std::cout << result <<std::endl;
    
    	return 0;
    }
    Output
    Code:
    0.5
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Works fine in my g++
    Code:
    #include <iostream>
    
    int main()
    {
        int a = 5;
        float result;
        float b = 10;
    
        result = a / b;
    
        std::cout << "Result: " << result << std::endl;
        return 0;
    }
    Code:
    $ ./intdiv 
    Result: 0.5
    $ g++ -v
    Using built-in specs.
    Target: i686-apple-darwin10
    Configured with: /var/tmp/gcc/gcc-5646.1~2/src/configure --disable-checking --enable-werror --prefix=/usr 
    --mandir=/share/man --enable-languages=c,objc,c++,obj-c++ --program-transform-name=/^[cg][^.-]*$/s/$/-4.2/ 
    --with-slibdir=/usr/lib --build=i686-apple-darwin10 --with-gxx-include-dir=/include/c++/4.2.1 
    --program-prefix=i686-apple-darwin10- --host=x86_64-apple-darwin10 --target=i686-apple-darwin10
    Thread model: posix
    gcc version 4.2.1 (Apple Inc. build 5646) (dot 1)
    Last edited by rags_to_riches; 03-15-2010 at 10:56 AM. Reason: Fix overrun

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Confused about Memory
    By gL_nEwB in forum C++ Programming
    Replies: 22
    Last Post: 06-20-2006, 07:32 PM
  2. Confused
    By (TNT) in forum C# Programming
    Replies: 1
    Last Post: 11-23-2005, 04:49 PM
  3. why wont this compile?!? :confused:
    By jdude in forum C++ Programming
    Replies: 5
    Last Post: 11-25-2004, 01:13 AM
  4. confused.. in selecting my line of deapth
    By jawwadalam in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 05-04-2003, 01:21 PM
  5. Extern Question, really confused
    By SourceCode in forum C Programming
    Replies: 10
    Last Post: 03-26-2003, 11:11 PM