tag:blogger.com,1999:blog-7098835175438692935.post6998155423588253867..comments2014-04-13T01:29:06.931-07:00Comments on Accounting++: C++: rounding a double to a fixed number of decimalsFulvio Senorehttp://www.blogger.com/profile/08431327146202932326noreply@blogger.comBlogger6125tag:blogger.com,1999:blog-7098835175438692935.post-31179901488004750492014-04-13T01:29:06.931-07:002014-04-13T01:29:06.931-07:00Which code did not work, mine or Wacec's?Which code did not work, mine or Wacec's?Fulvio Senorehttp://www.blogger.com/profile/08431327146202932326noreply@blogger.comtag:blogger.com,1999:blog-7098835175438692935.post-6141452714465288772014-04-10T02:07:03.719-07:002014-04-10T02:07:03.719-07:00I tested this code and didn't work for all cas...I tested this code and didn't work for all cases:<br />for example:<br />Profit vs Rounded: 3.70500 vs 3.70000<br />Profit vs Rounded: 6.13500 vs 6.14000<br />Profit vs Rounded: 6.18500 vs 6.19000<br />Profit vs Rounded: 2.04500 vs 2.05000SairuShttps://me.yahoo.com/a/x6CPa5Elw4_BogojL2AAoJKGB7upBw--noreply@blogger.comtag:blogger.com,1999:blog-7098835175438692935.post-86894649823517104662012-04-30T09:03:13.792-07:002012-04-30T09:03:13.792-07:00I tested your code and it works: very interesting!...I tested your code and it works: very interesting!<br />I debugged it and it looks like the trick is done when computing "val * power": in my example it is "val * 100" and the result is 315.5 instead of the expected 315.4999999...<br />As I wrote in the post 315.5 is a number with a finite representation in binary form, so it looks like the runtime is smart enough to understand it and to change the result to the right value.<br /><br />It looks like your code solves the problem: as soon as I will have some time I will test it under Linux with the gcc compiler to see if it works too.<br /><br />Thank you!Fulvio Senorehttp://www.blogger.com/profile/08431327146202932326noreply@blogger.comtag:blogger.com,1999:blog-7098835175438692935.post-73561085468673607222012-04-30T07:44:08.546-07:002012-04-30T07:44:08.546-07:00Thanks for comment. As a matter of fact when I cal...Thanks for comment. As a matter of fact when I call round(3.155,2) I get a result of 3.16 :-) (at least on my Windows machine with Visual C++ compiler). That's why I decided to share the code. <br />Greetings<br />wgWacekhttp://www.blogger.com/profile/05834989072372452132noreply@blogger.comtag:blogger.com,1999:blog-7098835175438692935.post-4975298993843253832012-04-30T01:12:28.502-07:002012-04-30T01:12:28.502-07:00Thank you for sharing. I use a different function ...Thank you for sharing. I use a different function but they both follow the mathematical rules for rounding, so they should yield the same result.<br /><br />I am pretty sure that if you send 3.155 to your function you get 3.15, a wrong result.<br /><br />The post was about a situation where rounding a number using correct rules yields a wrong result, and about a possible solution to the problem.Fulvio Senorehttp://www.blogger.com/profile/08431327146202932326noreply@blogger.comtag:blogger.com,1999:blog-7098835175438692935.post-78564675209653783472012-04-28T12:25:30.921-07:002012-04-28T12:25:30.921-07:00I'm using function pasted below to round doubl...I'm using function pasted below to round doubles. Prec parameter defines how many digits after decimal point are significant for you (in case of money it's obviously 2).<br />// -- cut --<br />double round(double x, int prec)<br /> {<br /> double power = 1.0;<br /> int i;<br /><br /> if (prec > 0)<br /> for (i = 0; i < prec; i++)<br /> power *= 10.0;<br /> else if (prec < 0)<br /> for (i = 0; i < prec; i++)<br /> power /= 10.0;<br /><br /> if (x > 0)<br /> x = floor(x * power + 0.5) / power;<br /> else if (x < 0)<br /> x = ceil(x * power - 0.5) / power;<br /><br /> if (x == -0)<br /> x = 0;<br /><br /> return x;<br /> }<br />// -- cut --<br />Greetings<br />wgWacekhttp://www.blogger.com/profile/05834989072372452132noreply@blogger.com