1. Re: I Need Help -- precision
- Posted by Ad Rienks <Ad_Rienks at COMPUSERVE.COM> Jun 23, 1997
- 643 views
Reply to Michael Bolin and Lucius Hilley: >> Hi Michael, >> >> Unfortunately, this is not useful to me. If I implement it with: >> > >fprint(1, "4.2%f", round(0.745)) >> >>what do you think I get? Well, it is 0.74! Strange enough, some of the >>values x.xx5 round up in the correct way, but not all of them! Is this = a >>bug in my processor (P75), or in the 'binary floating point' routines? >>I am puzzled over this for a long time already, as I want to do some exac=3D >> t >>financial calculating in Euphoria. Isn't this possible at all, or shoul= d =3D >> I >> maybe disable my math coprocessor? >This is not a bug in my routine, it is another bug resulting from >binary floating point representation. >But I have just modified the function to allow a variable amount of >precision, and it will work in cases such as yours. >---------------------------------------------------- >function round(atom x,atom precision) > precision=3Dpower(10,precision) > return floor(x*precision+.5)/precision >end function >---------------------------------------------------- >Just call it with an atom value, and a number of decimal places to >round to. You can use values less than one: >If you use 0, it will round to the nearest integer. If you >use -1, it will round to the nearest 10. If -2, the nearest 100, etc. >Regards, > Michael Bolin Thanks, also to Lucius, who sent me an equal solution. But still, unfortunately, this are not the right solutions. Imagine that = in the binary floating point routines x.xx5 is represented as x.xx49999.......... (etc) and I want a precision of 2 digits. This means for instance that .745 should be rounded up to .75. Check your local 'Pocket Japanese'. Multiplied by 100, this gives me 74.49999..., add .5 t= o it, you get 74,9999....... Floored, this gives ...... 74! Exactly this is what happens. But, even more unfortunate, .145 for instan= ce is represented as .145000000001 or so. So, a round() function as you propose it does not always give the right answers, certainly not the answers I get from my 10$ calculator. Someone, I think it was Jef Zeitlin, has already pointed this out some ti= me ago. But my question remains the same: Is there a way round this, using Euphoria? Thanks anyway, Ad Rienks