How to stop matlab from truncating long decimal numbers

14 ビュー (過去 30 日間)
Jian Valenton
Jian Valenton 2019 年 7 月 7 日
コメント済み: Steven Lord 2021 年 11 月 26 日
Is there a way for a long string of decimal numbers like 0.92834387282837738 to not be truncated by matlab?? I was using it for a code (arithmetic coding) that requires precision and matlab truncating the numbers ruins everything.

回答 (1 件)

madhan ravi
madhan ravi 2019 年 7 月 7 日
The decimals are not truncated it’s just the appearance in command window:
doc format
doc sprintf
  4 件のコメント
Giuseppe Degan Di Dieco
Giuseppe Degan Di Dieco 2021 年 11 月 26 日
Hello Guys,
thanks for setting up this thread, it really helped me.
Indeed, the command
sprintf
is very useful to create string that meets the user's needs.
Best.
Steven Lord
Steven Lord 2021 年 11 月 26 日
No, sprintf won't work if the number is truncated by being stored as a double precision number before it gets to sprintf.
sprintf('%0.100f', 0.0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789)
ans = '0.0123456789012345684308780136007044347934424877166748046875000000000000000000000000000000000000000000'
If as Walter Roberson suggested you'd converted this directly from text to a sym object:
s = sym('0.0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789');
vpa(s, 100)
ans = 
0.0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789

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

カテゴリ

Help Center および File ExchangeConversion Between Symbolic and Numeric についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by