 # Best pratice for working with floating constants

• Hello,

I am fairly new to VA and was wondering how to work with floating constants that I need in my computation.

A very simple task could be

I1 = I0 + b

where b could be a floating value between -1 and 1.

I could not enter the floating number into the CONST operator, and neither could I find an appropiate operator for using float values.
Thus, I thought about some implementation techniques, mainly quantizing the value before in an appopiate bitwidth to utilize fractional bits, then multiplying it, so I can use an integer in the CONST operator, and adapt my formula accordingly so it would look something like this:

I1 = 128*I0 + 128*bquantized

Since this is the the first thing which came into my mind I guess that there might be better ways to use point numbers.

Of course this is just a very simple example, will the techniques differ when I have constant matrix filled with floats? or a LUT ?

I am hoping to get some feedback and insights on how to work with floating numbers in VA this way and maybe help others facing the same problem.
I was not able to find any appropiate entry here, if there is one please let me know Greetings,

Kevin

• Dear Kevin,

in my opinion your solution is the best and easiest way to use FLOAT values in calculations within VA. At least this is the way I have been using for many years now As you did in your example, it is always recommended to work with bitshifts instead of multiplying the values by a power of ten for example. So, after calculation you can get rid of the fractional bits by a simple bitshift and you don't need to use the division operator, that can be very expensive regarding FPGA resources.

Greetings,

Oliver

• Dear Kevin,

Thank you for your precise question.
There is one kind of valid float values you can use: ParameterTranslations...

In VA native(FPGA) processing you will need to use fixed point arithmetics.

I am fairly new to VA and was wondering how to work with floating constants that I need in my computation.

Below you can find a simple example into fixed point arithmetics:

pasted-from-clipboard.png

The ShiftLeft will give you precision (here 10 bit) and I recommend RND to reduce to lower precision.
ShiftLeft by 10 bit will give you a resolution of 10bit = 1024 steps in between two integer values.
When you require more, use more bits...

Best regards,

Björn Rudde
Field Application Engineer / Consultant - Basler AG Call me on MS Teams

• floating numbers in VA

The following details will help you to implement floating point into the software interface:

ParameterTranslation:

 https://docs.baslerweb.com/liv…FloatParamTranslator.html Formula Syntax: Mathematical Operations The syntax complies to the GenICam API standard in version 2.0. The allowed formula elements are identical to the formula elements defined in the GenICam standard: Basic operations: () brackets + - * / addition, subtraction, multiplication, division % remainder ** power & | ^ ~ bitwise: and / or / xor / not <> = > < <= >= logical relations: not equal / equal / greater / less / less or equal / greater or equal && logical and / logical or << >> shift left / shift right Basic operations Conditional operator ? : Example: \${target.Value} = (\${this.Value} > 0) ? 1 : 0; Functions: SGN(x) return sign of x. Returns +1 for positive argument and -1 for negative argument NEG(x) swap sign of x ABS(x) return absolute value of x SQRT(x) return square root of x TRUNC(x) truncate x, which means returning the nearest integral value that is not larger in magnitude than x FLOOR(x) Round downward, returning the largest integral value that is not greater than x CEIL(x) Round upward, returning the smallest integral value that is not less than x ROUND(x,precision) round x to the number of decimal fractional digits given by precision, with halfway cases rounded away from zero SIN(x) return sine of an angle of x radians COS(x) return cosine of an angle of x radians TAN(x) return the tangent of an angle of x radians ASIN(x) return the principal value of the arc sine of x, expressed in radians ACOS(x) return the principal value of the arc cosine of x, expressed in radians ATAN(x) return the principal value of the arc tangent of x, expressed in radians EXP(x) return the base-e exponential function of x, which is e raised to the power x: ex LN(x) return the natural logarithm of x The natural logarithm is the base-e logarithm: the inverse of the natural exponential function (exp). LG(x) return the common (base-10) logarithm of x E() return Euler's number, 2.7182818284590451 PI() return circle constant, 3.1415926535897931 Functions Example: \${target.Value} = NEG(\${this.Value}) Access to Parameter Properties The formulas can not only incorporate the values of parameters, but also specific properties of parameters, such as minimal value, maximal value, or step size: \${PathToModule/Module.ParamName.From} or \${PathToModule/Module.ParamName.Min}: minimal valid value of parameter PathToModule/Module.ParamName. \${PathToModule/Module.ParamName.To} or \${ PathToModule/Module.ParamName.Max}: maximum valid value of parameter PathToModule/Module.ParamName. \${PathToModule/Module.ParamName.Inc}: Increment (step size) between two valid values of parameter PathToModule/Module.ParamName. \${PathToModule/Module.ParamName.Enum("EnumName")}: Integer value of enumeration name EnumName.

Best regards,

Björn Rudde
Field Application Engineer / Consultant - Basler AG Call me on MS Teams

• In addition to the great posts above for the use of fixed point arithmetic:

Notation: u(5,3) means unsigned, 5 integer and 3 fractional bits.

There is a simple rule for fixed point arithmetic.

For additions or subtractions you need to use the same number of fractional bits at the input. The result will have the same number of fractional bits. Integer bits will increase by one.

So u(5,3) + u(9,3) will become u(10,3)

e.g. 1.5 + 2.25 = 3.75 --> 12 + 18 = 30

For multiplications and division the number of fractional bits at the inputs can differ. For multiplications the resulting fractional bits will be the sum of the input fractional bits. The resulting integer bits will be the sum of the input integer bits.

So u(5,3) * u(2,8) will become u(7,11)

e.g. 1.5 * 2.25 = 3.375 --> 12 * 576 = 6912

Johannes

 Johannes Trein Group Leader R&D frame grabberBasler AG • Dear Johannes,

I would like to add one detail on the fractional bits:

Notation: u(5,3) means unsigned, 5 integer and 3 fractional bits.

In case of using 3 fractional bits, there will be 23 values betreen two integer values:

Using 3 fractional bits will represent 23 = 8 steps.

Each step in fixed point granularity : 1/8 = 0.125

Best regards,

Björn Rudde
Field Application Engineer / Consultant - Basler AG Call me on MS Teams

• Hi Kevin,

I am hoping to get some feedback and insights on how to work with floating numbers in VA this way and maybe help others facing the same problem. Call me on MS Teams
Yes and thank you 