Single Precision by Default? ... lots of auxiliary variables to cast

45 ビュー (過去 30 日間)
MikeStein
MikeStein 2011 年 6 月 3 日
編集済み: Scott Gordon 2023 年 1 月 22 日
Hi all,
I have an algorithm that I'd like to run using single precision instead of the default double precision. However, this algorithm uses many many auxiliary variables to store physical parameters that I'm simulating, and I really don't want to cast each and every one of them... Is there a way to set single precision to be the default? Otherwise, do you all have any suggestions?
Another complication that may matter... I set up the simulation parameters in an outside script, before calling a function with my primary algorithm. Both the outside script and the algorithm function contain a number of theses auxiliary variables (changing spatial grids, electromagnetic fields, and other physical parameters)

採用された回答

Walter Roberson
Walter Roberson 2011 年 6 月 3 日
There is no way to set single precision by default.
  3 件のコメント
Walter Roberson
Walter Roberson 2018 年 9 月 11 日
Unfortunately it would be difficult to figure out what these links refer to now that the newsreader is gone :(
There is feature() and system_dependent() calls that can set the default precision. No seems to know what happens if you call library routines after having set these.
In a discussion about setting the round-off mode using feature() I read a credible claim that MATLAB reset the mode after every external call. It is plausible that MATLAB might reset precision after every external call as well.

サインインしてコメントする。

その他の回答 (1 件)

Sean de Wolski
Sean de Wolski 2011 年 6 月 3 日
How does it hurt if the other variables are double precision, the lowest precision will be kept.
single(magic(3)).*magic(3);
class(ans)
ans =
single
  2 件のコメント
MikeStein
MikeStein 2011 年 6 月 3 日
Thanks both of you for your replies! I did find a way to 'trickle' down the single precision by casting a limited set of initial variables. I wish there was a Matlab feature for this though...
-Mike
Scott Gordon
Scott Gordon 2023 年 1 月 22 日
編集済み: Scott Gordon 2023 年 1 月 22 日
The example given is clearly cheating, .* is a kind of cast.
The two things not mentioned - a) Matlab is a HUGE memory hog and manages memory very inefficiently.
b) any number can be cast to anything (within reason), but that does not manage memory at all.
The slow answer is: do the calculations in double, cast to single and throw out the double.
In some cases you can cast to single first, and do the calculation after. However largely this just rains down errors, because Marlab's default is double. One is tempted to roll over in so many ways, but memory management is not one of them. Since Matlab manages memory poorly, one is forced to manage memory MANUALLY - which is a major pain.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by