Thread: Fraction folly - rounding down to uselessness.

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    2

    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.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    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.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    Lightbulb 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.
    Last edited by DougDbug; 03-17-2004 at 01:37 PM.

  4. #4
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    >>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.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    2
    Thankyou. All good now!

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >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.
    My best code is written with the delete key.

  7. #7
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    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
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >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.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with fraction division function
    By trippedwire in forum C++ Programming
    Replies: 6
    Last Post: 10-24-2004, 11:38 AM
  2. A tragedy!
    By Aakash Datt in forum C++ Programming
    Replies: 4
    Last Post: 05-01-2003, 06:57 PM
  3. Overloading insertion and extraction problem
    By Curwa in forum C++ Programming
    Replies: 1
    Last Post: 01-15-2003, 09:20 PM
  4. Help with Fraction class
    By cheeisme123 in forum C++ Programming
    Replies: 0
    Last Post: 06-04-2002, 07:48 AM
  5. reducing a fraction
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 03-13-2002, 08:56 PM