Function that returns value after decimal point?

조회 수: 1 (최근 30일)
sono
sono 2012년 10월 4일
Is there a function that returns the value after the decimal?
EX:
a = 2.876
function(a) = 0.876
Thank You

채택된 답변

Jan
Jan 2012년 10월 4일
편집: Jan 2012년 10월 4일
a = 2.876
b = mod(a, 1)
What is the wanted output for negative numbers? REM might be helpful:
x = -3.14;
mod(x, 1) % 0.86
rem(x, 1) % -0.14
abs(rem(x)) % 0.14
mod(abs(x), 1) % 0.14
  댓글 수: 1
sono
sono 2012년 10월 7일
Thanks. I was using this to calculate the relative 0-2pi angle from one greater then 2pi.
Negative angles were important because it was for quadrant identification and if angle is negative you have to spin around origin in opposite direction so keeping the sign was important. To make the function work the same way for negative angles I just used:
-mod(abs(a),1)
Making it positive so mod(a,1) function would work in right direction then re-adding the negative sign.

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

추가 답변 (1개)

Azzi Abdelmalek
Azzi Abdelmalek 2012년 10월 4일
편집: Azzi Abdelmalek 2012년 10월 4일
out=a-fix(a)
%or
A=num2str(a);
out=str2num(['0' A(strfind(A,'.'):end)])
  댓글 수: 2
Jan
Jan 2012년 10월 4일
What happens for integer values?
Azzi Abdelmalek
Azzi Abdelmalek 2012년 10월 4일
편집: Azzi Abdelmalek 2012년 10월 4일
a=1
a-fix(a)=0
and
A=num2str(a);
out=str2num(['0' A(strfind(A,'.'):end)])=0

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

카테고리

Help CenterFile Exchange에서 Operators and Elementary Operations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by