Compiler generated code for TI C2000 device errors with "floating constant is out of range"
显示 更早的评论
When compiling code that has been generated for a TI C2000 device, I get the following error:
"floating constant is out of range"
How can I fix this?
采纳的回答
更多回答(1 个)
Andy Bartlett
2020-12-10
编辑:Andy Bartlett
2020-12-10
Hi,
Having a model that simulates with 64-bit floating-point doubles, but behaves as 32-bit floating-point on the target misses some of the key benefits of model based design. For example, it is often easier, faster, cheaper, and sometimes safer to discover your numeric edge cases in Simulink simulations than on the embedded device.
To get the full benefits of tight numeric agreement between the model and the embedded device behavior, the model can be converted from use of 64-bit floating-point doubles to 32-bit floating-point singles. There is a Single Precision Converter tool that ships with Fixed-Point Designer that will automatically convert a subsystem or model to use single precision types. Demonstrated in this video.
The compiler error message "floating constant is out of range" suggest you have a constant term such as a parameter that is too big for singles
abs( param ) > realmax('single') % 3.4028e+38
or too small to be represented with full 24 bits of precision.
abs( param ) < realmin('single') % 1.1755e-38
For the latter case, if the TI C2000 handles denormalized floating-point values with flush to zero, then all these small values will quantized to zero. Thus they will have 100% relative precision loss. That precision loss may or may not effect whether your design still meets behavioral requirements.
One way to avoid these issues is to re-scale your design.
For example, if the values are too big, then re-scale the physical units to a different SI prefix. For example, change the units of a signal from something fine like micro-X to something coarser like kilo-X. Where 'X' is meters, amps, watts, etc. This re-scaling would make all the values 10^9 times small and thus more likely to avoid crossing single's range limit of realmax('single').
Likewise, if the values are to small for single precision, then switch from a coarser unit like mega-X to something finer like micro-X. This re-scaling would make all the values 10^12 bigger and thus more liikely to stay above realmin('single').
The scaling changes don't have to correspond to an SI prefix change. You could use any scaling amount that works for your design. Using a scaling change that is an exact power of two (2^n) would have the advantage of avoiding rounding errors in any online re-scaling multiplications.
-Andy
类别
在 帮助中心 和 File Exchange 中查找有关 Simulink Coder 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!