mod function return wrong answer?
Show older comments
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 Comments
Sindar
on 5 Jul 2020
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
on 5 Jul 2020
Edited: madhan ravi
on 5 Jul 2020
Sindar I suggest moving your comment as an answer
Hiroaki Yamamoto
on 5 Jul 2020
Answers (1)
madhan ravi
on 5 Jul 2020
>> mod( -eps, 1 ) < 1
ans =
logical
1
4 Comments
Hiroaki Yamamoto
on 5 Jul 2020
madhan ravi
on 5 Jul 2020
Edited: madhan ravi
on 5 Jul 2020
>> sprintf('%.32f', mod(-1e-17, 1))
ans =
'1.00000000000000000000000000000000'
>> sprintf('%.32f', mod(-eps, 1))
ans =
'0.99999999999999977795539507496869'
Hiroaki Yamamoto
on 5 Jul 2020
madhan ravi
on 5 Jul 2020
Practically speaking it depends on what m is.
Categories
Find more on Argument Definitions in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!