reduce precision of a number

244 vues (au cours des 30 derniers jours)
Dwight
Dwight le 22 Juin 2013
Déplacé(e) : Dyuman Joshi le 25 Fév 2024
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 commentaires
Walter Roberson
Walter Roberson le 24 Fév 2024
Déplacé(e) : Dyuman Joshi le 25 Fév 2024
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 le 25 Fév 2024
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:

Connectez-vous pour commenter.

Réponses (4)

Azzi Abdelmalek
Azzi Abdelmalek le 22 Juin 2013
Modifié(e) : Azzi Abdelmalek le 22 Juin 2013
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 commentaires
Walter Roberson
Walter Roberson le 25 Juin 2013
Not using regexp, No. Instead use
find( (x(:,1)-b) < 1/1000 )
Iain
Iain le 25 Juin 2013
find( abs(x(:,1)-b) < 1/1000 )
would be better.

Connectez-vous pour commenter.


Steven Lord
Steven Lord le 23 Fév 2017
This is an old discussion, but if you're using release R2014b or later you can round to a specified number of digits.
  2 commentaires
Faez Alkadi
Faez Alkadi le 21 Sep 2017
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 le 21 Sep 2017
Modifié(e) : Walter Roberson le 21 Sep 2017
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.

Connectez-vous pour commenter.


Walter Roberson
Walter Roberson le 22 Juin 2013
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 commentaires
Mayank Ghugretkar
Mayank Ghugretkar le 19 Août 2019
use
digits(4)
Answer= vpa(1.123456)
Walter Roberson
Walter Roberson le 19 Août 2019
vpa() requires the Symbolic Toolbox

Connectez-vous pour commenter.


Francis Agbali
Francis Agbali le 24 Mar 2019
I would try using
format short
1.23400007787666
  1 commentaire
mohamed kandil
mohamed kandil le 15 Juil 2022
format bank

Connectez-vous pour commenter.

Catégories

En savoir plus sur Numeric Types dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by