Matlab syms variable precision? -
just having issues variable precision in matlab..
i have code:
x = 0.1; syms y; s = solve(x+1==(1+y)/(1-y),y); y = double(s); val = abs(((2^2)*(y^2))/(2*(y-1)^2))
but val rounded off. should getting val = 0.0049999
instead getting val = 0.0050
.
anyone have idea why?
thanks.
edit: adding code
x = 0.1; syms y; s = solve(x+1==(1+y)/(1-y),y); y = double(s); val = abs(((2^2)*(y^2))/(2*(y-1)^2)) sprintf('%22.20f',val) i=1:2 val= val+((2^i)*(y^i))/(i*(y-1)^i); sprintf('%22.20f',val) end
the first sprintf works , shows correct rounding, second doesnt!!
it has floating-point representation , how matlab displays such numbers readability. if add line end of code:
sprintf('%22.20f',val)
you'll get:
ans = 0.00499999999999999924
edit
though technically deals python, this website offers brief , concise overview on limitations of floating-point representations.
Comments
Post a Comment