# int rather than a double

• (2 Pages)
• 1
• 2

## 16 Replies - 1757 Views - Last Post: 24 April 2011 - 09:45 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=229140&amp;s=d1579ad9e2fb0912b72010ce551bcb69&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 JamesConley

• D.I.C Regular

Reputation: 19
• Posts: 341
• Joined: 09-March 09

# int rather than a double

Posted 24 April 2011 - 07:39 PM

Hey everyone! I just started learning C++ and am currently following Bjarne Stroustrup's "Programming Principles and Practice using C++". Specifically I am doing a basic operators exercise on pg.68 (for those that own the book).

Book says: Modify what I originally did to read an int rather than a double. Note that sqrt() is not defined for an int so assign n to a double and take the sqrt() of that.

Output1:
```//Exercise Operators on pg.67

#include "std_lib_facilities.h"

int main()
{
double   n;
cin>>  n;
cout<< "n == " << n
<<"\nn+1=="<<n+1
<<"\nthreetimes n == " <<3*n
<<"\ntwice n == " << n+n
<<"\nn squared == " << n*n
<<"\n half of n == " << n/2
<<"\nsquare root of n == "<<sqrt(n)
<<endl;
keep_window_open();
}
```

Output2 w/int:
```//Exercise Operators on pg.67

#include "std_lib_facilities.h"

int main()
{
int   n;
cin>>  n;
cout<< "n == " << n
<<"\nn+1=="<<n+1
<<"\nthreetimes n == " <<3*n
<<"\ntwice n == " << n+n
<<"\nn squared == " << n*n
<<"\n half of n == " << n/2
<<"\nsquare root of n == "<<sqrt(n)
<<endl;
keep_window_open();
}

```

I appreciate your time with this. I know it's nothing major but I feel I should be getting an error I also feel that I'm probably doing something stupid and am just overlooking it.

Thanks!

#### Attached image(s)

Is This A Good Question/Topic? 0

## Replies To: int rather than a double

### #2 KYA

• Wubba lubba dub dub!

Reputation: 3202
• Posts: 19,232
• Joined: 14-September 07

## Re: int rather than a double

Posted 24 April 2011 - 07:45 PM

[I just tested it out and it doesn't make any difference, but] what is your warning level setting?

Under project settings->C/C++->General->Warning Level/Treat Warnings as Errors?

I cannot get VS to ignore the ambiguous overload, congratulations

### #3 r.stiltskin

• D.I.C Lover

Reputation: 2032
• Posts: 5,435
• Joined: 27-December 05

## Re: int rather than a double

Posted 24 April 2011 - 07:54 PM

Sorry to be dense, but I'm not sure I understand your question.

Are you complaining that you aren't getting a run-time error when an int is given as argument to sqrt?

### #4 JamesConley

• D.I.C Regular

Reputation: 19
• Posts: 341
• Joined: 09-March 09

## Re: int rather than a double

Posted 24 April 2011 - 08:03 PM

KYA, on 25 April 2011 - 03:45 AM, said:

[I just tested it out and it doesn't make any difference, but] what is your warning level setting?

Under project settings->C/C++->General->Warning Level/Treat Warnings as Errors?

I cannot get VS to ignore the ambiguous overload, congratulations

Warning level is Level3 (/W3).

Treat warnings as errors is No (/WX-).

I'm guessing these should be at different settings? I have yet to learn about properties or settings of this type in VS. Any info as to where I can get more documentation on these settings would be great! I'm sure as time goes by I will learn more about these.

Thanks KYA!

r.stiltskin, on 25 April 2011 - 03:54 AM, said:

Sorry to be dense, but I'm not sure I understand your question.

Are you complaining that you aren't getting a run-time error when an int is given as argument to sqrt?

Yes I am. I suppose its sorta neat that I'm not getting an error but when I read and am told that sqrt()is not defined for int I expect an error. As for now I suppose this isn't that big of an issue but I would just assume to figure it out now rather than later when I'm writing something important.

Also,here is the header that was given with the book to use.

```/*
simple "Programming: Principles and Practice using C++" course header to
be used for the first few weeks.
It provides the most common standard headers (in the global namespace)
and minimal exception/error support.

Students: please don't try to understand the details of headers just yet.
All will be explained. This header is primarily used so that you don't have
to understand every concept all at once.

Revised April 25, 2010: simple_error() added
*/

#ifndef H112
#define H112 201004L

#include<iostream>
#include<fstream>
#include<sstream>
#include<cmath>
#include<cstdlib>
#include<string>
#include<list>
#include<vector>
#include<algorithm>
#include<stdexcept>

//------------------------------------------------------------------------------

#ifdef _MSC_VER
#include <hash_map>
using stdext::hash_map;
#else
#include <ext/hash_map>
using __gnu_cxx::hash_map;

namespace __gnu_cxx {

template<> struct hash<std::string>
{
size_t operator()(const std::string& s) const
{
return hash<char*>()(s.c_str());
}
};

} // of namespace __gnu_cxx
#endif

//------------------------------------------------------------------------------

#define unordered_map hash_map

//------------------------------------------------------------------------------

typedef long Unicode;

//------------------------------------------------------------------------------

using namespace std;

template<class T> string to_string(const T& t)
{
ostringstream os;
os << t;
return os.str();
}

struct Range_error : out_of_range {	// enhanced vector range error reporting
int index;
Range_error(int i) :out_of_range("Range error: "+to_string(i)), index(i) { }
};

// trivially range-checked vector (no iterator checking):
template< class T> struct Vector : public std::vector<T> {
typedef typename std::vector<T>::size_type size_type;

Vector() { }
explicit Vector(size_type n) :std::vector<T>(n) {}
Vector(size_type n, const T& v) :std::vector<T>(n,v) {}
template <class I>
Vector(I first, I last) :std::vector<T>(first,last) {}

T& operator[](unsigned int i) // rather than return at(i);
{
if (i<0||this->size()<=i) throw Range_error(i);
return std::vector<T>::operator[](i);
}
const T& operator[](unsigned int i) const
{
if (i<0||this->size()<=i) throw Range_error(i);
return std::vector<T>::operator[](i);
}
};

// disgusting macro hack to get a range checked vector:
#define vector Vector

// trivially range-checked string (no iterator checking):
struct String : std::string {

String() { }
String(const char* p) :std::string(p) {}
String(const string& s) :std::string(s) {}
template<class S> String(S s) :std::string(s) {}
String(int sz, char val) :std::string(sz,val) {}
template<class Iter> String(Iter p1, Iter p2) : std::string(p1,p2) { }

char& operator[](unsigned int i) // rather than return at(i);
{
if (i<0||size()<=i) throw Range_error(i);
return std::string::operator[](i);
}

const char& operator[](unsigned int i) const
{
if (i<0||size()<=i) throw Range_error(i);
return std::string::operator[](i);
}
};

#ifndef _MSC_VER
namespace __gnu_cxx {

template<> struct hash<String>
{
size_t operator()(const String& s) const
{
return hash<std::string>()(s);
}
};

} // of namespace __gnu_cxx
#endif

struct Exit : runtime_error {
Exit(): runtime_error("Exit") {}
};

// error() simply disguises throws:
inline void error(const string& s)
{
throw runtime_error(s);
}

inline void error(const string& s, const string& s2)
{
error(s+s2);
}

inline void error(const string& s, int i)
{
ostringstream os;
os << s <<": " << i;
error(os.str());
}

#if _MSC_VER<1500
// disgusting macro hack to get a range checked string:
#define string String
// MS C++ 9.0 have a built-in assert for string range check
// and uses "std::string" in several places so that macro substitution fails
#endif

template<class T> char* as_bytes(T& i)	// needed for binary I/O
{
void* addr = &i;	// get the address of the first byte
// of memory used to store the object
return static_cast<char*>(addr); // treat that memory as bytes
}

inline void keep_window_open()
{
```

### #5 KYA

• Wubba lubba dub dub!

Reputation: 3202
• Posts: 19,232
• Joined: 14-September 07

## Re: int rather than a double

Posted 24 April 2011 - 08:06 PM

MSDN is probably the best resource for VS related items.

As for the compile time error, no idea. Passing an integer to sqrt() should trigger "error C2668: 'sqrt' : ambiguous call to overloaded function" regardless of settings.

### #6 JamesConley

• D.I.C Regular

Reputation: 19
• Posts: 341
• Joined: 09-March 09

## Re: int rather than a double

Posted 24 April 2011 - 08:16 PM

Well I'm sure I will run into an error sooner or later. Thanks for looking into it!

### #7 r.stiltskin

• D.I.C Lover

Reputation: 2032
• Posts: 5,435
• Joined: 27-December 05

## Re: int rather than a double

Posted 24 April 2011 - 08:22 PM

I can't get gcc or g++ to complain about sending an int to sqrt no matter how I set the compiler flags. There are various implicit conversions (type promotions) in function arguments, although I can't find any documentation specifically mentioning int to double.

On the other hand, it never surprised or bothered you that you could do this:
```double x = 4.15;
int y = 4;
double z = x + y;

```

did it?

I realize an operator isn't exactly the same as a function, but similar principles should apply. Regarding the + operator I found

"When both operands have an arithmetic type, the usual arithmetic conversions on the operands are performed. The result has the type produced by the conversions on the operands and is not an lvalue."

I guess int to double falls under the heading of "the usual arithmetic conversions".

### #8 Oler1s

• D.I.C Lover

Reputation: 1397
• Posts: 3,884
• Joined: 04-June 09

## Re: int rather than a double

Posted 24 April 2011 - 08:23 PM

Are these part of the conversion rules in C++0x? I see:

Quote

A prvalue of an integer type or of an unscoped enumeration type can be converted to a prvalue of a floating point type.

### #9 r.stiltskin

• D.I.C Lover

Reputation: 2032
• Posts: 5,435
• Joined: 27-December 05

## Re: int rather than a double

Posted 24 April 2011 - 08:32 PM

Oler1s, on 24 April 2011 - 10:23 PM, said:

Are these part of the conversion rules in C++0x? I see:

Quote

A prvalue of an integer type or of an unscoped enumeration type can be converted to a prvalue of a floating point type.

It's nice that someone here can read ... and has the proper references available.

Where did you find that?

### #10 Oler1s

• D.I.C Lover

Reputation: 1397
• Posts: 3,884
• Joined: 04-June 09

## Re: int rather than a double

Posted 24 April 2011 - 08:35 PM

> Where did you find that?

Off the C++0x standard draft. See the Working Group website.

Actually, pay close attention the conversion rules. It's not just implicit promotions that are OK now. It's any conversion. Bigger to smaller is perfectly ok. E.x.: "A prvalue of an integer type can be converted to a prvalue of another integer type. A prvalue of an unscoped enumeration type can be converted to a prvalue of an integer type.""

That's....anything to anything. OK: "If the destination type is signed, the value is unchanged if it can be represented in the destination type (and bit-field width); otherwise, the value is implementation-defined."

See the implications of these conversion rules?

### #11 r.stiltskin

• D.I.C Lover

Reputation: 2032
• Posts: 5,435
• Joined: 27-December 05

## Re: int rather than a double

Posted 24 April 2011 - 09:00 PM

Just that the programmer has to be cautious about relying on implicit type conversions. Is there anything else I should be seeing there?

For what it's worth, my GCC compiler happily lets me do this with no warning
```    int b;
unsigned int a = 4000000000u;
b = a;

```

even though it's going to overflow.

### #12 Oler1s

• D.I.C Lover

Reputation: 1397
• Posts: 3,884
• Joined: 04-June 09

## Re: int rather than a double

Posted 24 April 2011 - 09:07 PM

> Just that the programmer has to be cautious about relying on implicit type conversions.

But part of having a type system is that there is a mechanism in the language to avoid unintended constructs. It's not OK to just have a "be careful" sign.

> Is there anything else I should be seeing there?

I'm not the right person to ask.

> For what it's worth, my GCC compiler happily lets me do this with no warning

I think I managed to pull out a warning with some combination of Wall, ansi, pedantic, and std=c++98

### #13 JamesConley

• D.I.C Regular

Reputation: 19
• Posts: 341
• Joined: 09-March 09

## Re: int rather than a double

Posted 24 April 2011 - 09:12 PM

r.stiltskin, on 25 April 2011 - 04:32 AM, said:

Oler1s, on 24 April 2011 - 10:23 PM, said:

Are these part of the conversion rules in C++0x? I see:

Quote

A prvalue of an integer type or of an unscoped enumeration type can be converted to a prvalue of a floating point type.

It's nice that someone here can read ... and has the proper references available.

Where did you find that?

I can read and if I had references or knew how to fix my problem I wouldn't post on a programming forum. Is it bothering you to answer my post? If it is why are you responding?

I am new to C++. I don't know if you READ that. I wouldn't ask for help if I didn't need it.

### #14 r.stiltskin

• D.I.C Lover

Reputation: 2032
• Posts: 5,435
• Joined: 27-December 05

## Re: int rather than a double

Posted 24 April 2011 - 09:20 PM

Sorry, that wasn't meant as a dig at you. It was directed at all of us who were "supposed" to be able to answer your question.

Myself included.

And it certainly doesn't bother me to answer your post. It was an excellent question & generated, I thought, a worthwhile discussion.

And jeez, why so touchy?

### #15 JamesConley

• D.I.C Regular

Reputation: 19
• Posts: 341
• Joined: 09-March 09

## Re: int rather than a double

Posted 24 April 2011 - 09:26 PM

r.stiltskin, on 25 April 2011 - 05:18 AM, said:

Sorry, that wasn't meant as a dig at you. It was directed at all of us who were "supposed" to be able to answer your question.

Myself included.

And it certainly doesn't bother me to answer your post. It was an excellent question & generated, I thought, a worthwhile discussion.

Misunderstanding. I have seen and ran into people who verbally rape you/someone for posting a question. I understand if it was incredibly stupid or in the wrong section. I thought this was a good question because I'm expecting an error and not getting one.

I'm still working with it. Could it be an issue with the header file, perhaps? I have never had experience up until now with including a header file. The book suggests that it will get into more detail in chapter 23. I'm only on chapter 4...

Is the header file setting the rules and libraries for what I'm writing in that specific project or .cpp file? (I may have worded that incorrectly by using "rules")I plan on skipping ahead to read about it but a little insight for now wouldn't help either.

Thanks guys and sorry for jumping the gun!