How do I make my code compatible with both single and double precision mode?

In order to generate single-precision code with the "-singleC" flag, I have to manually change some of my functions. For example, whenever I use the "realmax" function, I have to manually change every instance of "realmax('double')" to "realmax('single')".
How can I automate this process? What is the best workflow to easily convert back and forth between single and double precision?

 採用された回答

MathWorks Support Team
MathWorks Support Team 2022 年 12 月 7 日
You should utilize the "like" syntax supported by a number of functions. The documentation for the "zeros" function at the following link describes this workflow: https://www.mathworks.com/help/fixedpoint/ref/zeros.html#btuia59-1
In your case, this workflow could be applied as follows:
realmax('like', 0)
specifying that the "realmax" constant should have the same type as "0", i.e., single-precision when code is generated in single-precision mode (as with the "-singleC" flag), or double-precision otherwise.
This same workflow is also recommended for easy conversion between floating and fixed point types.

その他の回答 (0 件)

カテゴリ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by