how can i take a variable which store decimal values
7 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
I am having problem to store decimal value. whenever i am taking greater denominator value it shows only zero . please can any one help me asap
1 Commento
dpb
il 24 Dic 2015
Well, from just the description we don't know precisely what operation you mean by "[I} am taking greater denominator value". Show us code and input.
Risposte (1)
Walter Roberson
il 25 Dic 2015
If you have variables A, B, that are members of one of the integer classes, such as uint8 or int16, then when you divide A/B the result is defined to be the same as
cast(double(A)/double(B), class(A))
That is, a fraction will be calculated but the result will be made back into the original data type. The process of converting a floating point value to an integer data type is defined to be done by rounding. uint8(159.3) rounds to uint8(159), uint8(159.6) rounds to uint8(160) .
Therefore if you have two integer variables, A/B and B > A, then the result will come out as either 0 or 1 depending on which way the fraction double(A)/double(B) rounds. In particular for A/B with B from A up to and including 2*A will round to 1, and A/B with B > 2*A will round to 0.
If this is a problem then you should either not be using integer class variables or you should be converting the values to double before doing the division, double(A)/double(B) so that the result is not converted back to class(A).
Note: It isn't exactly class(A) that is used in practice but it only makes a difference if you start mixing double and integer class in an operation.
2 Commenti
Madhu
il 22 Gen 2024
Spostato: John D'Errico
il 22 Gen 2024
x = 195
fd = 2
y = x./fd
i am getting the value as 98
but i need the value as decimal as 97.2
what can i do/
please anyone help me
John D'Errico
il 22 Gen 2024
your number is an INTEGER. Dividing it by 2 yields another integer.
x = uint8(195)
y = x/2
class(y)
You need to convert it to a double FIRST.
z = double(x)/2
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!