Positive integers that multiply to a negative value

前端 未结 9 1195
礼貌的吻别
礼貌的吻别 2021-02-07 05:17

I am learning C++ by reading Stroustrup\'s \"Principles and Practice Using C++\".

In the section about pre- and post-conditions there is the following example of functio

9条回答
  •  旧时难觅i
    2021-02-07 05:28

    Yes if suppose you are using 16 bit computer so int = 2B Max value +32767 so in following

    {
        length = 500, width = 100;
        if (length<=0 || width <=0) error("area() pre-condition");
        int a = length*width;   // a = 500 * 100 = 50000
        if (a<=0) error("area() post-condition");
        return a;
    }
    

    now final value will be a = -17233 because it gets into -ve value. so second condition gets false.

    Its all depends on range.

提交回复
热议问题