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?
 

std_match

Advanced Member level 4
Joined
Jul 9, 2010
Messages
1,196
Helped
445
Reputation
890
Reaction score
410
Trophy points
1,363
Location
Sweden
Activity points
9,234
Try c:= a/b + 0.5;
It can be more complicated if negative values are involved.
 

kripacharya

Banned
Joined
Dec 28, 2012
Messages
1,209
Helped
182
Reputation
360
Reaction score
175
Trophy points
1,343
Location
New Delhi
Activity points
0
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
 

TrickyDicky

Advanced Member level 5
Joined
Jun 7, 2010
Messages
7,065
Helped
2,077
Reputation
4,171
Reaction score
2,030
Trophy points
1,393
Activity points
39,119
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.
 

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
Thank you for the valuable suggestions
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top