If command does not work correctly.
3 次查看(过去 30 天)
显示 更早的评论
Hello, I am preparing a gui for some formulas.
I want to prefix the formula results like mili-micro.
For example
Let the result be 0.0025V(voltage)
I want to write 2.5mV or 2500uV in the result section
For this, I want to get an integer by multiplying the resulting number by 10.
I will act according to how many times the multiplication is made.
For example
Let the answer be 0.026V, the number obtained from 3 multiplications is 26.
Since it is the result of 3 multiplications, the prefix will be "mili"
To understand where the multiplication stops, I check whether the number is an integer after each multiplication.
I did some research for this.
floor(V)==V
I will check the number this way.
There are some problems in the code
for loop doesn't stop where it should
I=input("enter number");
disp(I)
if I<1
for multiplication = 1:100
I=I*10
if floor(I)==I
break;
end
end
end
For example, I will enter two numbers
enter number 0.159
159.000000000000e-003
I =
1.59000000000000e+000
I =
15.9000000000000e+000
I =
159.000000000000e+000
>>
It worked correctly for that number and stopped when the if condition was true,when it was an integer.
enter number 0.234
234.000000000000e-003
I =
2.34000000000000e+000
I =
23.4000000000000e+000
I =
234.000000000000e+000
I =
2.34000000000000e+003
I =
23.4000000000000e+003
I =
234.000000000000e+003
I =
2.34000000000000e+006
I =
23.4000000000000e+006
I =
234.000000000000e+006
I =
2.34000000000000e+009
I =
23.4000000000000e+009
I =
234.000000000000e+009
I =
2.34000000000000e+012
I =
23.4000000000000e+012
I =
234.000000000000e+012
I =
2.34000000000000e+015
I =
23.4000000000000e+015
but in this example, even though it was an integer (even though the if condition was met), the for loop did not stop.
Can you help me I'm very confused
1 个评论
Stephen23
2024-2-24
编辑:Stephen23
2024-2-24
"I want to prefix the formula results like mili-micro"
The simple approach is to download my FEX submission NUM2SIP():
and use it like this, with the examples from your question:
[num2sip(0.0025),'V']
[num2sip(0.026),'V']
[num2sip(32000000),'Hz']
[num2sip(25000),'Ω']
[num2sip(0.0000035),'F']
Note that the space is required by the SI standard.
Most users underestimate the level of detail required to perform provide this conversion in a robust way**. Given that I have already created this function and tested it on hundeds of test-cases (included on FEX), why reinvent the wheel?
** I have systematically tested all FEX submissions (that I could find) that claim to make this conversion. Most of them are buggy (usually with very similar oversight e.g. regarding how to handle significant figures correctly and how to round up to the next prefix when required), very few of them actually gave the correct output.
回答(1 个)
Dyuman Joshi
2024-2-18
编辑:Dyuman Joshi
2024-2-19
Welcome to the world of floating point numbers, where not all numbers can be represented exactly in binary form.
0.234 is not exactly stored as 0.234 -
I=0.234; %input("enter number");
disp(I)
%stored value upto 42 digits after decimal
fprintf('%0.42f', I)
Let's check the value of I (upto 20 digits after decimal), with for a few iterations -
if I<1
for multiplication = 1:5%100
I=I*10;
fprintf('Iteration %d\nI = %0.20f\nfloor(I) = %0.20f\n\n', multiplication, I, floor(I))
if floor(I)==I
break;
end
end
end
As you can see, I is not equal to floor(I), that's why the loop does not break.
When comparing floating point numbers, the best practice is to use a tolerance -
I = 0.234;
tol = 1e-3;
if I<1
for multiplication = 1:5%100
I=I*10;
fprintf('Iteration %d\nI = %0.20f\nfloor(I) = %0.20f\n\n', multiplication, I, floor(I))
if abs(floor(I)-I)<tol
break;
end
end
end
Now you can observe that the loop has been terminated after 3 iterations, as initially expected.
Note - The choice of tolerance depends on requirement and other factors.
P.S - If you want to see the exact conversion of a number (to string format) based on IEEE floating point number system, you can utilize this FEX submission by James Tursa - https://in.mathworks.com/matlabcentral/fileexchange/22239-num2strexact-exact-version-of-num2str?s_tid=prof_contriblnk
10 个评论
Stephen23
2024-2-24
"it can be any value between 10e-15 - 10e+15"
My function NUM2SIP() already copes with all real values from +REALMAX to -REALMAX, includes the SI prefixes added in 2022, and is tested on hundreds of testcases. Why reinvent the wheel?
Dyuman Joshi
2024-2-24
Let me clarify - the input will be of the form of - 32MHz, 25k, and 3.5µF, and you want the output to be of the form - '32.000,000 hertz, 25.000 ohms and 0.0000035 farads' (diregarding the units, of course)?
Or is it the opposite of what I mentioned above?
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!