Rounding big numbers?!

19 ビュー (過去 30 日間)
tonton
tonton 2016 年 4 月 22 日
コメント済み: Walter Roberson 2016 年 4 月 22 日
How come when I try to round say 1799999999999999916113920.000000 to 3 significant digits from the left I can't get 18000000000....... Here is my instructions:
fprintf('%f %f\n',round(1799999999999999916113920.000000,3,'significant'),1799999999999999916113920.000000)
My output:
1799999999999999916113920.000000 1799999999999999916113920.000000
  1 件のコメント
tonton
tonton 2016 年 4 月 22 日
Thanks very much for your answer, but it doesn't help for my main issue which is to get only say for example the 3 first digits of such numbers. I have convergence issues with fsolve/fzero solvers due to these big figures, and would like to round them.
This is not the decimal I am trying to round, but the whole figure, and in this example case it would be suitable to get 180000000000000000000000 instead of 1799999999999999916113920.
Regarding what you said Walter, this is very helpful but how would you proceed to keep only the first significant digits in order to avoid convergence issues due to the accuracy of the arithmetic in the calculations, please.
I am in need of something generic since I have lengthy arrays of such numbers.

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

採用された回答

Walter Roberson
Walter Roberson 2016 年 4 月 22 日
>> eps(1799999999999999916113920)
ans =
268435456
In other words, 180000000000000000000000 is not exactly representable in 53 bit binary floating point.
  2 件のコメント
Guillaume
Guillaume 2016 年 4 月 22 日
Or to be even clearer, there is no point you rounding to three decimals. Since your number is so big, the integral part can't even be stored exactly and the decimal part is not even stored.
You've got a potential error of about 268e6 on your number in any case.
Walter Roberson
Walter Roberson 2016 年 4 月 22 日
"it doesn't help for my main issue which is to get only say for example the 3 first digits of such numbers"
The number 180000000000000000000000 cannot be exactly represented in 53 bit binary floating point. Try your same fprintf operation with 1.80E+24 and with 180E+22 and you will see that what prints out as 1799999999999999916113920 is as close as you can get.
You can define
r3 = @(x) round(x, min(3-ceil(log10(x)),309))
but remember that 10 is not the base of binary floating point numbers, so beyond 1/eps = 4503599627370496 the representible numbers are no longer adjacent integers and you will not always be able to create a nice round decimal integer.

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

その他の回答 (0 件)

カテゴリ

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