converting to 'int' from 'double'

This is a discussion on converting to 'int' from 'double' within the C++ Programming forums, part of the General Programming Boards category; How do I fix the following problem? I am not intentionally using a double but I'm guessing the function sqrt() ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    10

    converting to 'int' from 'double'

    How do I fix the following problem?

    I am not intentionally using a double but I'm guessing the function sqrt() in the cmath library is a double because this is the line the error occurs on?

    Error: converting to 'int' from 'double'

    Is there a way to make the sqrt function be an int? If so, how do I do this?
    Thanks.

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,236
    Your compiler is whining because you are not explicitly casting the result of sqrt(). Try something like this:

    Code:
    int result = (int)sqrt(x);
    Just be sure you understand what's happening here -- if x is not a perfect square, the result of sqrt() will not be an integer. Truncating it to an integer type may or may not be the right thing to do.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    10
    thank you very much. my error is gone. i appreciate it.

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    17
    It's called Typecasting. Try it out with some other problems. :-)

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,445
    brewbuck's example is from C, though it can be used in C++. In C++, it would be more correct to pick the appropriate cast, in this case static_cast:
    Code:
    int result = static_cast<int>(sqrt(x));
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,236
    Quote Originally Posted by laserlight View Post
    brewbuck's example is from C, though it can be used in C++. In C++, it would be more correct to pick the appropriate cast, in this case static_cast:
    Code:
    int result = static_cast<int>(sqrt(x));
    True. C-style casts are considered "unsafe" in C++ (technically they are unsafe in C as well), but for casting between numeric types I wouldn't worry too much about the difference.

    Does a C-style cast from double to int obey different rounding rules than a static_cast? I don't know.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Does a C-style cast from double to int obey different rounding rules than a static_cast? I don't know.
    As far as I know, they do the same thing as far as primitive types are concerned.

    http://bytes.com/groups/c/540140-sta...-c-style-casts

    Both should produce identical machine code.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  2. Working with random like dice
    By SebastionV3 in forum C++ Programming
    Replies: 10
    Last Post: 05-26-2006, 09:16 PM
  3. Converted from Dev-C++ 4 to Dev-C++ 5
    By Wraithan in forum C++ Programming
    Replies: 8
    Last Post: 12-03-2005, 06:45 AM
  4. 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
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 04:26 PM

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