# Fraction folly - rounding down to uselessness.

• 03-17-2004
Jets Connor
Fraction folly - rounding down to uselessness.
Hello, everyone. First time poster here. I'm rather inexperienced with C++, and I was wondering if anyone could help me out.

The problem is that I have declared a float, which I have called pi, but when I make an attempt to add, say, (1 / 5) to pi, the addition is rounded to the nearest whole number. (In this case, zero.)

So, is there any way of doing this? It is necessary that I add the number in numerator/denominator style, so decimal is out of the question.

Thanks.
• 03-17-2004
hk_mp5kpdw
The problem is that 1 and 5 are looked at by the compiler as integers and therefore the operation as a whole is looked at as being an integer division operation. You need to let the compiler know you want floating point division instead. You can do this in different ways: either cast the value to floats or simply do this:

Code:

`1.0f / 5.0f`
The compiler will see the values as floating point values and should therefore use floating point division. 1.f/5.f should also work if you don't want to have those zeroes there.
• 03-17-2004
DougDbug
A couple of topics to look up.
Hmmm... In most cases, when you combine different types in a single statement, C++ will promote the types automatically, and you shouldn't get this problem.

Look-up typecasting (aka casting) and type-promotion.

Typecasting makes one type of variable "behave like' another type. The simplest way to typecast is to put the desired type in parenthesis in front of the variable:
Code:

```int x = 1; float y; y= (float)x;  // Cast x to a float.    (not really required here)```
Now, typecasting should be used rarely. Generally, you should design your programs to use the "correct" variable types. IMHO, about the only time you should use typecasting is because you , you need to pass the "wrong" type into a library function that you didn't write.
• 03-17-2004
XSquared
>>Hmmm... In most cases, when you combine different types in a single statement, C++ will promote the types automatically, and you shouldn't get this problem.
It happens in his code too. It does the (1/5) with 1 and 5 being treated as integers, resulting in 0, and then casts the result to a float. If you did (1.0f / 5), or (1/5.0f), it would probably work correctly, although it's much better to just do explicity casting.
• 03-17-2004
Jets Connor
Thankyou. All good now!
• 03-17-2004
Prelude
>it would probably work correctly
Assuming the compiler is hopelessly broken it would probably work correctly. Otherwise it would definitely work correctly. :)

>although it's much better to just do explicity casting
Can you elaborate? I can think of very few times when it's much better to do explicit casting, and fixing 1/5 is not one of them. ;)
• 03-17-2004
XSquared
I'm thinking in terms of code readability, the implicit casting can get confusing at times. But if you say it's better not to do explicit casting, I'll take your word for it. :P
• 03-17-2004
Prelude
>I'm thinking in terms of code readability, the implicit casting can get confusing at times.
Any expression that involves two or more types is sure to cause confusion. The rules for promotion are interesting, especially when signedness comes into play...

>But if you say it's better not to do explicit casting, I'll take your word for it. :P
I prefer to avoid casting where I can in C++. But that's a stylistic issue. An explicit cast doesn't really hurt anything in this case, it's just ugly. ;)