reduce precision of a number

577 次查看(过去 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 个评论
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 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by