Modulo with expression not the same as modulo with value

5 visualizzazioni (ultimi 30 giorni)
Why does mod(1, Y-X) not approximately equal 0?
mod(1, 0.1)
ans = 0
X = 2;
Y = 2.1;
Y - X
ans = 0.1000
mod(1, Y - X)
ans = 0.1000
I realize that Y - X ~= 0.1 but
isapprox(Y - X, 0.1, "loose")
ans = logical
1
therefore, why not
isapprox(mod(1, Y - X), 0, "loose")
ans = logical
0
My guess is that it is either something to do with floating point precision or the fact that it is an expression vs. a value?

Risposta accettata

Matt J
Matt J il 18 Lug 2025
Modificato: Matt J il 18 Lug 2025
Because mod(1,z) is a discontinuous function of z at 0.1,
fplot(@(z) mod(1,z),[0,0.2])
  1 Commento
James
James il 18 Lug 2025
Modificato: James il 18 Lug 2025
Ah right, thought I was missing something obvious, thanks.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Programming in Help Center e File Exchange

Tag

Prodotti


Release

R2025a

Community Treasure Hunt

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

Start Hunting!

Translated by