Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8120231
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T05:04:52+00:00 2026-06-06T05:04:52+00:00

Possible Duplicate: C programming division in the following code example the result should yield

  • 0

Possible Duplicate:
C programming division

in the following code example the result should yield 130.0 yet once compiled, I get 129.0. Is the right hand side not producing enough precision to get an accurate result? Is there a way around this?

#include<stdio.h>
int main(void) {
    double sum = ((117+130)/3) + ((130+13)/3);
    printf("sum: %f\n", sum);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T05:04:55+00:00Added an answer on June 6, 2026 at 5:04 am

    What you are observing is the result of integer truncation. When doing integer division with two integer operands, the result will be another integer, with the fractional part thrown away. Note that this is different from rounding, e.g, with truncation 3.8 will become 3.

    This will give you your expected output:

    double sum = ((117+130) / 3.0) + ((130+13) / 3.0);
    

    since we divide by 3.0 rather than 3.

    I.e., if at least one, or both of the operands in an integer division is a float type, the result will be a float. (Note that I’m using float/double here somewhat interchangeably with regard to ints and truncations)

    In addition to appending a . or .0 to values (.0 will generate a double, while .0f will generate a float – as pointed out by @Morwenn’s helpful comment below), we can also explicitly cast ints to floats, but it matters when we do it. A simple example (note that the values end up as float after the assignment in this example in any case since v is a float):

    float v = 0;
                         /* values shown are BEFORE assignment */
    v = (5 / 2);         /* value is 2 due to integer truncation before assignment */
    v = (float) (5 / 2); /* 2.0 as integer division occurs 1st, then cast to float */
    v = (float) 5 / 2;   /* 2.5 since 5 becomes 5.0 through casting first. */
    

    I borrowed the above example from Division and floating points

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: C programming division I'm trying to calculate the period of accelerometer updates
Possible Duplicate: Get path of executable I'm programming on Windows using MinGW, gcc 4.4.3.
Possible Duplicate: C programming division probably my question is very simple and stupid. I
Possible Duplicate: Can you write object oriented code in C? Object oriented programming in
Possible Duplicate: Cross platform programming I've written some code using wxwidgets in c++. But
Possible Duplicate: using Clipboard in WP7 programming For example you have a long text
Possible Duplicate: How to get useful error messages in PHP? I'm programming php on
Possible Duplicate: #include anywhere For the respective languages, is the following valid (acceptable programming
Possible Duplicate: What is your longest-held programming assumption that turned out to be incorrect?
Possible Duplicate: C++ beginner, execution window disappears quickly I am beginner at C programming.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.