Trouble with Decimals? =(

This is a discussion on Trouble with Decimals? =( within the C++ Programming forums, part of the General Programming Boards category; I saw the Euler's thread, and i implemented a function which calculates it correctly, however my program is just turning ...

  1. #1
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434

    Trouble with Decimals? =(

    I saw the Euler's thread, and i implemented a function which calculates it correctly, however my program is just turning everything to ints =(

    I'm using floats for the variables that need to be decimals, but every fraction i have it calculate (ie. 1/2), it turns to a decimal first, and then adds it. I don't understand this at all.

    Any help?
    "Anyone can aspire to greatness if they try hard enough."
    - Me

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,590
    Posting your code would attract a far more specific response.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    >> (ie. 1/2), it turns to a decimal first, and then adds it.
    Do you mean it turns to an int first (like 0) and then adds it? When putting literal values into your code, if you want them to be floating point you have to indicate that they are floating point. 1 and 2 are considered integers, and integer division will be performed. 1.0 and 2.0 are considered doubles (which is the default floating point type), and so floating point division will be performed and 0.5 will be returned.

  4. #4
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    Code:
    double x, x2;
    
    x = 1 / 2;
    
    x2 = 1.0 / 2.0;
    
    cout<< x <<" and "<< x2;
    It would end up printing

    Code:
    0 and .5

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,590
    In other words, one is integer division and the other is floating point division.

    1 / 2 is performed as integer division (because they are integer constants), THEN the result is cast to a double by the assignment.

    1.0 / 2.0 is performed as floating point division (because they are float constants), THEN the result is simply assigned to the variable.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    Dankeschon!

    It works now: 2.71828
    "Anyone can aspire to greatness if they try hard enough."
    - Me

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help to show decimals
    By engstudent363 in forum C Programming
    Replies: 4
    Last Post: 02-19-2008, 03:13 PM
  2. Help with Decimals
    By Deadlyaim in forum C++ Programming
    Replies: 8
    Last Post: 09-28-2006, 05:15 PM
  3. trouble scanning in... and link listing
    By panfilero in forum C Programming
    Replies: 14
    Last Post: 11-21-2005, 11:58 AM
  4. C++ program trouble
    By senrab in forum C++ Programming
    Replies: 7
    Last Post: 04-29-2003, 11:55 PM
  5. Decimals to Fractions
    By ToasterPas in forum C++ Programming
    Replies: 4
    Last Post: 12-28-2001, 11:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21