强制MATLAB默认使用“单精度"? [英] Forcing MATLAB to use `single` precision as default?
问题描述
是否有一种方法可以强制MATLAB将single
精度用作默认精度?
Is there a way to force MATLAB to use single
precision as default precision?
我有一个MATLAB代码,我需要将其输出与C代码输出进行比较,并且C代码仅使用floats
编写,不允许使用doubles
.
I have a MATLAB code, whose output I need to compare to C code output, and C code is written exclusively using floats
, no doubles
allowed.
推荐答案
简短的回答:不能.
更长的答案:在大多数情况下,可以通过将初始变量设置为single来解决此问题.一旦完成,该类型(几乎总是)将在您的代码中向下传播. (请参见此和此在MathWorks上的线程).
Longer answer: In most cases, you can get around this by setting your initial variables to single. Once that's done, that type will (almost always) propagate down through your code. (cf. this and this thread on MathWorks).
例如,如果您执行以下操作:
So, for instance, if you do something like:
>> x = single(magic(4));
>> y = double(6);
>> x * y
ans =
4×4 single matrix
96 12 18 78
30 66 60 48
54 42 36 72
24 84 90 6
MATLAB使答案保持较低的精度.我偶尔会遇到一些内置和FileExchange的函数,它们会将输出重铸为两倍,因此您偶尔会撒在assert
语句中,以便在初次调试时保持诚实(或者最好放在断言是您编写的用于检查关键输入的任何子功能的第一行),但这可以使您获得99%的解决方案.
MATLAB keeps the answer in the lower precision. I have occasionally encountered functions, both built-in and from the FileExchange, that recast the output to be a double, so you will want to sprinkle in the occasional assert
statement to keep things honest during your initial debugging (or better yet put the assertion as the first lines of any sub-functions you write to check the critical inputs), but this should get you 99% of the way there.
这篇关于强制MATLAB默认使用“单精度"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!