Confused by behaviour

This is a discussion on Confused by behaviour within the C++ Programming forums, part of the General Programming Boards category; Hey guys, I have a piece of code here that gives the wrong answer Code: int a = 5; float ...

  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
    21,310
    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?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,451
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,648
    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, 03:49 PM
  3. why wont this compile?!? :confused:
    By jdude in forum C++ Programming
    Replies: 5
    Last Post: 11-25-2004, 12: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, 10:11 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21