Why is MATLAB rounding this?

I have a part of my code:
eps = 10^(log10(eps) + 0.125);
The program will round it off. For instance, if the original value of eps is 2, the calculated value is 133. This is rounded. If I do the same code in the command window, it gives me the correct value, which is a decimal.
If I run the code:
eps = 10^(log10(eps) - 0.125);
and the original value of eps is 0, it returns 0, and the while loop I have it in blows up.
I would like to stop it from rounding. These take place in a nested if statement, within a while loop.

4 件のコメント

Oleg Komarov
Oleg Komarov 2011 年 2 月 13 日
Are you sure it's not just a visualization problem?
And btw don't use eps as a variable since it's a builtin matlab fcn
Sam Falzone
Sam Falzone 2011 年 2 月 13 日
I'm not actually using eps. Not sure about a visualization error, but I'm pretty sure it is not just a visualization. I am displaying the variable with:
disp(['eps value = ' int2str(eps)])
Bruno Luong
Bruno Luong 2011 年 2 月 13 日
Please write what do you expect and you get with a concrete value for eps (and btw change the name eps to something else as Oleg suggested).
Paulo Silva
Paulo Silva 2011 年 2 月 13 日
So you want matlab to give 10^-Inf instead of zero?

サインインしてコメントする。

回答 (3 件)

Bruno Luong
Bruno Luong 2011 年 2 月 13 日

1 投票

a = 2
fprintf('%1.16e', 10^(log10(a) + 0.125))
2.6670428643266479e+000
What's your problem???

4 件のコメント

Paulo Silva
Paulo Silva 2011 年 2 月 13 日
his problem seems to be with a=0
Sam Falzone
Sam Falzone 2011 年 2 月 14 日
Not using eps. That's just what I used to describe the problem.
The problem is that 10^(log10(100) + 0.125) displays as 133, not the full decimal.
10^(log10(1) - 0.125) is not equal to 10^-INF. It's 10^-0.125.
Oleg Komarov
Oleg Komarov 2011 年 2 月 14 日
It doesn't give me any problem. What you do before that in your code? Do you have a variable log10 defined, or a self-written log10 fcn?
Paulo Silva
Paulo Silva 2011 年 2 月 14 日
I wonder why is Sam messing around with matlab built in functions?!

サインインしてコメントする。

Jan
Jan 2011 年 2 月 14 日

1 投票

If you use INT2STR to display the result, the printed number is rounded - read the help text of INT2STR! Use NUM2STR or SPRINTF instead to get the decimals.
Try Bruno's example and post, if it runs as wanted.
Walter Roberson
Walter Roberson 2011 年 2 月 14 日

0 投票

Double-check to see which "format" you have in effect. And if you are using a variable for the "100" part, check the data class of the variable: if it happened to be one of the integer types then the answer would be integer.

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

質問済み:

2011 年 2 月 13 日

Community Treasure Hunt

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

Start Hunting!

Translated by