0
#include <stdio.h>

int main()
{
    float a = 355/113;
    printf("%f", a);
    return 0;
}

Why is this returning 3.0000 instead of 3.141592?

Shubh Roy
  • 37
  • 6
  • An excerpt from [How much research effort is expected of Stack Overflow users?](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users): _Asking a question on Stack Overflow should be the last step in your process for finding an answer_ I Googled the words ___C division return int and not float___ which led me to this SO question: [Why dividing two integers doesn't get a float?](https://stackoverflow.com/questions/16221776/why-dividing-two-integers-doesnt-get-a-float) Does it, or any of its duplicate questions, provide an answer for you? – Abra Apr 05 '20 at 01:48
  • Even when you fix the constants to become floating point, the answer is 3.141593 rather than 3.141592 — the value is approximately 3.1415929, compared with π ≈ 3.1415926538979… – Jonathan Leffler Apr 05 '20 at 01:56
  • @Abra ... I tried searching on Google. I haven't completely wrapped my head around the terms in programming so I guess I didn't use the right key words when searching because I wasn't finding what I needed. – Shubh Roy Apr 05 '20 at 02:02

3 Answers3

3

Because 355/113 is integer division, not floating-point division. The decimal portion is getting truncated off before the result is assigned to the float.

Try this instead:

float a = 355.0f / 113.0f;
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • I think it's important to mention that the reason it's integer division is that, in C, types are computed inside-out, or bottom-up. The sort of division done by `/` is decided *only* based on the "intrinsic" type of its arguments, not on the "expected type" of its context. – HTNW Apr 05 '20 at 01:42
  • What is the 'f' for? It works without it. – Shubh Roy Apr 05 '20 at 02:09
  • @ShubhRoy: The `F` suffix denotes a `float` constant; no suffix on a floating point constant denotes a `double` constant, and an `L` suffix denotes a `long double` constant. The case of `F` and `L` is not significant (but `l` and `1` are easily confused; always use the upper-case letters). See also C11 [§6.4.4.2 Floating constants](http://port70.net/~nsz/c/c11/n1570.html#6.4.4.2). – Jonathan Leffler Apr 05 '20 at 02:25
3

The division is being performed using integer arithmetic and the result of the division is converted to a float. If you want float division use floating point literals such as 355.0 and 113.0.

Jim Rogers
  • 4,822
  • 1
  • 11
  • 24
  • Strictly, `355.0` is a `double` literal; `355.0F` is the `float` equivalent (and `355.0L` is the `long double` equivalent). In this context, the difference doesn't matter; in some contexts, it does. See also C11 [§6.4.4.2 Floating constants](http://port70.net/~nsz/c/c11/n1570.html#6.4.4.2). – Jonathan Leffler Apr 05 '20 at 02:22
  • @JonathanLeffler Detail: Strictly, `355.0` is a `double` constant, a _decimal-floating-constant_- not literal. C has _string literals_ and _compound literals_. In C literals can have their address taken, not so with constants. – chux - Reinstate Monica Apr 05 '20 at 02:29
  • @chux-ReinstateMonica — consider my wrists suitably slapped. In colloquial C, they're literals. And the type of `355.0` is `double` rather than `float`. – Jonathan Leffler Apr 05 '20 at 02:44
  • @JonathanLeffler Agree about colloquial C. It was _"strictly"_ that stepped passed casual, hence the comment. Always enjoy your work. – chux - Reinstate Monica Apr 05 '20 at 02:50
2

You are dividing integers but you want float, so just do this:

int main()
{

  float a = 355/(float)113;

  printf("%f", a);
  return 0;
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Everaldo
  • 57
  • 8
  • Thanks for the tip. I've understood the problem. I just need to know how this works. Could I do this with any other data type? – Shubh Roy Apr 05 '20 at 01:54
  • You can perform a "casting" with types, but when you do that, depending on the type, you can lose data – Everaldo Apr 05 '20 at 01:58