# (float answer = 1/3) = 0?!

Page 1 of 1

## 3 Replies - 4488 Views - Last Post: 27 August 2009 - 03:15 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=122572&amp;s=72043a13eff7a8bb8effdc37486256ba&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 IngeniousHax

• |>|20-514<|{3|2

Reputation: 84
• Posts: 1,385
• Joined: 28-March 09

# (float answer = 1/3) = 0?!

Posted 27 August 2009 - 03:09 PM

```#include <stdio.h>
#include <stdlib.h>

int main(){
float answer = (1 / 3);

printf("Why does this equal zero ---> %f\n", answer);
return(0);
}

```

I can't figure out why is equals zero... should equal .3333333

This post has been edited by IngeniousHax: 27 August 2009 - 03:10 PM

Is This A Good Question/Topic? 0

Reputation:

## Re: (float answer = 1/3) = 0?!

Posted 27 August 2009 - 03:11 PM

Because you're dividing integers. Integers don't have decimal places, they truncate everything.

```float answer = (1.f / 3);

```

Now you're dividing a float by integer, in which case the result is also a float and thus retains the decimal places.

This post has been edited by Neumann: 27 August 2009 - 03:14 PM

### #3 IngeniousHax

• |>|20-514<|{3|2

Reputation: 84
• Posts: 1,385
• Joined: 28-March 09

## Re: (float answer = 1/3) = 0?!

Posted 27 August 2009 - 03:12 PM

Doh, they need to be 1.0 and 3.0 huh?

Reputation:

## Re: (float answer = 1/3) = 0?!

Posted 27 August 2009 - 03:15 PM

Well, in that case you will be dividing doubles. Better stick an f at the end to make them floats. Also note that only one of them needs to be float.