Thread: Data type problem

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    2

    Data type problem

    Code:
    double x ;
    x = 20 -730531.5;
    cout<< x;
    when I run the code the result is -730512
    why?

    shouldn't it be -730511.5

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    20 is an integer.
    int - double = int....

    Don't make me write a proof for it.

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    2
    okay thanks but what shall I do to get result -730511.5

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    One would think so, but perhaps you don't use enough precision to show the decimals?

    Something like this would show the exact value:
    Code:
    	cout << fixed << x;
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by MacGyver View Post
    20 is an integer.
    int - double = int....

    Don't make me write a proof for it.
    Nope, that's not how it works. If either side of an expression is a "bigger" type, the other side gets "promoted" to the "bigger" type, e.g. int + char or char + int makes the char int before adding, double + float makes the float double before adding, and int + double makes the int into double before add.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Actually, I'm wrong.

    Code:
    #include <iostream>
    
    int main()
    {
    	double y;
    	
    	y = 730531.5;
    	
    	std::cout << y << std::endl;
    	
    	return 0;
    }
    Output for me:

    Code:
    730532
    Doubles and floats are just approximations. They aren't never 100% correct. This is probably what you're seeing.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Actually, a double (assuming it's IEEE-754 64-bit variant) should be good enough for about 15 digits - and integers are never inaccurate - just limited in range, which is why we use floating point types to represent numbers outside of the integer range.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Try this:
    Code:
    #include <iomanip>
    ...
    
    	std::cout << fixed << y << std::endl;
    Mainframe assembler programmer by trade. C coder when I can.

  9. #9
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Here's why:

    The default manipulator, "normal", is good for 6 digits. Had your example been 73053.5, it would have came out as you expected.

    (I just learned this too!) Todd
    Mainframe assembler programmer by trade. C coder when I can.

  10. #10
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    I think I prefer printf()'s way of printing digits.

    /me steps away as he starts another C vs C++ flamewar.

  11. #11
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Quote Originally Posted by MacGyver View Post
    Doubles and floats are just approximations. They aren't never 100&#37; correct. This is probably what you're seeing.
    You get approximations when decimal fractions (in base 10) cannot be saved in base 16 evenly. With .5, that will never be a problem, since 8 is half of 16 and it divides evenly. Its fractions like .6 and such where you'll get rounding.

    Todd
    Mainframe assembler programmer by trade. C coder when I can.

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Todd Burch View Post
    You get approximations when decimal fractions (in base 10) cannot be saved in base 16 evenly. With .5, that will never be a problem, since 8 is half of 16 and it divides evenly. Its fractions like .6 and such where you'll get rounding.

    Todd
    Now you're using your IBM hex-float format again. In IEEE-754 floating point, it's when the fraction can't be described precisely in binary, and the fraction is actually counted from the first number, so for example 0.1 is not precise, which means that all 2^n multiples of 0.1 are also subject to the same thing. 0.7 is the same sort of issue, along with several other numbers. 0.5 is not a problem, and 2^n * 0.5 is obviously also no problem.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    I think we just said the same thing.
    Mainframe assembler programmer by trade. C coder when I can.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. data type problem
    By td4nos in forum C Programming
    Replies: 3
    Last Post: 06-02-2009, 09:49 AM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM
  4. Replies: 4
    Last Post: 06-14-2005, 05:45 AM
  5. C diamonds and perls :°)
    By Carlos in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 05-16-2003, 10:19 PM