LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-20-2005, 03:58 PM   #1
lmvent
LQ Newbie
 
Registered: Sep 2005
Posts: 24

Rep: Reputation: 15
sqrt function


Code:
#include <iostream>
#include <vector>
#include <cmath>
#include <iomanip>

using namespace std;

int main(void)
{   cout << "Input the largest positive integer to check: ";
    int maxN;
    cin >> maxN;
    vector <int> primes;
    int n;
    n = (int)sqrt(maxN);
    for (int i = maxN; i > n; i = i - 1)
    {   bool isPrime = true;
        for (int j = 2; j <= n; j = j + 1)
        {   if (i%j == 0)
            {   isPrime = false;
            }
        }
            if (isPrime)
            {   //cout << i << " ";
                primes.push_back(i);
            }
    }
    cout << "\n\nThere are " << primes.size() << " prime numbers <= " << maxN << "\n";
    return 0;
}

I get an error that says sqrt(int&) is ambiguous, but I have done it in another code similarly.

Code:
#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

int main(void)
{   int y, x;
    for(y = -10; y <= 10; y = y + 1)
    {   for(x = -10; x <= 10; x = x + 1)
        {   float res;
	    int ires;
	    ires = (int)sqrt(x*x + y*y);
	    if(ires == 10)
	    {   cout << "*";
	    }
	    if(ires == 7 && y > 0)
	    {   cout << "=";
	    }
	    res = (sqrt((x^2) + (y^2)));
	    if(res < 1.25)
	    {   cout << "*";
	    }
	    res = (sqrt(((x + 4)^2) + ((y + 4)^2)));
	    if(res < 2)
	    {   cout << "\\";
	    }
	    res = (sqrt(((x - 4)^2) + ((y + 4)^2)));
	    if(res < 2)
	    {   cout << "/";
	    }
            cout << "   ";
	}
        cout << endl;
    }
return 0;
}
what makes it different?
 
Old 10-20-2005, 04:10 PM   #2
jailbait
LQ Guru
 
Registered: Feb 2003
Location: Virginia, USA
Distribution: Debian 12
Posts: 8,337

Rep: Reputation: 548Reputation: 548Reputation: 548Reputation: 548Reputation: 548Reputation: 548
"I get an error that says sqrt(int& is ambiguous"

The variable passed in sqrt should be real or complex, not integer. So the compiler is telling you that sqrt may not calculate the answer the way that you think it will when passing a wrong variable type.

http://gcc.gnu.org/onlinedocs/gcc-4....tran/SQRT.html

-------------------------
Steve Stites
 
Old 10-20-2005, 04:13 PM   #3
lmvent
LQ Newbie
 
Registered: Sep 2005
Posts: 24

Original Poster
Rep: Reputation: 15
How come it works for the second code i posted though?
 
Old 10-20-2005, 04:14 PM   #4
spooon
Senior Member
 
Registered: Aug 2005
Posts: 1,755

Rep: Reputation: 51
I compiled it with no problems. What compiler are you using?
 
Old 10-20-2005, 04:20 PM   #5
lmvent
LQ Newbie
 
Registered: Sep 2005
Posts: 24

Original Poster
Rep: Reputation: 15
You compiled the first one with no problem?? I am using g++ on cygwin, which could also be my problem.
 
Old 10-20-2005, 04:51 PM   #6
lmvent
LQ Newbie
 
Registered: Sep 2005
Posts: 24

Original Poster
Rep: Reputation: 15
I went to a linux computer and I get the same error
 
Old 10-20-2005, 07:53 PM   #7
spooon
Senior Member
 
Registered: Aug 2005
Posts: 1,755

Rep: Reputation: 51
I used computers with GCC 3.4.1, GCC 3.4.4, and GCC 4.0.1 and it compiled without problems. When I used a computer with GCC 3.2.3, it gave the error you mentioned.

The problem it said in that version is that "sqrt" is defined for "float", "double", and "long double", and it doesn't know which one to use for "int" (hence the ambiguity). You have to explicitly convert it to double or something ("sqrt((double)maxN)"). In newer versions of GCC, they templated it for all other types so there is no ambiguity.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
red hat 9.0 gcc (ceil, floor and sqrt function errors) ?? gemini_shooter Linux - Enterprise 1 06-04-2005 04:23 PM
Problems with Sqrt() nostrum Programming 2 04-15-2005 12:24 PM
How do you write your own sqrt()? The_Nerd Programming 6 11-02-2004 08:08 PM
Bash sqrt problem Dimitris Programming 1 08-26-2003 03:30 PM
sqrt() function IamDaniel Programming 6 08-09-2003 10:24 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:04 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration