c++static-castceil

How do I get ceil to work when variable on left of '=' is an int, and variable on right is a double?


EDIT: To rephrase this question, how do I avoid truncation when assigning a double value to an int variable that displays and rounds up said value using ceil? Without changing their declaration, if possible.

My teacher gave us an assignment where we need to make a formula which outputs 1 x per 3 y, and use ceil to round up the value of variable x so that when y exceeds each 6, an extra x is added.

IIRC, he said during class that we need to use static_cast because declaring variables to data types that don't make sense for them is bad programming practice. (E.g., You can't have half a person.)

At first, ceil wasn't working for me. I think I figured out why:

x = (y / 6.0);

I keep getting warning C4244. I think what's happening is, because x is an int, it's truncating the double value from the formula. When I declared my int variables as doubles, ceil worked and the number rounded up in the output.

I don't think I'm allowed to declare those variables as doubles, though. And I think I'm using static_cast wrong, the examples of it I've seen look like they're meant to be used on the right of '='. In which case, I'm not sure how to achieve what I want.

These are some ways I've tried to rearrange static_cast to use it:

static_cast<double>(x) = (y / 60);     // 1 (gives error EO137: must be modifiable lvalue)
  
x = static_cast<double>(x = y / 6.0);  // 2

static_cast<double>(x);                // 3
x = (y / 6.0);                  

x = (y / 6.0);                         // 4
static_cast<double>(x);

x = static_cast<double>(x);            // 5 (gives error C4700: uninitialized variable 'x' used)
x = (y / 6.0);

x = (y / 6.0);
x = static_cast<double>(x);            // 6

(double)(int)x;
x = (y / 60);                          // 7 (another E0137)

And this is the ceil() statement:

cout << "\n\nThank you, " << name <<
    ". You've been assigned " << ceil(x) << " x's."

The C4244 warning stays through all of these. I keep running across a paradox: casting x would have to happen before the formula to assign doubles to it, but x can't be uninitialized when I cast it.

I'd be happy for anyone to point out how I'm being an idiot here, and put me back on track, because I have spent way too much time on this.


Solution

  • If you want to do floating point arithmetic and then apply the ceiling function, and have the result stored in an integer, the appropriate method would be to perform the floating-point division, apply the ceil function to the result, and then use static_cast<int> to convert it back to an integer.

    #include <iostream>
    #include <cmath> // for ceil()
    
    int main() {
        int y = 19; // sample value for y
        int x; // declare x as an int as required
    
        double intermediateResult = static_cast<double>(y) / 6.0;
        x = static_cast<int>(std::ceil(intermediateResult));
    
        std::cout << "You've been assigned " << x << " x's." << std::endl;
    
        return 0;
    }
    

    Here, the first static_cast<double> explicitly converts y to a double to ensure that the division is performed in floating-point arithmetic. We store this result in intermediateResult.

    Then, we apply the ceil() function to intermediateResult. The result of ceil() is a floating-point number, which we then convert back to an integer using static_cast<int> and store in x.