How to seperate fractional and decimal part in a real number
375 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
DSP Masters
le 16 Nov 2011
Commenté : Les Beckham
le 25 Jan 2023
Hi, Please help me in seperating fractional and decimal part in a real number. For example: If the value is '1.23', I need to seperate decimal part '1' and 'fractional part '0.23'.
Thanks and regards, soumya..
5 commentaires
Jeremy Wood
le 5 Juil 2017
Try using the floor operator to get the greatest integer below your number then subtract out your integer. For example 1.5 - floor(1.5) 0.5. It's trickier with negative numbers though so try using the absolute value of the number then when you get your fractional part multiply it by -1 so for -1.5 you would do -1*(1.5 - floor(1.5))
Bart McCoy
le 25 Juil 2018
EXTRACTING THE INTEGER PART
Extracting the integer part can be the most tricky part. MATLAB's "fix" function rounds toward zero, which is useful because it extracts the integer part of BOTH positive and negative numbers. It returns doubles and also works on NxM arrays.
By contrast, the "ceil" function always rounds upward, to the next integer in the POSITIVE direction; "floor" always rounds down, to the next integer in the NEGATIVE direction. Use whatever makes sense, but note:
INTEGER EXTRACTION: fix(pi) = 3; fix(-pi) = -3;
ROUNDING UP: ceil(pi) = 4; ceil(-pi) = -3;
ROUNDING DOWN: floor(pi) = 3; floor(-pi)= -4;
EXTRACTING THE FRACTIONAL PART:
fractional_part = value - fix(value);
Réponse acceptée
Walter Roberson
le 14 Fév 2016
number = -1.23
integ = fix(number)
frac = mod(abs(number),1)
2 commentaires
CS MATLAB
le 19 Sep 2016
What if the number is unknown and you want to compare decimal value with something..
Walter Roberson
le 19 Sep 2016
Comparing the fraction is risky
If you want to compare to a certain number of decimal places, N, I recommend comparing round(number*10^N)
Plus de réponses (5)
Naz
le 16 Nov 2011
number=1.23;
integ=floor(number);
fract=number-integ;
1 commentaire
Walter Roberson
le 16 Nov 2011
That fails on negative numbers. For negative numbers, you need fract=number-ceil(number)
Revant Adlakha
le 24 Fév 2021
Modifié(e) : Revant Adlakha
le 24 Fév 2021
How about this?
sign(x)*(abs(x) - floor(abs(x)))
% Number -> x = -1.23
% Answer -> -0.23
% Number -> x = 1.23
% Answer -> 0.23
Resam Makvandi
le 26 Déc 2012
Modifié(e) : Walter Roberson
le 24 Fév 2021
i think the better way is to use:
number = 1.23;
integ = fix(number);
fract = abs(number - integ);
it works for both negative and positive values.
2 commentaires
Les Beckham
le 25 Jan 2023
Did you try it?
x = [0.2, 1.2 1.0]
integ = fix(x)
fract = abs(x - integ)
Are Mjaavatten
le 9 Fév 2016
Modifié(e) : Are Mjaavatten
le 9 Fév 2016
mod(number,1)
5 commentaires
Are Mjaavatten
le 13 Fév 2016
Point taken. I should be old enough to have learned to read the problem definition. Still, I think it is nice to have a single command for the fractional part.
Jan
le 13 Fév 2016
What about rem instead of mod?
abs(rem(-0.123, 1)) % => 0.123
Voir également
Catégories
En savoir plus sur Logical 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!