Thread: why square functions returns double instead of integer?

  1. #1
    Registered User
    Join Date
    Jul 2014
    Posts
    41

    why square functions returns double instead of integer?

    why are functions for finding square written as
    Code:
    double square(double x){
    	return x*x;
    }
    instead of
    Code:
    int square(int x){
    	return x*x;
    }

  2. #2
    Registered User
    Join Date
    Dec 2010
    Location
    Trinidad, CO (log cabin in middle of nowhere)
    Posts
    148
    I guess because some folks want to square floating point numbers. I love easy questions!

  3. #3
    Registered User
    Join Date
    Jul 2014
    Posts
    41
    yeah maybe but according to me, the latter also works fine.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by V8cTor View Post
    yeah maybe but according to me, the latter also works fine.
    Of course it does, but what if you want to square pi? Or e? what about some other non-whole number? What are you going to do then?
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by V8cTor
    why square functions returns double instead of integer?
    I think that your question makes an incorrect assumption to begin with: there is no function named square in the C++ standard library. There are examples of functions that coincidentally are named square, e.g.,
    Quote Originally Posted by C++11 Clause 7.1.5 Paragraph 1 (part)
    Code:
    constexpr int square(int x) {  // OK: definition
        return x * x;
    }
    And lo and behold, the example is of your latter variety, so in fact "functions for finding square" are written like that! Yet this is just an example. You can define your function named square as you see fit. You can even overload:
    Code:
    double square(double x) {
        return x * x;
    }
    
    int square(int x) {
        return x * x;
    }
    Or perhaps define a function template:
    Code:
    template<typename T>
    T square(T x) {
        return x * x;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 02-19-2015, 12:18 AM
  2. Replies: 4
    Last Post: 02-11-2011, 03:29 PM
  3. Double returns 1.#INF
    By jae5086 in forum C++ Programming
    Replies: 3
    Last Post: 10-14-2010, 05:19 AM
  4. Checking for integer after square root
    By Boomba in forum C++ Programming
    Replies: 13
    Last Post: 01-04-2006, 10:22 PM
  5. I need a function like ceil which returns an integer
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 09-20-2001, 10:16 AM