Calculation Error sum float

I Write This Code:
clc;
clear;
format long
a=1;
for n=1:1:100
a=a+.1;
end
a
and answer is:
a =
10.999999999999979
!!!!! Why not: a= 11
How I Can Solve it?

1 件のコメント

James Tursa
James Tursa 2014 年 5 月 16 日
See Azzi's link below. But for your particular case this is the decimal equivalent of the closest number to 0.1 in IEEE double format:
>> num2strexact(0.1)
ans =
0.1000000000000000055511151231257827021181583404541015625
You can find num2strexact here:

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

回答 (1 件)

Azzi Abdelmalek
Azzi Abdelmalek 2014 年 5 月 16 日

1 投票

2 件のコメント

Maha dasra
Maha dasra 2014 年 5 月 16 日
How I Can Solve it?
José-Luis
José-Luis 2014 年 5 月 16 日
  1. Use a computer that doesn't use the binary system. There are a few such strange base 10 machines out there.
  2. Try variable precision arithmetic. Either with the symbolic toolbox or the excellent vpa from the file exchange.

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

カテゴリ

タグ

質問済み:

2014 年 5 月 16 日

コメント済み:

2014 年 5 月 16 日

Community Treasure Hunt

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

Start Hunting!

Translated by