Thread: Question about data types and basic arithmatic

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    21

    Question about data types and basic arithmatic

    Hi,

    Why can you put integer values into a float variable and there is no error? maybe I am confused about float ranges but I thought it only contained decimals.

    I had two integers, 10 and 3, and tried to divide them to get a result as 3.333 by having the total as a float variable. However, it only returned 3.000.

    But, when I declared 10 and 3 as float, I got the result I looked for.

    Can someone clarify the possible values you can store in:
    integer
    double
    float

    Thanks

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    integers are whole numbers. The integer part of 10/3 is 3

    floats and doubles are the same, except for range and precision (doubles have more of both, compared to floats).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    21
    So what is the proper way to do 10/3 to get a decimal result?

    Is it to store 10 and 3 as floats or is it to use the casting method: a = (float) 10/3?

    C - The float and double Data Types and the sizeof Operator - Casting, Scientific Notation

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Try 10/3.0
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    The result is a float when any one of the numerator or denominator is a float. So you can do something like this to get float value.
    10.0/3 or 10/3.0
    The other way is to cast as you have shown.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    >> The other way is to cast as you have shown.
    Cool! So I can have my integer result pointlessly made into a floating point number.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extending basic data types.
    By nempo in forum C++ Programming
    Replies: 23
    Last Post: 09-25-2007, 03:28 PM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. Basic Data types help
    By viryonia in forum C Programming
    Replies: 6
    Last Post: 03-10-2003, 11:00 AM
  4. Basic Data types continue
    By viryonia in forum C Programming
    Replies: 6
    Last Post: 03-10-2003, 10:21 AM