Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] How to do integer divison rounding to closest integer, vhdl synthesizable code?

Status
Not open for further replies.

nicklas_a74

Member level 1
Joined
Oct 4, 2002
Messages
37
Helped
8
Reputation
16
Reaction score
7
Trophy points
1,288
Activity points
316
Hi

I want to have generic input values e.g. a and b what are used to initialize a
constant by e.g. constant c:= a/b; However due to the division the
correct answer could be 4.6 but it’s rounded down to closest integer 4 as default.
How to being able to initialize the c and have a rounding to closest integer 5?
The code should be synthesizable and use the standard libraries. Is there a
way to do this without fixedpoint or special math library?
 

The simple trick is to add half the divisor to the dividend before doing the division.

So basically you should do c:= ( a + b/2 ) / b
 
If it is only a generic or constant, you can use the real type:

c := integer( real(a)/real(b) );

real -> integer conversion always rounds to nearest.
 
Thank you for the valuable suggestions
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top