Error in matlab function
古いコメントを表示
Hi,
does anybody knows why matlab doesn't calculate the correct value of a function?
I have the function: err_acc = err_acc + (err_corr * dt);
where: err_acc = 0; dt = 0.02; err_corr = -6
and the answer is always 0.
Everything is in int32 type.
Thanks for any help.
Best Regards.
2 件のコメント
Leonardo Tommasini
2017 年 11 月 7 日
Steven Lord
2017 年 11 月 7 日
Show your exact code. My guess is that you're assigning the result of that computation in double back into an element of an int32 array.
採用された回答
その他の回答 (2 件)
Cam Salzberger
2017 年 11 月 7 日
編集済み: Cam Salzberger
2017 年 11 月 7 日
Hello Leonardo,
If all variables are of int32 datatype, then they can only contain integer values. Thus, dt would contain 0, rather than 0.02, because when a floating point number is cast to an integer datatype, the non-integer part is dropped.
Even if dt is actually a floating point datatype, if you do a double*integer, MATLAB will make the result an integer datatype:
0.02*int32(-6)
ans =
int32
0
-Cam
2 件のコメント
Leonardo Tommasini
2017 年 11 月 7 日
Image Analyst
2017 年 11 月 7 日
Use double(). See my answer.
Geoff Hayes
2017 年 11 月 7 日
Leonardo - if you are expecting a non-integer answer, then the fact that all variables are int32 will prevent this. If the variables are doubles, then
err_acc = 0;
dt = 0.02;
err_corr = -6
the answer is
err_acc = err_acc + (err_corr * dt)
err_acc =
-0.1200
カテゴリ
ヘルプ センター および File Exchange で LEGO MINDSTORMS EV3 Hardware についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!