Simulate single precision arithmetic in Matlab?
11 次查看(过去 30 天)
显示 更早的评论
Hi! I'm trying to replicate, in Matlab, compiled algorithms implemented in C using single-precision arithmetic; I have the C source code, and an executable which is reputed to implement said C source, but no convenient means to compile and link said source myself. (Yes, I know I can download the Microsoft C compiler, incl. a no-frills version of Visual Studio, for free, but that's hardly "convenient.") I've implemented the algorithms and I'm 99% sure they're right because the "residuals" (the difference between my results and those of the original C implementations) are more-or-less randomly distributed around zero, and about 85% of them are within half of the precision of the C implementations' output (which varies, but, for example, when it's 1e-4, 85% of my residuals satisfy abs(res)<5e-5). It's the other 15% that leave me 1% uncertain, and my attempts to explain the discrepancy have brought me to the single/double precision issue: I have good cause to suspect that the C implementations are using single-precision arithmetic, and I've been led to believe that Matlab always performs double-precision arithmetic, even on single precision "data," yes? (I tried making 'single' all the data in my implementation, and confirmed after each "gross" calculation that the output remained so, but that only reduced the number of bad residuals by 3 out of 4118 total input points.) So, I'm wondering if there's either a "convenient" way to simulate single-precision arithmetic in Matlab (I searched File Exchange for relevant functions, but found none) or to estimate/bound the "error" introduced by using single-precision arithmetic (including power/root and exponential/log functions) instead of double. Thanks!
2 个评论
Geoff
2012-4-11
Look at the C code. If it's full of variables of type 'float', it's using single precision. If it's full of variables of type 'double' it _might_ be using double precision... Depending on the various functions (if any) that are used to obtain intermediate values.
Samana
2024-4-21
Round-off error can often accumulate when performing computations with repeated additions and subtractions. The error accumulates differently depending on the precision with which the numbers are stored. To demonstrate this concept, consider the following summation: The analytical result for is exactly zero, but you will find that does not equal zero when performing this computation with MATLAB due to the finite precision with which numbers are stored in memory. For this problem, write a script that performs the computation two different ways: Use all double precision (MATLAB's default) numbers in the calculation. Assign the result for to the variable named ZeroDouble. Use all single precision numbers in the calculation. Assign the result for using single precision to the variable named ZeroSingle.
采纳的回答
Walter Roberson
2012-4-11
Look on undocumentedmatlab.com for the information about the features supported by system_dependent()
1 个评论
Jan
2012-4-11
Good idea! "feature('SetPrecision', 24)" or 53 or 64 affects the rounding, when intermediate results are copied from the processor registers back to the memory. While the Intel processors and e.g. the lovely 68040 have 80 bit registers in the floating point unit, DOUBLE have 64 bits only. A processor flag can control, if this register is rounded. Matlab uses 53 bit precision for the mantissa as default (this means 64 bit for the DOUBLE when stored in a register). This is useful to get equivalent results for running Matlab on different platforms. Increasing the accuracy by "feature('SetPrecision', 64)" -this means 80 bits in the register- does not reduce the speed, but it causes a platform dependency of the results.
更多回答(4 个)
Jan
2012-4-10
Matlab uses single precision arithmetic for single precision values. Therefore there is no reason to simulate it.
I'd suggest to use double arithmetic in the C-function instead for the intermediate results to increase the accuracy.
Downloading and installing the Windows-SDK needs several minutes only.
3 个评论
Jan
2012-4-11
Simply try it:
k = eps('single'); one = single(1);
a) disp(one + k - one) % Single values
b) disp(one + k/10 - one) % Single values
c) disp(1.0 + double(k)/10.0 - 1.0) % Double values
Now I expect that b) replies zero, because adding k/10 to 1 is exactly 1. But in double precision arithmetics, 1+k/10 differs from 1.0.
Converting all values to SINGLE and using SINGLE precision arithmetic in consequence can conceal programming errors, e.g. if some _small_ values are accidently added to _large_ value. If 15% of the results are effected substantially by using different precisions, the algorithm is instable or contains a bug.
Setting the floating point processor flag for the storage of intermediate values by "feature('SetPrecision', 64)" (See Walter's answer) should not change the result of a computation substantially. But the result of 1e17 + 1 - 1e17 can be influenced -- this means that the naive approach to calculate a SUM is not stable.
Jan
2012-4-11
Another evidence for SINGLE arithmetics in Matlab is using Matlab 6.5: Due to the missing implementation, SINGLE(1)+SINGLE(1) failed with an error message. This means, that at least in this old release the operation has not been performed in DOUBLE precision with a later re-casting to SINGLE. But of course this is not a proof.
FILTER stores the temporary accumulator variables as SINGLE, when the signal is SINGLE. This limits the accuracy of the results remarkably without increasing the speed dramatically. The Mex functions for faster filtering I've published in the FEX accumulates the internal status as DOUBLEs to reduce the artifacts produced by rounding.
James Tursa
2012-4-11
It is very difficult to verify what is actually happening behind the scenes for single precision calculations. E.g., on a PC it may be the case that certain calculations on float variables get converted to 80-bit extended precision for the actual calculations and then re-converted to float to store the result. This can be the case for your C code even though all the variables are float. I am not sure which functions of MATLAB are strictly single and which are not. Bottom line is I seriously doubt you will be able to get an exact match no matter how much time you spend on the problem, unless the code is very simple. Whether the residuals you are currently seeing are "small" or not depends on the algorithms you are implementing. I think Jan's advice is a pretty good one ... do it all in double and compare that result to the single result.
0 个评论
David Goldsmith
2012-4-11
2 个评论
Walter Roberson
2012-4-11
You know that in C you can coerce apples into being oranges, since they both have the same underlying quark bits. MATLAB is a bit more restrictive: it only allows you to coerce whole numbers of unstemmed apples into unstemmed oranges.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!