# Confused about floats, ints, and doubles

#1

I get all these warnings about int being converted to float, and possible loss of precision. I don’t want to change all my int constants to floats. Why does this error appear? I thought float function parameters were promoted to double anyway? Why aren’t the arguments simply double?

Why can’t I pass a const Rectangle& where a const Rectangle& is expected? I thought they could be represented identically. For example, why can’t I do this:

``````Graphics g;
g.drawRoundedRectangle( getLocalBounds(), 2, 1 );``````

As for the int to float, this happens every time I make a rectangle’s coordinates take on fractional values. For example:

``````Rectangle<int> r;
g.drawRoundedRectangle( r.getX()+0.5, r.getY()+0.5, r.getWidth()-1, r.getHeight()-1 );``````

Through a template member function there should be a way to promote a Rectangle to a Rectangle (or Rectangle?):

``````Rectangle<int> r;
g.fillRect( r.reduced( 0.25, 0.25 ) ); // promote to Rectangle<float>``````

#2

Not much point in moaning about the way c++ handles types! I’ve never found it a problem though, it’s not exactly a big chore to have to type 1.0f instead of 1, and it makes it clear to the reader what’s going on.

You mean like Rectangle::toFloat() and Rectangle::getSmallestIntegerContainer() ?

#3

those look like useful functions. I guess I must be blind!

I’m porting a ton of drawing code and I would rather not change every integer constant into a float constant.

#4

Why std::floor instead of std::ceil for x2 and y2?

``````    const Rectangle<int> getSmallestIntegerContainer() const throw()
{
const int x1 = (int) std::floor (static_cast<float> (x));
const int y1 = (int) std::floor (static_cast<float> (y));
const int x2 = (int) std::floor (static_cast<float> (x + w + 0.9999f));
const int y2 = (int) std::floor (static_cast<float> (y + h + 0.9999f));

return Rectangle<int> (x1, y1, x2 - x1, y2 - y1);
}``````

#5

Good question. I’m pretty sure there was a good reason, but that code’s very old and I can’t remember why I did it that way… Probably I was trying to remain compatible with an environment that didn’t have ceil() or something… It does look like something that I could update now.