Board index » delphi » CL - function Round(x) - am I going mad ?
Kees Lagendij
![]() Delphi Developer |
Fri, 13 Jul 2001 03:00:00 GMT
|
Kees Lagendij
![]() Delphi Developer |
Fri, 13 Jul 2001 03:00:00 GMT
CL - function Round(x) - am I going mad ?
Why does Round(12,5) results in 12 and Round(13,5) in 14 ?
(the same with 14,5 and 15,5; try it yourself !) Kees |
Ing. Franz Glase
![]() Delphi Developer |
Fri, 13 Jul 2001 03:00:00 GMT
Re:CL - function Round(x) - am I going mad ?QuoteKees Lagendijk wrote: If the previous digit is even, the # is rounded down, else up. It is made to give some "random distribution". The usual way to round is: Rounded := Int(Input+0.5); The "legal" way of roundig seems to be: convert the Franz Glaser |
Rudi Ziegau
![]() Delphi Developer |
Fri, 13 Jul 2001 03:00:00 GMT
Re:CL - function Round(x) - am I going mad ?AFAIK, Round rounds to the next even number. Rudi PS: To be absolutely sure, roll your own round-function. Kees Lagendijk schrieb in Nachricht <78igfj$m6...@reader1.wxs.nl>... Quote>Why does Round(12,5) results in 12 and Round(13,5) in 14 ? |
Ross Elliot
![]() Delphi Developer |
Fri, 13 Jul 2001 03:00:00 GMT
Re:CL - function Round(x) - am I going mad ?I'm assuming you're using Delphi4 because I don't think any other version exhibits this behaviour. The reason it does this is because be default, 64-bit integer types are used (INT64). I'm not sure the exact reason why the rounding happens like it does, suffice to say that it does. One way to fix it is to turn off 64-bit integers (I forget how, check the help file/manual on how to do this...it's something like -$I) or to use something other then integer (ie Cardinal, Word, Byte, etc...) HTH, QuoteKees Lagendijk <ksbreda@_NOSPAM_wxs.nl> wrote in message Quote>Why does Round(12,5) results in 12 and Round(13,5) in 14 ? |
Bruce Robert
![]() Delphi Developer |
Fri, 13 Jul 2001 03:00:00 GMT
Re:CL - function Round(x) - am I going mad ?There are several rounding methods. I think the one under discussion is called "bankers rounding". QuoteSteve Hodges wrote in message ... |
Steve Hodge
![]() Delphi Developer |
Sat, 14 Jul 2001 03:00:00 GMT
Re:CL - function Round(x) - am I going mad ?Well I was always taught that when rounding, you go to the nearest number. If the value is exactly in the middle you go to the even number. this ensures that I beleive it's a convention. Steve QuoteIng. Franz Glaser wrote in message <36ACD3CB.FF05...@eunet.at>... |
Dmitry M.Ivle
![]() Delphi Developer |
Sat, 14 Jul 2001 03:00:00 GMT
Re:CL - function Round(x) - am I going mad ?Hmm... I checked it too (simple in Evalute/Modify dialog). Function Round doesn't give right value for any value n*2+0.5 where n =0,1... i.e 0.5, 2.5, 4.5, 6.5 and so on. It's bug I think. May be better ask Inprise? Kees Lagendijk D??? ???Y?? <78igfj$m6...@reader1.wxs.nl> ... Quote>Why does Round(12,5) results in 12 and Round(13,5) in 14 ? |
Ken Whit
![]() Delphi Developer |
Sat, 14 Jul 2001 03:00:00 GMT
Re:CL - function Round(x) - am I going mad ?Kees, This is the documented behavior for Round(). It's called "Banker's Ken Clipper Functions for Delphi QuoteKees Lagendijk wrote: |
Markku Nevalaine
![]() Delphi Developer |
Sun, 15 Jul 2001 03:00:00 GMT
Re:CL - function Round(x) - am I going mad ?QuoteSteve Hodges wrote: the 300 million EU-citizens should round currencies when converted from EURO to national currencies, and vice versa. From the commission documents, I can't find any reference to the rounding rule that is mentione above. You should just always round upwards all the decimals ending to five: 12.335 -->12.34 13.345-->13.35 14.355-->14.36 etc. That's why we need also some simple biased rounding functions: function RndToFlt (const X : Extended ) : Extended; function RndToInt (const X : Extended ) : Integer; Markku Nevalainen |
Ray Lischn
![]() Delphi Developer |
Sun, 15 Jul 2001 03:00:00 GMT
Re:CL - function Round(x) - am I going mad ?On Wed, 27 Jan 1999 00:39:06 +0200, Markku Nevalainen <m...@iki.fi> wrote: Quote>So European Commission is biased:) They have given exact directions, how floating-point control word before calling Round. Just be sure to restore it after calling Round. Set Set8087CW in the online help, or roll your own in ASM. |
Ken Whit
![]() Delphi Developer |
Sun, 15 Jul 2001 03:00:00 GMT
Re:CL - function Round(x) - am I going mad ?Ross, Sorry... This has absolutely nothing to do with 64-bit integer types; Ken Clipper Functions for Delphi QuoteRoss Elliott wrote: |
Duncan Murdo
![]() Delphi Developer |
Mon, 16 Jul 2001 03:00:00 GMT
Re:CL - function Round(x) - am I going mad ?On Wed, 27 Jan 1999 00:39:06 +0200, Markku Nevalainen <m...@iki.fi> wrote: Quote> From the commission documents, I can't find any reference to the rounding value := 12.335; rounded := RndToFloat(value*100)/100; because the extended type can't store numbers like 12.335 exactly You should use the currency type (exact storage to 4 decimal places), Duncan |
Markku Nevalaine
![]() Delphi Developer |
Mon, 16 Jul 2001 03:00:00 GMT
Re:CL - function Round(x) - am I going mad ?QuoteDuncan Murdoch wrote: compatibility also with D1, and D1 does not have TCurrency type, only Currency datafield. I think you can quite safely do also all the monetary calculations using Markku Nevalainen |
Markku Nevalaine
![]() Delphi Developer |
Mon, 16 Jul 2001 03:00:00 GMT
Re:CL - function Round(x) - am I going mad ?QuoteDuncan Murdoch wrote: would need more info. http://europa.eu.int/euro/html/entry.html Markku Nevalainen |
Christian Fah
![]() Delphi Developer |
Mon, 16 Jul 2001 03:00:00 GMT
Re:CL - function Round(x) - am I going mad ?Kees Lagendijk schrieb: Quote> Why does Round(12,5) results in 12 and Round(13,5) in 14 ? Try the following Procedure. You need a Form (Form1), three I hope This is what you need. Christian Fahn procedure TForm1.Button1Click(Sender: TObject); |