Rounding methods (precise vs coarse) #71
Replies: 2 comments
-
Hello @lucacutrignelli First of all, sorry about the delay in the answer, but I was busy with other projects! Thank you for such a detailed post, it is very clear. I was thinking about some straightforward implementations of remaining methods you're proposing and I think there a some could work: Rounding to infinitywhere For example: # Fxp template
n_frac = 2
n_int = 0
n_word = n_int + n_frac
overflow = 'saturate'
fxp_ref = Fxp(None, signed=True, n_int=n_int, n_frac=n_frac, overflow=overflow)
# float values
ratio = 4
float_precision = fxp_ref.precision / ratio
x = np.arange(fxp_ref.lower - (ratio*float_precision), fxp_ref.upper + ratio*float_precision, step=float_precision)
# fxp
rounding = 'trunc'
fxp_var = Fxp(x + 2*np.sign(x)*2**(-2*n_frac), rounding=rounding, like=fxp_ref) Rounding to plus infinityrounding = 'ceil'
fxp_var = Fxp(x-2**(-2*n_frac), rounding=rounding, like=fxp_ref) Rounding to zerorounding = 'trunc'
fxp_var = Fxp(x + np.sign(x)*2**(-2*n_frac), rounding=rounding, like=fxp_ref) Rounding to minus infinityrounding = 'floor'
fxp_var = Fxp(x+2**(-2*n_frac), rounding=rounding, like=fxp_ref) ConclusionIf these approaches are right, those method can be implemented into What do you think? |
Beta Was this translation helpful? Give feedback.
-
Hello @francof2a, thanks for taking the time to look into this topic. Let me comment on your proposed implementation. I found you had a good intuition to implement the missing precise rounding on top of trunc/ceil/floor rather than on top of around, as the convergent rounding would be more problematic to reuse due to the special treatment of tie conditions. If I understood correctly your approach, as I tried to summarize above, I agree with it: it looks viable and probably not adding much performance penalty. Concerning the correction formulas, I reviewed them and they don't seem correct to me. From what I understand when sketching input/output relationship, in order to match the behavior of trunc/ceil/floor with the expected one from round_inf/round_plus_inf/... we need to always correct the input data by adding/subtracting half of the precision of the target Fxp variable. So add/subtract The corrections you proposed are different and don't seem to generalize when Rounding to infinity
Rounding to plus infinity
Rounding to minus infinity
Rounding to zeroThis looks trickier, as it would seem easier to distinguish two cases: For positive input values For negative input values Trying to put it together in a general (both positive and negative inputs) but cumbersome formula: Examples of supposedly incorrect behavior of the original proposalThese cases seem not correct to me. Rounding to infinity Rounding to plus infinity ConclusionPlease have a look at my proposal and let's see if we agree with the corrections to the input data. |
Beta Was this translation helpful? Give feedback.
-
Hello @francof2a,
I would like to discuss the rounding methods of fxpmath in comparison with other existing solutions.
Throughout the explanation I'll refer to the documents
http://cfs-vision.com/2017/08/14/learning-systemc-001-data-types/
https://nl.mathworks.com/help/fixedpoint/ug/rounding.html
Precise rounding:
I define this way the rounding methods that only differ for handling of tie cases
Coarse rounding:
I define this way the rounding methods that are actually operating a truncation of the data to a neighbouring code, so they produce a larger rounding error than the Precise rounding
I hope these comparison tables are clear and might be a useful summary of the current supported roundings.
From the tables above, Fxp is well covered for the coarse rounding cases, but only provides one precise rounding case (the convergent rounding). My assumption is that this is due to the underlying python rounding functions that fxpmath is using.
Nonetheless, in my experience in hardware modeling the other precise rounding methods might prove useful. Particularly the Round to infinity and Round to plus infinity.
My question is: would there be a big performance penalty in implementing the other precise rounding methods mentioned above?
Could you comment on the complexity of the implementation?
Thanks!
Beta Was this translation helpful? Give feedback.
All reactions