Thread: Float Division question

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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