- "Is there any inbuilt function to count these digits after decimal"   No
- Did you read and understand Why is 0.3 - 0.2 - 0.1 (or similar) not equal to zero?
how to calculate digits after decimal point?
17 ビュー (過去 30 日間)
古いコメントを表示
Hello everyone! I want to calculate the number of digits after decimal point but want to ignore the trailing zeros. example: 1.2344500 digits after decimal should be 5.
Is there any inbuilt function to count these digits after decimal or to remove trailing zeros? Thanks in advance...
5 件のコメント
Stephen23
2015 年 3 月 21 日
編集済み: Stephen23
2021 年 4 月 28 日
Most decimal representations of binary fractional numbers do not have a finite number of digits: the fractional values that you are giving most certainly do not have "zero" for all trailing digits. You need to learn about floating point numbers, this would be a good start:
This is also a topic that has been discussed many times before:
Note also that a double floating point value only has a decimal precision of around 15 to 17 digits, which means it is possible that your "altering" of a decimal digit after the fourteenth decimal digit may be simply beyond any representable change in the floating point value: i.e. is meaningless.
You might be interested in James Tursa's submission "num2exact" which shows the decimal digits required to represent a binary floating point value:
採用された回答
James Tursa
2015 年 3 月 20 日
編集済み: James Tursa
2015 年 3 月 23 日
See this related thread:
Using the function in the above link, the number of significant digits after the decimal point could be calculated as follows:
max(sigdigits(x)-floor(log10(abs(x)))-1,0) % Don't count sig digits before decimal pt
EDIT:
If you want to add a digit after the last significant digit, then something like this (assumes x is positive):
x = original_number; % E.g., 123.456
a = added_digit; % E.g., 9
y = x + a*10^(floor(log10(x))-sigdigits(x)); % New number
E.g.,
>> x = 123.456
x =
1.234560000000000e+02
>> a = 9
a =
9
>> y = x + a*10^(floor(log10(x))-sigdigits(x))
y =
1.234569000000000e+02
>> x = 1.23456789123
x =
1.234567891230000
>> a = 9
a =
9
>> y = x + a*10^(floor(log10(x))-sigdigits(x))
y =
1.234567891239000
>> x = 12345.12345
x =
1.234512345000000e+04
>> a = 8
a =
8
>> y = x + a*10^(floor(log10(x))-sigdigits(x))
y =
1.234512345800000e+04
3 件のコメント
James Tursa
2015 年 3 月 27 日
The term "LSB" implies that you are now working in the binary realm instead of the decimal realm. Those are two different animals and require completely different strategies than are posted in this thread. I suggest you open up a new Question stating what you are doing, exactly the data type you have, and how exactly you want to manipulate the LSB. There are several inbuilt MATLAB functions that can help with this.
その他の回答 (2 件)
per isakson
2015 年 3 月 23 日
Another approach, which is based on sprintf and regexp
nsg = significant( 1.2344500 )
nsg = significant( 1e-16 )
nsg = significant( 1e-17 )
nsg = significant( 1.2000000 )
nsg = significant( 1.0000000 )
echos
nsg =
5
nsg =
16
nsg =
33 % <<< error?
nsg =
1
nsg =
0
where
function nsg = significant( num )
ddd = max( [ 1, -floor( log10( eps( num ) ) ) ] );
frm = sprintf( '%%.%df', ddd );
str = sprintf( frm, num );
cac = regexp( str, '(?<=\.)\d*?(?=0*+\>)', 'match', 'emptymatch' );
nsg = numel( cac{:} );
end
2 件のコメント
James Tursa
2015 年 3 月 24 日
編集済み: James Tursa
2015 年 3 月 24 日
nsg = significant( 1e-17 )
nsg =
33 % <<< error?
Yes, unfortunately sprintf can't be relied upon to print 0 digits when the number of digits printed gets down to eps of the number. E.g., for 1e-17 the string generated is:
.000000000000000010000000000000001
But that 1 at the end really isn't significant. E.g.,
>> isequal(.000000000000000010000000000000001,1e-17)
ans =
1
Also, the behavior of sprintf on Windows vs Unix can be very different when printing out these ending digits. E.g., on Windows I get this:
>> num2strexact(1e-17)
ans =
1.00000000000000007154242405462192450852805618492324772617063644020163337700068950653076171875e-17
>> sprintf('%97.92e',1e-17)
ans =
1.00000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000e-17
So on Windows those trailing digits get rounded up to 1 regardless of how many more digits you print. However, on Unix systems a different algorithm is used so that sprintf has essentially the behavior of num2strexact (with rounding depending on number of digits printed out). In fact, this difference in behavior is why I wrote num2strexact in the first place. So getting a working algorithm that depends on the behavior of sprintf for these trailing digits is going to be very difficult.
For reference, the next lower number in IEEE double is:
>> num2strexact(1e-17-eps(1e-17))
ans =
9.999999999999999174680285036430562640498207781290622431225045829705777578055858612060546875e-18
Murat Balc?
2021 年 4 月 27 日
function decimal=digit(x)
x=abs(x);
y=x*10^0;
a=round(x(:)-floor(x(:)),8);
b=num2str(a);
[d c]=size(b);
decimal=max(c)-2;
Riad
2025 年 3 月 25 日
You should use extractAfter function, strrep can also remove the tailling zeros
>> num = '1.2344500';
decimals = extractAfter(num,'.')
decimals =
'2344500'
0 件のコメント
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!