0

I'm trying to do a really simple calculation in C++

double area()
{
    return (base*height)/2;
}

where base and height are type int - though when I supply base and height values of 5 and 5 (they are declared earlier in the file) - I get back 12, when I should be getting back 12.5 considering the double return type.

Am I using the return correctly, or should I be trying to use setprecision() or trying to cast before returning?

unicornication
  • 635
  • 2
  • 7
  • 26
  • 2
    Possible [duplicate](http://stackoverflow.com/questions/7571326/why-cant-i-return-a-double-from-two-ints-being-divided) – Mohamad Elghawi Feb 18 '16 at 10:16
  • What you do with a result after it's computed has no effect on how that result is computed. That you return the result of the division as a double has no effect on how it's computed. C++'s syntax is complex enough without adding lots more complexity. – David Schwartz Feb 18 '16 at 10:18

5 Answers5

4

Because base, height and 2 are all int, the (base*height)/2 's result will be an int too (12 here), which will be casted to double when return (12.0 here).

Change

return (base*height)/2;

to

return (base*height)/2.0;

The (base*height)/2.0 's result is double because 2.0 is double, and you could get 12.5 now.

Or as @Ajay pointed, cast base or height to double to avoid potential integer overflow. Such as,

return (static_cast<double>(base)*height)/2;
songyuanyao
  • 169,198
  • 16
  • 310
  • 405
  • 1
    It may give wrong result if 'base' and 'height' are too large to be handled by integer (which may be 1 - 8 bytes). For eg. if they are `BYTE`, and are valued 250,250. It is always better to typecast one of the operand. Therefore `((double)base/height)/2` is more appropriate. 'int' is quite large, but not as large as `double`'s value capacity. – Ajay Feb 19 '16 at 06:39
1

You are perfoming an integer division here, since both arguments of the division are integers.

Casting afterwards wont help, but casting one of the arguments to a float/double will.

Easiest is to write:

return (base*height)/2.;

(The dot after the 2 makes it a double, which makes the compiler choose the floating-point division instead of integer division)

Anedar
  • 4,235
  • 1
  • 23
  • 41
1

If all the terms are int an integer division is performed and the result (an integer) is cast to a double by the return. Try using 2.0 instead, or multiply by 0.5 and avoid the division too.

Bob__
  • 12,361
  • 3
  • 28
  • 42
1

Do this:

double area()
{
    return ((double)base*height)/2;
}

Why? Entire expression is int and result will be int.

double x  = 100/3;

Will be 33.0 and not 33.33333 as you might expect. You need to make at least one operand to be double:

  • 100.0/3
  • 100/3.0
  • double(100) / 30
  • 30 / double(100)
  • NOT this: (double)(100/3)
Ajay
  • 18,086
  • 12
  • 59
  • 105
0

Explicit Type casting

double area()
{
    return (base*height)/2.0;  //at least make any one of the operand double 

       //or   return (double(base*height))/2; external typecasting of base*height

      //or    return (double(base)*height)/2; external typecasting of base

}

Simple Note: when you are defining your function Then its your responsibility to return the value in the specified data type otherwise compiler will do implicit typecasting that may result in loss of precision.

For more visit this link : http://www.cprogramming.com/tutorial/c/lesson11.html

secretgenes
  • 1,291
  • 1
  • 19
  • 39