Understanding Printf Formatting in C for Mathematical Operations

  • Thread starter Thread starter Spectre32
  • Start date Start date
Click For Summary

Discussion Overview

The discussion revolves around understanding the formatting and calculation of mathematical expressions in the C programming language, specifically focusing on the use of the printf function for outputting results. The context includes questions about integer and double type operations and their implications on results.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Homework-related

Main Points Raised

  • One participant expresses confusion about how to correctly perform mathematical operations in C, particularly with mixed data types (integers and doubles).
  • Another participant clarifies the calculation for E, indicating that the integer division of c by b results in truncation, affecting the final output.
  • A participant suggests that casting integers to doubles can help achieve the desired results in calculations.
  • It is noted that when performing operations with both integers and doubles, the integers are automatically promoted to doubles in C.
  • One participant expresses a preference for using double types exclusively for calculations to avoid issues with integer truncation.

Areas of Agreement / Disagreement

Participants appear to agree on the mechanics of type promotion in C, but there is no consensus on the best approach to avoid issues with integer division and truncation.

Contextual Notes

The discussion does not resolve the specific calculations or clarify all assumptions regarding the operations performed, particularly in relation to the expected outputs.

Who May Find This Useful

Individuals learning C programming, particularly those interested in mathematical operations and data type handling in coding contexts.

Spectre32
Messages
136
Reaction score
0
Ok perhaps I'm not grasping how C does math. I have these few questions:

E = delta*beta/gamma+c/b

F = (d/b)%d;


The print formats for those are %2.3lf and %3d respectively. Here are the values:

delta = 4 .000
beta = 4.0
gamma = 3.0
c = 2
b = 3
d = 7


c,b,d are type int and the rest are type double. On my answer sheet they says the answers for those two are E = 5.333 and F = 2 i have the spaces down, but I'm having a problem getting to those answers.

Any help would be great.
 
Computer science news on Phys.org
E = [(delta * beta) / gamma] + (c / b) = 5.333 + 0. The result of c / b is truncated. Similarly, F = (d / b) % d = 2 % 7 = 2.
 
hmmm i think i wa sjust screwing something minor then up. THanks
 
you can cast the ints into doubles if you wish

... + (double) c / (double) b

actually you only need to cast one of them as when an operation involves a mixture of ints and doubles, the remaining int(s) will be promoted automatically.

However, with your formula, the c/b part is calculated first, and as they are both ints, an integer calculation is done. then the answer to that calculation is promoted to a double, so that it can be added to the other doubles.
 
Last edited:
well see these are like test questions, and I wanted to know how to do them in the proper way. If it was up to me everything would be double.
 

Similar threads

  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 1 ·
Replies
1
Views
11K
Replies
7
Views
1K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 20 ·
Replies
20
Views
4K
Replies
19
Views
3K
  • · Replies 10 ·
Replies
10
Views
3K