piclist 2001\06\01\191009a >
Thread: Brain Burp Rounding??
face BY : Andrew Warren email (remove spam text)

William Chops Westfield <.....billwspamspam_OUTcisco.com> wrote:

> Is there a simple algorithm for doing the odd/even rounding
> (equivilent to N+0.5 and truncate?)


There's a simple implementation in PIC assembly:

   ; Enter with integer in REG, fraction in the Carry flag.

       BNC     DONE    ;If the carry's clear, no rounding
                       ;is required, so jump.

       BTFSS   REG,0   ;If the integer is odd, skip ahead.
       INCF    REG     ;Otherwise, round up (i.e., round
                       ;to odd).


This implementation uses "round to odd" rather than "round to even"
in order to make the rounding of multi-byte integers easier; with
this method, only the least-significant byte is ever changed.

This introduces a tiny bit of bias, however, since only ONE value
(0.0) rounds to 0 while THREE values (254.5, 255.0, and 255.5) round
to 255.

If it were important for numbers close to 0 to round down to 0, you
could reverse things so that the bias was toward 0 rather than 255...
But then you'd non-intuitively be rounding 1.0 to 0.


=== Andrew Warren --- @spam@aiwspamcypress.com
=== IPD Systems Engineering, CYSD
=== Cypress Semiconductor Corporation
=== Opinions expressed above do not
=== necessarily represent those of
=== Cypress Semiconductor Corporation

http://www.piclist.com hint: PICList Posts must start with ONE topic:
[PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads

<3B17BAC3.9116.A9959B3@localhost> 7BIT

In reply to: <CMM.>
See also: www.piclist.com/techref/index.htm?key=brain+burp+rounding
Reply You must be a member of the piclist mailing list (not only a www.piclist.com member) to post to the piclist. This form requires JavaScript and a browser/email client that can handle form mailto: posts.
Subject (change) Brain Burp Rounding??

month overview.

new search...