0

I want to round a number to 10 decimal places , but it returns '6.75677', although it could returns '6.7567653457'

#include <iostream>
#include <cmath>
using namespace std;

double rounded(double number, int N)
{
    return round(number * pow(10, N)) / pow(10, N); // i've chanched float to double
}

int main()
{
    double value = 6.756765345678765; 
    cout << rounded(value, 10)
}

I'd like to see a function returns rounded number

Frankly speaking, I'd see an alternative of function 'round' in python

print(round(6.756765345678765, 10))
MIkhail
  • 49
  • 8
  • 3
    A `float` does not "have" 10 decimal places to round to in the first place. It does not represent values precisely enough to get that information - the smallest difference between two "adjacent" valid `float` values is much larger than the 10^-10. (never mind that numbers of any kind do not "have" digits at all in the first place; that's a separate topic.) – Karl Knechtel May 21 '22 at 21:12
  • Does this answer your question? [What is the difference between float and double?](https://stackoverflow.com/questions/2386772/what-is-the-difference-between-float-and-double) – Karl Knechtel May 21 '22 at 21:13
  • I also try a double type but it still not works. So, how to round number with perfect accuracy? – MIkhail May 21 '22 at 21:18
  • “it still not works” is not an adequate problem description. Update the question to provide a [mre], including an exact copy of the observed output and a sample of the output desired instead. – Eric Postpischil May 21 '22 at 21:21
  • I mean, it returns the same answer – MIkhail May 21 '22 at 21:23
  • 2
    _"with perfect accuracy"_ and floating points is a hard problem – Ted Lyngmo May 21 '22 at 21:24
  • 1
    You may be needing a [Fixed-point representation](https://en.wikipedia.org/wiki/Fixed-point_arithmetic) or a [Multi-precision Type](https://en.wikipedia.org/wiki/Arbitrary-precision_arithmetic). – user4581301 May 21 '22 at 21:36
  • Floating point types with a base-2 mantissa (which includes most floating point representations, including IEEE) cannot represent decimal fractions like 0.1 or 0.01 (or other negative powers of 10) exactly. The reason is exactly the same as why the fraction one-third cannot be represented in a finite number of decimal places - 0.1 has an infinite representation (endlessly repeating) in base 2. This question is therefore meaningless as it stands. Rounding of floating point is normally done by formatting *output* (e.g. printing) not trying to produce a "rounded value". – Peter May 22 '22 at 01:56
  • Alternatively you could use a 64-bit long long int, which has ~19 decimal digits and use it in units of .0000000001. Depending on whether you want to generally print 10 digits or 10 digits after the decimal point, you can further format/truncate the output. Advantage is that it can accurately represent decimal fractions. Disadvantage is that it cannot calculate very small or very high numbers like 10^-23 or 10^23. – Sebastian May 22 '22 at 06:42

1 Answers1

1
cout << fixed << setprecision(10) << value << endl