From: duraisridhar on
Hi All,

My coding have following stmt in which I divide large number say X by
number y which is sligthly greater then X. Say Y = X + 1


double successRate = 0;
double passedCandidates = 10000000 ;
double failedCandidates = 1;

sucessRate = (passedCandidates * 100) / ( passedCandidates +
failedCandidates );

In the above stmt , I always get 100 % while I except 99.9999. . I
tried type casting sucessRate to float , but still i get 100 %. When
the failedCandiates value is 20, I get success rate as 99.99.

Is thier i can success rate less then 100 when Y is greater then X.

Thanks in advance,

Regards,
Sri


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Martin Bonner on
On Jun 24, 8:27 am, duraisrid...(a)gmail.com wrote:
> Hi All,
>
> My coding have following stmt in which I divide large number say X by
> number y which is sligthly greater then X. Say Y = X + 1
>
> double successRate = 0;
> double passedCandidates = 10000000 ;
> double failedCandidates = 1;
>
> sucessRate = (passedCandidates * 100) / ( passedCandidates +
> failedCandidates );
>
> In the above stmt , I always get 100 % while I except 99.9999.
Well, Windows calculator tells me to expect 99.99999 (note: *five*
nines after the decimal point, not four).

Are you sure you are displaying enough decimal places? (If you
displayed four, then I would expect it to be rounded to 100.0000).
> I
> tried type casting sucessRate to float , but still i get 100 %.
float is only going to make the problem worse. You may find that
passedCandidates and passedCandidates + failedCandidates are the same
floating point number.
> When
> the failedCandiates value is 20, I get success rate as 99.99.
Really. With 20, I get 99.9998 - have you given us the right value
for passedCandidates?


>
> Is thier i can success rate less then 100 when Y is greater then X.
>

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Seungbeom Kim on
duraisridhar(a)gmail.com wrote:
>
> double successRate = 0;
> double passedCandidates = 10000000 ;
> double failedCandidates = 1;
>
> sucessRate = (passedCandidates * 100) / ( passedCandidates +
> failedCandidates );
>
> In the above stmt , I always get 100 % while I except 99.9999. . I
> tried type casting sucessRate to float , but still i get 100 %. When
> the failedCandiates value is 20, I get success rate as 99.99.

It's just rounding that makes the result "look" like an exact 100,
but the actual value may not be so. Try:

std::cout << successRate << '\n';
std::cout.precision(53); // use "%.53g"
std::cout << successRate << '\n';

Result:

100
99.9999900000010057965482701547443866729736328125

If you want to always round the value down at a specific place,
you can use:

double round_down(double x, double scale)
{ return std::floor(x * scale) / scale; }

successRate = round_down(successRate, 100); // 2 decimal places
std::cout << successRate << '\n';

Result:

99.99

Actually, due to the imprecise nature of floating-point representation,
the value is still not exactly 9999/100; it's 99.989999..., and you can
check it out yourself again by precision(53).

In addition, if you want the output to consistently have two decimal
places (i.e. for 0.5 to appear as 0.50), you need the following too:

std::cout.setf(std::ios_base::fixed, std::ios_base::floatfield);
std::cout.precision(2);

(This piece of code, though it may look dreadful, are simply the C++
iostream version of the good-old-stdio's "%.2f". Oh my.)

--
Seungbeom Kim

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: ThosRTanner on
On Jun 24, 8:27 am, duraisrid...(a)gmail.com wrote:
> Hi All,
>
> My coding have following stmt in which I divide large number say X by
> number y which is sligthly greater then X. Say Y = X + 1
>
> double successRate = 0;
> double passedCandidates = 10000000 ;
> double failedCandidates = 1;
>
> sucessRate = (passedCandidates * 100) / ( passedCandidates +
> failedCandidates );
>
> In the above stmt , I always get 100 % while I except 99.9999. . I
> tried type casting sucessRate to float , but still i get 100 %. When
> the failedCandiates value is 20, I get success rate as 99.99.
>
> Is thier i can success rate less then 100 when Y is greater then X.

Are you sure you are getting 100% - you haven't said how you've
verified this. If you are printing the valid of successRate somewhere,
it's likely you'll see 100.

casting succesRate to float won't help much as it loses precision, so
it might round up, down, left or sideways.

If you want to see if its less than 100%, try comparing it with 100
instead.

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Bob Lied on
> duraisridhar(a)gmail.com wrote [2008-06-24 2:27 AM]:
> My coding have following stmt in which I divide large number say X by
> number y which is sligthly greater then X. Say Y = X + 1
>
> double successRate = 0;
> double passedCandidates = 10000000 ;
> double failedCandidates = 1;
>
> successRate = (passedCandidates * 100) / ( passedCandidates +
> failedCandidates );
>
> In the above stmt , I always get 100 % while I expect 99.9999.

Actually, 99.99999 (seven nines). You're being fooled because the
output is getting rounded to six digits. Try increasing output precision:

#include <iomanip>
std::cout << std::setprecision(10) << successRate << "\n";

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]