CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Dec 2005
    Posts
    2

    simple C++ question

    I am a beginning C++ programmer and I would like to know one thing about variables that is not clear in the book I have.

    When I declare a variable as double do I have to include a decimal?

    for example in the book they use :

    double speed;
    speed = 139000.0;

    if you enter speed = 139000 , will it not still remain a double value since it was declare as double?

    This last example my better illustrate my question.
    suppose I declare the wide char variable 'a' like so:

    wchar_t = ch;
    ch = L'a';

    since I already declared the variable as wide, why do I have to still include the L in front of 'a'?

    does this mean that my double variable is converted to an int value because it has no decimal?

  2. #2
    Join Date
    Aug 1999
    Location
    Germany
    Posts
    2,338

    Re: simple C++ question

    Hi code_slinger and welcome to the forum and to c++

    Well, in the case of the double, it's just more clear and accurate to assign a double to a double. You could use:
    speed = 139000;
    which converts the 13900 to a double and the assigns it to the variable speed. So why not give a double from the beginning?

    The point is that you can make errors here easily. For example:
    speed = 99/10;
    will result in "speed=10.0", because the compiler sees 2 integer values getting devided (99/10 = 9.9) and because they are integer, the compiler rounds them to 10. So the correct version would be:
    speed = 99.0/10.0;

    See the point?


    It's equal with
    wchar_t = ch;
    ch = L'a';

    'a' is a char (not a wchar_t), so you have to convert it. In this case I think the compiler isn't able to convert a wchar from a char, and
    ch='a'
    will fail.

  3. #3
    Join Date
    Dec 2005
    Posts
    2

    Re: simple C++ question

    Thank you, I believe I understand now. So the conversion to double happens after the variable value is "stored"? From your example if you assign speed = 99/10 the 99 and 10 are not converted before the division so the compiler rounds as it would with integers.

    Thak you very much, I wish the book had used that. I guess its obvious?
    anyway good example. (am I understanding the example correctly?)

  4. #4
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: simple C++ question

    Quote Originally Posted by code_slinger
    Thank you, I believe I understand now. So the conversion to double happens after the variable value is "stored"? From your example if you assign speed = 99/10 the 99 and 10 are not converted before the division so the compiler rounds as it would with integers.
    Exactly, if you wanted to calculate the exact division you should do speed = 99.0/10.0 or speed = 99/10.0. In the last example the compiler will auto-cast the 99 to double because you divide by a double.
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  5. #5
    Join Date
    Aug 1999
    Posts
    586

    Re: simple C++ question

    Quote Originally Posted by martho
    speed = 99/10;
    will result in "speed=10.0"
    It results in 9, not 10. Integer division truncates the fractional portion. Rounding of native types never occurs in C++ unless you call a function to implement it. The op should Google for "Usual Arithmetic Conversions" and "C++" for starters.

  6. #6
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: simple C++ question

    Quote Originally Posted by Sef
    It results in 9, not 10. Integer division truncates the fractional portion. Rounding of native types never occurs in C++ unless you call a function to implement it. The op should Google for "Usual Arithmetic Conversions" and "C++" for starters.
    Exactly.
    As a side note, a common trick to perform rounding by exploiting the truncate feature is:
    Code:
    int i = int(99/10+0.5);
    The above is for positive numbers. For negative numbers, you need to subtract 0.5.
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  7. #7
    Join Date
    Aug 1999
    Posts
    586

    Re: simple C++ question

    [QUOTE=Marc G]Exactly.
    As a side note, a common trick to perform rounding by exploiting the truncate feature is:
    Code:
    int i = int(99/10+0.5);
    Won't work for this particular example (as is) but I know what you meant

  8. #8
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: simple C++ question

    Quote Originally Posted by Sef
    Won't work for this particular example (as is) but I know what you meant
    I know, as I said it was a side note on your "Rounding of native types never occurs in C++"
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured