DO NOT BUY "C++ Without Fear" by Brian Overland!!!

As I'm sure all of you guys already know, you cannot pass (directly) an integer to the sqrt function... Well, this guy Brian Overland, who says that he was a Microsoft programmer and attended Yale and blah, blah, blah.... Well, in the first 30 pages of this book he has this code, VERBATIM!!!
#include<iostream>
#include<cmath>
using namespace std;

int main(){
INT n;
int i;
int is_prime=true;

cout<<"Enter a number and press ENTER: ";
cin>>n;

i=2;
while(i<= SQRT(N)){
if(n%i==0)
is_prime=false;
i++;
}
Then he prints the results and system pause return zero....

I'm taking this back to Books-a-million! Ridiculous!!!
Cool site, btw...



You rant in haste.

I have come across this anomaly before between Microsoft compiler and GCC

You will find GCC will compile it OK because GCC will probably auto convert it
to a double but MS won't.

So there appear to be a difference in <cmath> between MIcrosoft and GCC




Compiles without errors or warnings in GCC:
1
2
3
4
5
6
7
#include <iostream>
#include <cmath>

int main(){
	int a=8;
	std::cout <<sqrt(a)<<std::endl;
}


EDIT: Ah, I see. It's likely GCC's standard library has a sqrt(int) overload defined. Okay, it's not standard, but certainly no reason to label a book as crappy and disparage the author.
Last edited on
closed account (zb0S216C)
The declaration of all variables are placed at the top of the scope; does he have a C background? It's better practice to declare variables/objects when you actually need them. That being said, there's multiple things I would change in that code.

If you do actually take it back, and not put it in the bin (literally), buy C++ Primer 4th Ed. instead.

Wazzak
In C++11 you can pass integers to std::sqrt.
@Court
As I'm sure all of you guys already know, you cannot pass (directly) an integer to the sqrt function...


It depends on an implementation. For example if you are using GCC 4.7.1 you may do that. If you are using MS VC++2010 you may not do that, It is implementation defined whether overload C-functions will be placed in the global namespace.

@Peter87
In C++11 you can pass integers to std::sqrt.


In any C++ you can pass integers to sqrt. The problem is that an ambiguity can occur.:)
Last edited on
As for the code itself then it has a bad style. For example it is not clear why is_prime is declared as int instead of bool. Or why the loop continues iterations though it is already clear that the number is not prime. I would rewrite it the following way.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <cmath>

using namespace std;
 
int main()
{
   int n;

   cout << "Enter a number and press ENTER: ";
   cin >> n;
 
   bool is_prime = true;

   for ( int i = 2; is_prime && i <= sqrt( ( double ) n ); i++ )
   {
      is_prime = n % i != 0;
   }

   // and so on
}



Or even the following way


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <cmath>

using namespace std;
 
int main()
{
   int n;

   cout << "Enter a number and press ENTER: ";
   cin >> n;
 
   bool is_prime = n % 2 != 0;

   for ( int i = 3; is_prime && i <= sqrt( ( double ) n ); i += 2 )
   {
      is_prime = n % i != 0;
   }

   // and so on
}

Last edited on
vlad from moscow wrote:
In any C++ you can pass integers to sqrt. The problem is that an ambiguity can occur.:)
What I mean is that the C++11 adds overloads to remove the possibility of ambiguity. See ยง26.8/11.
@Peter87, as I understand this it does not mean that these overloads will be placed in the global namespace. That is it is implemenation-defined whether overloads will be placed in the global namespace or will not.
Topic archived. No new replies allowed.