Mail Archives: djgpp/1999/05/29/17:28:55
> 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. You could probably
come up with a smilar rule if you had an upper bound. The idea is
to always round negative or positive numbers which accounts for
the shift. If you don't have bounded numbers, then what you said is
right, you need to treat positive and negative numbers separately.
-Mohamed
- Raw text -