Coder Social home page Coder Social logo

Comments (3)

japm48 avatar japm48 commented on May 22, 2024

I provide here a quick explanation (you are free to copy it if you wish). To avoid confusion I believe it is better to think that the output is an integer (the radix point is after the last digit), instead of a fixed-point number, so the difference is just a simple shift.

First, there should be a distinction between directed rounding (i.e. towards or away from a reference value), and rounding to the nearest integer (or nearest representable number if we are dealing with fixed-point numbers).

In directed rounding we have:

  • round towards negative infinity: floor. -1.4 → -2, 1.5 → 1
  • round towards positive infinity: ceiling. -1.4 → -1, 1.5 → 2
  • round towards zero: truncate. -1.4 → -1, 1.5 → 1
  • round away from zero: does not have a dedicated C function, this would behave like -truncate(-x). -1.4 → -2, 1.5 → 2

All of these methods introduce a bias (average error).

For nearest integer rounding methods, the bias is a lot smaller than directed rounding. There are many variants, but all of them produce these example results: -1.3 → -1, -0.7 → -1, 1.6 → 2, 2.1 → 2.
The difference only occurs when there is a tie.

Some of them are:

  • round half towards positive infinity: -0.5 → 0, 1.5 → 2
  • round half towards negative infinity: -0.5 → -1, 1.5 → 1
  • round half away from zero, this is round(): -0.5 → -1, 1.5 → 2
  • round half to even (*rint with fesetround(FE_TONEAREST)): -1.5 → -2, -2.5 → -2, 1.5 → 2, 2.5 → 2

Also, it would be interesting to explain the Quantization Modes in Vivado HLS.

from pp4fpgas.

stephenneuendorffer avatar stephenneuendorffer commented on May 22, 2024

Hi, Thanks for the comment! It's not clear to me what exactly you're suggesting is incorrect? You're probably right that we should clarify that lrint() uses the currently set rounding mode, so we've assumed that the default rounding mode has not been changed. I see a few other things that could potentially be cleaned up here, but I think they are more clarity issues than anything else. One of the challenges in this section is to try to identify some correspondence with IEEE rounding modes (both directed rounding modes and round-to-nearest-even), while not suggesting that ap_fixed implements the IEEE standard. I think a few more examples might make it easier to see the patterns in how these different rounding modes work, but one of the key things I was trying to get across in this section is that rounding modes can be extended to work on fixed-point numbers.

from pp4fpgas.

japm48 avatar japm48 commented on May 22, 2024

My apologies, I completely forgot to answer.

we've assumed that the default rounding mode has not been changed

You are right, that's the default rounding mode for lrint(), I didn't know that. To my defense: apparently, there is no default defined in standard C (and possibly in C++), although different default modes are pretty rare.

One of the challenges in this section is to try to identify some correspondence with IEEE rounding modes (both directed rounding modes and round-to-nearest-even), while not suggesting that ap_fixed implements the IEEE standard [...]

Good point.


What I think is misleading are the following parts:

or to larger absolute values (called rounding away from zero or rounding to
infinity and corresponding to the round() function)).

This is presented as the opposite of trunc which is "round away from zero"; this is actually incorrect as there is no function (AFAIK) to do that. round() is actually "round half away from zero" which only behaves as suggested for interval midpoints, for the rest of values it returns the nearest representable number.

And also:

A better approach is called rounding to nearest even,
convergent rounding, or banker's rounding and is implemented in the lrint() function.

Similar argument: this is a variant of round() (i.e. round to nearest) where the only difference occurs for midpoint values. The majority of the error minimization comes from selecting the nearest number, not because it selects even numbers in case of a tie (although it is true that for this reason, it is slightly better than round()).

Also, I believe that in

None of these operations always minimizes the error caused by rounding, however.

it should be more precise to say "average error" or "error bias" instead of just "error".

from pp4fpgas.

Related Issues (16)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.