Thread: Float Division question

  1. #1
    Registered User
    Join Date
    Dec 2020
    Posts
    1

    Cool Float Division question

    I was writing a program where two integer values needed to be divided so i assigned both of them to a float variable and casted them to (float), so i did this:

    ```
    float average = (float) wordcount / (float) lettercount;
    ```
    Unfortunately this gave me a wrong answer when dividing both of these integer values casted to floats using (float). But then I changed up my code and did this:

    ```
    printf ( "%f", ( ( (float) wordcount) / ( (float) lettercount) ) );
    ```
    This bottom line seemed to work and gave me the correct float value, my question is, why did the top line not give me the correct answer, and why did the bottom line work. I would have thought the top line is a float and all operands are being casted to float so there is no precedence issues of what is calculated first?

    Also could you please recommend me an article or anything so i can further deepen my knowledge on data type arithmetic, thank you.
    Last edited by butcher47; 12-20-2020 at 10:24 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Post some actual compilable code showing both test cases.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    The second one likely used double instead of float to store the result.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    505
    Quote Originally Posted by butcher47 View Post
    I was writing a program where two integer values needed to be divided so i assigned both of them to a float variable and casted them to (float), so i did this:

    ```


    ```
    Unfortunately this gave me a wrong answer when dividing both of these integer values casted to floats using (float).
    Should be fine. Almost certainly you have an error elsewhere
    in the code. For example when printing the result out.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C Float: Should use Division or Multiplication
    By hqt in forum C Programming
    Replies: 8
    Last Post: 01-01-2012, 03:30 AM
  2. [beginner] float and division
    By codezero in forum C Programming
    Replies: 5
    Last Post: 04-27-2009, 09:32 PM
  3. float number division
    By hoistyler in forum C Programming
    Replies: 6
    Last Post: 01-14-2009, 03:13 AM
  4. issues in float division a/b
    By George2 in forum C# Programming
    Replies: 17
    Last Post: 04-24-2008, 06:15 AM
  5. int division to float
    By rimig88 in forum C++ Programming
    Replies: 4
    Last Post: 04-22-2008, 08:48 AM

Tags for this Thread