Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Precision of sum output #59

Open
mahudu97 opened this issue Jan 21, 2018 · 2 comments
Open

Precision of sum output #59

mahudu97 opened this issue Jan 21, 2018 · 2 comments

Comments

@mahudu97
Copy link

Does "This should be printed in decimal, and be correct to at least 3 fractional digits." mean:

round to 3 decimal places
truncate after 3 decimal places

@mahudu97 mahudu97 changed the title Precision of sum output and Precision of sum output Jan 22, 2018
@m8pple
Copy link
Contributor

m8pple commented Jan 24, 2018

I meant correct in the sense of correctly rounded - I think the "correct to" idiom
is a habit from school, as they always used that term.

--

So more formally, the result should be correctly rounded. This has a very specific
technical/mathematical meaning, whereby if the number base is $b$, and we want
to round to the digit in place $j$, then the error in the represented number should
be at most $b^j / 2$. For example, if we had binary ($b=2$) with no fractional
part ($j=0$), then the answer should be correct to within $2^0 / 2 = 0.5$.

We have decimal ($b=10$) and 3 fractional digits ($j=-3$), so the given answer
needs to be within $10^-3/2$ = 0.0005 of the true answer.

The interesting cases are where the true answer lies exactly between two possible
correctly rounded answers. In those cases you need to choose between round to
zero, round to negative infinity, and round to even (convergent rounding). Usually
people choose convergent rounding by default, as if you add together lots of
numbers you'll get the smallest average error.

There's also something called faithful rounding, which is rounding to within $b^j$,
so to either of the two closest numbers. Truncation is a valid implementation of
that, and it is used a lot in digital circuits to reduce gate count.

More information than you need, but it's something related to my research area.

@m8pple
Copy link
Contributor

m8pple commented Jan 24, 2018

Just because someone did ask in lab...

Yes, std::setprecision does set the number of correctly rounded digits.

Probably one of the benefits of coming to lab is that you'll get the answer
you might want to receive, rather than the answer I might prefer to write.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants