0
 #include <stdio.h>

 int main(void)
 {
     float c =8/5;
     printf("The Result: %f", c);
     return 0;
 }

The answer is 1.000000. Why isn't it 1.600000?

intcreator
  • 4,206
  • 4
  • 21
  • 39
Jabed
  • 178
  • 1
  • 2
  • 13
  • 1
    There is no type casting involved. Would be a good idea to change the title. – too honest for this site Jan 30 '16 at 04:36
  • @ShadowRanger: I disagree; While the question is badly researched, it is not about he difference between integer and rounded floating point division, but why an integer division does not yield a float result. – too honest for this site Jan 30 '16 at 04:44
  • While the title is better now, you seem to have missed the point `8 / 5` is **not** a float(ing point) expression, but an integer. None of the operands are floating point values. – too honest for this site Jan 30 '16 at 04:59
  • float c is getting its result from a cast of an int result of (8/5) which is (obviously) 1. – milevyo Jan 30 '16 at 04:59
  • @milevyo: There is no cast! A cast is an expiclt conversion, e.g. `(float)8`. This is a(n implicit) converversion from `int` to `float`. (I sometimes wish Pascal - or better Modula-2 - had won the game). – too honest for this site Jan 30 '16 at 05:03

3 Answers3

2

C is interpreting your 8/5 input as integers. With integers, C truncates it down to 1.

Change your code to 8.0/5.0. That way it knows you're working with real numbers, and it will store the result you're looking for.

earl
  • 99
  • 4
1

The expression

8/5

is an all int expression. So, it evaluates to (int )1

The automatic conversion to float happens in the assignment.

If you convert to float before the divide, you will get the answer you seek:

(float )8/5

or just

8.0/5
Doug Currie
  • 40,708
  • 1
  • 95
  • 119
0

When you don't specify what data types you use (for example, in your code you use the integer constants 8 and 5) C uses the smallest reasonable type. In your case, it assigned 8 and 5 the integer type, and because both operands to the division expression were integers, C produced an integer result. Integers don't have decimal points or fractional parts, so C truncates the result. This throws away the remainder of the division operation leaving you with 1 instead of 1.6.

Notice this happens even though you store the result in a float. This is because the expression is evaluated using integer types, then the result is stored as is.

There are at least two ways to fix this:

Cast the part of the expression to a double type or other type that can store fractional parts:

Here 8 is cast to the double type, so C will perform float division with the operands. Note that if you cast (8 / 5), integer division will be performed before the cast.

foo = (double) 8 / 5

Use a double as one of the operands:

foo = 8.0/5
intcreator
  • 4,206
  • 4
  • 21
  • 39
  • 1
    C doesn't do floor division; pre-C99, it's either floor or truncate towards zero, implementation defined, in C99 and later it's strictly truncate towards zero, not floor. For positive results it's the same thing (both round down), but for negative results floor division rounds down, truncate towards zero rounds up. – ShadowRanger Jan 30 '16 at 04:39
  • @ShadowRanger Good to know, I'll edit that in. – intcreator Jan 30 '16 at 04:42
  • 1
    It's a little misleading to say that you're casting the expression in the first example. The cast operator has precedence over the division, so you're actually casting 8 to a float. If you wrote `(float)(8/5)`, you'd still get 1. – zneak Jan 30 '16 at 04:43
  • @zneak Good catch. I'll add that as well. – intcreator Jan 30 '16 at 04:44
  • 1
    C does not "guess" what types you use, but this is very well specified by the standard. Basically, for an integer constant, it uses the "smallest" possible, starting with `int` (not `char`!). For that the term "rank" is used in the standard. – too honest for this site Jan 30 '16 at 04:46
  • @Olaf You're right, I was dumbing it down a little. The answer is now more accurate. – intcreator Jan 30 '16 at 04:49
  • 1
    You use some uncommon (at best) or very missleading terms in your answer. For instance: what do you mean with "decimals"? an _integer constant_ like `101` is very well a decimal number (`0101` is octal for example). Also note that `8.0` is not a `float`, but a `double`. A `float` constant would be `8.0f`. – too honest for this site Jan 30 '16 at 04:51
  • Sorry to tell you, it is still incorrect. FYI: http://port70.net/~nsz/c/c11/n1570.html – too honest for this site Jan 30 '16 at 04:52
  • @Olaf Which part are you referring to as incorrect? How C decides which type to use when evaluating expressions with magic numbers? I'm looking at the standard right now but I'm not sure what I'm supposed to be looking for... – intcreator Jan 30 '16 at 05:03
  • 1
    1) The term "magic numbers" is missplaced here. `8` and `5` are _integer constants_ according to the standard, another term is _integer literal_ (to differentiate from symbolic constants of interger type - which C does not support). 2) As there are some "flaws" in your text, I'd recommend to get comfortable with the terminology in the standard in general. That greatly imploves readbility/understandability (& some other -abilities) and constistency with the standard. 3) For this, you might start with the conversions section. Tough reading for a beginner, but worth, if you want to learn. – too honest for this site Jan 30 '16 at 05:12
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/102056/discussion-between-brandaemon-and-olaf). – intcreator Jan 30 '16 at 05:14
  • 1
    Sorry, no time. Just to clarify: That honestly was meant as friendly help,not an offence. Everyone has to learn. – too honest for this site Jan 30 '16 at 05:18
  • I just suggested chat because SO comments are getting mad at me. I really appreciate the help and I did learn a lot. Thanks! – intcreator Jan 30 '16 at 05:21