From: Mbaccar AT aol DOT com Message-ID: <8710ed13.2481af58@aol.com> Date: Sat, 29 May 1999 17:00:08 EDT Subject: Re: how to round fp-numbers correctly ? To: djgpp AT delorie DOT com MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Mailer: AOL 4.0 for Windows sub 11 Reply-To: djgpp AT delorie DOT com In a message dated 5/29/99 3:46:02 PM EST, ChristianHofrichter AT gmx DOT de writes: > I want to round an fp-number to the nearest representable integer value. > The standard rounding methode simply leaves out the mantisse of an > fp-number (e.g 0.5 is rounded to 0 not to 1). Adding 0.5 only works if > the fp-number is positive, otherwise I have to check if the fp-number is > negative and then would have to substract 0.5. This would waste too much > time. Changing the rounding methode with "_control87(RC_NEAR,MCW_RC);" > didn't work. I don't know why .So what can I do to solve this problem ? > This should not be too difficult because there are a lot of > fp-instructions which "push" and "round" an fp-number. Shall I use the > inline-assembler ? If you know a lower bound for the numbers you are dealing with, I would use: round (fp - min + 0.5) + min where min is the lower bound integer number. If you don't have bounded numbers, then what you said is right, you need to treat positive and negative numbers separately. For example, a macro #define ROUND(x) ((x >= 0) ? (int) (x + 0.5) : (int) (x - 0.5)) should do the trick. I ran into this problem before. Hope you find this helpful. -Mohamed