reduce precision of a number

조회 수: 231 (최근 30일)
Dwight
Dwight 2013년 6월 22일
이동: Dyuman Joshi 2024년 2월 25일
I am having a problem trying to reduce precision output of a number
for example
x = 1.123456 I want x = 1.123 (only 3 values after a decimal and not a string)
I use x = round(x*1000)/1000 and get x = 1.1230 (I dont want the end zero)
I use x = sprintf('%.3f',x) and get a string '1.123' (i dont want a string)
so i use x = str2num(sprintf('%.3f',x)) and get x = 1.1230 (again with the zero)
please help me remove a zero at the end. I am not concerned with precision.
  댓글 수: 5
Walter Roberson
Walter Roberson 2024년 2월 24일
이동: Dyuman Joshi 2024년 2월 25일
The options are
round(X)
or
round(X, N)
or
round(X*10^N)/10^N
or
vpa(X, N) %requires symbolic toolbox
For positive N, the output of round() will not generally happen to be an exact multiple of a power of 2, so the output() of round() will not generally happen to be exactly representable in binary. (It will happen sometimes -- for example round(0.25319,2) will happen to be exactly 0.25 which is exactly representable in binary -- but round(0.26319,2) would be approximately 0.26 and exactly 0.2600000000000000088817841970012523233890533447265625 decimal.)
The internal value is different from how the values are displayed.
If format short is in effect, then a complicated set of rules is used. Values with absolute value between 0.001 and 999.9999 that do not happen to be integers are generally displayed with 4 digits after the decimal place. Values outside that range are displayed in scientific notation with 5 signficant digits (including the value before the decimal point.) If you are using format short then there is no way to get rid of trailing 0s
If format long g is in effect and the absolute value is between 1e-4 and (1e15 -1e15*eps) then decimal representation will be used, and trailing zeros will be stripped away.
If you want other display rules, then you will need to code them yourself using fprintf() or sprintf() or compose()
Stephen23
Stephen23 2024년 2월 25일
This question is a very good example of
The actual problem is that the OP was attempting to test for exact equivalence of binary floating point numbers.
The actual solution is given here:

댓글을 달려면 로그인하십시오.

답변 (4개)

Azzi Abdelmalek
Azzi Abdelmalek 2013년 6월 22일
편집: Azzi Abdelmalek 2013년 6월 22일
When you use x for calculation use
out = str2num(sprintf('%.3f',x)) % when x is not displayed, the 0 does not appear!
%or
x = round(x*1000)/1000
When you want to display it use
sprintf('%.3f',x)
  댓글 수: 6
Walter Roberson
Walter Roberson 2013년 6월 25일
Not using regexp, No. Instead use
find( (x(:,1)-b) < 1/1000 )
Iain
Iain 2013년 6월 25일
find( abs(x(:,1)-b) < 1/1000 )
would be better.

댓글을 달려면 로그인하십시오.


Steven Lord
Steven Lord 2017년 2월 23일
This is an old discussion, but if you're using release R2014b or later you can round to a specified number of digits.
  댓글 수: 2
Faez Alkadi
Faez Alkadi 2017년 9월 21일
Steven, I'm using R2017a. and i used round function as
x=2.123456789123456789
y=round(x,3),
so i get
y=2.123000000000000000
where i want it to be (saved) as
y=2.123
its odd that mat lab doesn't have this. Just dumb whatever comes after certain decimal number.
Walter Roberson
Walter Roberson 2017년 9월 21일
편집: Walter Roberson 2017년 9월 21일
MATLAB uses IEEE 754 Binary Double Precision to represent floating point numbers. All floating point scheme that uses binary mantissas cannot exactly represent 1/10, just like decimal representation schemes cannot exactly represent 1/3 or 1/7 .
IEEE 754 also defined a Decimal Double Precision representation scheme, which can represent 2.123 exactly. However, computing those values in software is much slower. The only systems I know of that implement IEEE 754 Decimal Double Precision in hardware are the IBM z90 series.
If you need a certain specific number of decimal places to be stored, then use rationals with a power-of-10 denominator.

댓글을 달려면 로그인하십시오.


Walter Roberson
Walter Roberson 2013년 6월 22일
At the command prompt give the command
format short g
Note: it is not possible to represent 1.123 exactly as a binary double precision number. The closest representable number is 1.1229999999999999982236431605997495353221893310546875
  댓글 수: 4
Mayank Ghugretkar
Mayank Ghugretkar 2019년 8월 19일
use
digits(4)
Answer= vpa(1.123456)
Walter Roberson
Walter Roberson 2019년 8월 19일
vpa() requires the Symbolic Toolbox

댓글을 달려면 로그인하십시오.


Francis Agbali
Francis Agbali 2019년 3월 24일
I would try using
format short
1.23400007787666

카테고리

Help CenterFile Exchange에서 Numeric Types에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by