-1

I want to return a double from a function with a set precision! It is that possible ? I want an accuracy of 10-5 !

For example :

double f(double a ,double b)
{
  //something like return.setprecision(6); 
  return (a+b)/2;
}
Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
Vasiu Alexandru
  • 103
  • 3
  • 16
  • Do you mean truncated to n decimals ? What use is that ? –  Oct 11 '15 at 17:52
  • 1
    Double is in binary. You can't accurately round it to certain number of decimal digits, there will be rounding issues. So you need to tell what you want to achieve, to get the right solution. – hyde Oct 11 '15 at 18:02
  • @hyde: Re "double is binary", that is so *in practice* on current PC computers. The C++ standard allows both binary and decimal floating point representations. Integer types are however restricted to binary. – Cheers and hth. - Alf Oct 11 '15 at 18:07
  • @Cheersandhth.-Alf - Modern computers store stuff in binary. To display it - it converts it into another representation - typically ASCII. (this is also binary). – Ed Heal Oct 11 '15 at 18:11
  • @Cheersandhth.-Alf The point about (lack of) standard floating point numbers in C++ is good (if a bit advanced topic and not practically relevant to OP), after all even enabling agressive floating point optimizations may break calculations which are written for IEEE standard floating point... – hyde Oct 11 '15 at 18:27

2 Answers2

3

Assuming you mean truncated to 5 decimals:

Multiply by 105. Round to integer. Divide result by 105.

For the rounding, use the standard library so that you avoid exceeding the range of an integer type.


Note that the result can only be perfect for numbers that can be exactly represented. The common representations of floating point numbers are binary, although the C++ standard allows decimal representations. And that means that with most numbers the result will not be exact.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
  • @EdHeal: If the OP needs to deal with numbers near `DBL_MAX`, it's trivial to check for that kind of size and simply do nothing. If the OP means something else than 5 decimals, e.g. only 6 decimal digits in mantissa, then that's harder to do. Anyway the result can only be perfect for numbers that can be exactly represented. – Cheers and hth. - Alf Oct 11 '15 at 18:04
  • The edge cases are due to the conversion between binary and decimal system. There are lots of them. – Ed Heal Oct 11 '15 at 18:09
  • @EdHeal: Those are not edge cases. They are the common case. I.e. you have an impractical notion of "correctly", as well as a rather peculiar notion of "edge case", but the issue you point out does perhaps deserve to be mentioned in the answer, not just in a comment, so I'm adding it. – Cheers and hth. - Alf Oct 11 '15 at 18:11
  • Please reference where that doubles can be represented in memory as a decimal – Ed Heal Oct 11 '15 at 18:13
  • @EdHeal: Rather, the C++ standard places an explicit requirement on built-in integer types to be binary, and leaves that implementation defined for floating point types. Thus `numeric_limits::radix` is stated to be "often 2" for `T` a floating point type. It's equivalent to the C library's `FLT_RADIX`, and then you get down to C99 §5.2.4.2.2 which described the characteristics of floating point types, and requires the radix (numeral system base) to be "an integer > 1". – Cheers and hth. - Alf Oct 11 '15 at 18:27
0

No it is not possible. The doubles precision depends on the hardware/CPU etc.

Anyway - why do this?

Ed Heal
  • 59,252
  • 17
  • 87
  • 127