How can I make sure mod(a,b) returns value less than b, without explicitly calling min( mod(a,b), b )?
mod( -eps, 1 ) = 1 when eps ~ 10^-17.
Numerical accuracy is not an issue, but I need mod( a, 1 ) be less than 1.

댓글 수: 3

What value would you like it to take?
mod(a,m) is expressed as:
b = a - m.*floor(a./m);
so, you have b = -eps - (1)*floor(-eps) = -eps - (-1) = 1
rem(-eps,1) will return -eps
madhan ravi
madhan ravi 2020년 7월 5일
편집: madhan ravi 2020년 7월 5일
Sindar I suggest moving your comment as an answer
Hiroaki Yamamoto
Hiroaki Yamamoto 2020년 7월 5일
I expected mod( a, m ) < m (less than m),
but numerically mod(a, m) <= m (less than or equal to m) is the result when a = -1e-17 and m = 1.
If, internally, 1 - 10^-17 = 1 is calculated and returned, I think 0 is the mathematically correct return value rather than 1.

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

답변 (1개)

madhan ravi
madhan ravi 2020년 7월 5일

0 개 추천

>> mod( -eps, 1 ) < 1
ans =
logical
1

댓글 수: 4

Hiroaki Yamamoto
Hiroaki Yamamoto 2020년 7월 5일
I get different answers as follows, which indicare mod(a, m) can be equal to m, not less than m.
>> mod(-1e-17,1) < 1
ans =
logical
0
>> mod(-1e-17,1) == 1
ans =
logical
1
madhan ravi
madhan ravi 2020년 7월 5일
편집: madhan ravi 2020년 7월 5일
>> sprintf('%.32f', mod(-1e-17, 1))
ans =
'1.00000000000000000000000000000000'
>> sprintf('%.32f', mod(-eps, 1))
ans =
'0.99999999999999977795539507496869'
Hiroaki Yamamoto
Hiroaki Yamamoto 2020년 7월 5일
My question is, mathematically which of the following is correct or intended to be implemented.
1) mod(a, m) < m
or
2) mod(a, m) <= m.
Practically, I found mod(a,m) = m can happen and I do explicit check and avoid problems caused by the case mod(a,m) = m.
madhan ravi
madhan ravi 2020년 7월 5일
Practically speaking it depends on what m is.

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

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

제품

릴리스

R2020a

질문:

2020년 7월 4일

댓글:

2020년 7월 5일

Community Treasure Hunt

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

Start Hunting!

Translated by