Converting a fraction into a floating number
Ältere Kommentare anzeigen
Hi, I want to simply convert a fraction into a float. When I checked with the help, it seems really simple. To convert 2/5 into a float, it seems like I just need to type float(2/5). But, when I do that, I have for response :Error using float (line 46) The input argument to float was not a supported type. The only recognized input character vectors are 'single' and 'double'. The input type was 'double'. Can someone explain me that?
5 Kommentare
Stephen23
am 30 Jan. 2018
"When I checked with the help, it seems really simple. To convert 2/5 into a float, it seems like I just need to type float(2/5)"
Can you please show a reference to the MATLAB help, where that conversion is stated?
Samuel Généreux
am 30 Jan. 2018
John D'Errico
am 30 Jan. 2018
I assume you mean the MuPAD function float. float itself is NOT part of MATLAB proper. There is no MATLAB function called float, nor do you use float to create a floating point number in MATLAB, as I said in my answer.
Numbers are automatically assumed to be cast as floating point numbers by default in MATLAB.
Walter Roberson
am 30 Jan. 2018
John: float() is a legacy Simulink Fixed Point function. https://www.mathworks.com/help/simulink/slref/float.html
John D'Errico
am 30 Jan. 2018
Bearbeitet: Walter Roberson
am 15 Dez. 2023
No. The examples given by Samuel are NOT consistent with the use of float as it is seen in the Simulink docs. However, they ARE consistent with the examples shown in the MuPAD documentation. That is why I talked about MuPAD. In fact, if I read the examples shown for the MuPAD float, we find very coincidentally these examples:
float(17), float(PI/7 + I/4), float(4^(1/3) + sin(7))
As copied directly out of this link:
Antworten (3)
John D'Errico
am 30 Jan. 2018
No. There is no need to use float.
x = 2/5
x =
0.4
whos x
Name Size Bytes Class Attributes
x 1x1 8 double
x is automatically a floating point number, of class double.
1 Kommentar
Samuel Généreux
am 30 Jan. 2018
Cyrus Azizzadeh
am 22 Dez. 2021
0 Stimmen
If I have an equation like (256753367864*t)/(46897532469) How can I convert that to float?
6 Kommentare
Could you confirm that you want to work at the MuPAD level and convert the expression to a mix of symbolic variables and MuPAD float() numbers?
I suspect that is not what you want. I suspect you want
expr = str2sym('(256753367864*t)/(46897532469)')
vpa(expr)
Cyrus Azizzadeh
am 23 Dez. 2021
Thank you so much for your answer. Str wasn't my mean. I have another question: when I use vpa(equation,4) the accuracy of my calculation will be reduced? (Compare by vpa(equation,32))
format long g
x = rand()
xsr = sym(x), xsd = sym(x, 'd')
xs4r = vpa(xsr, 2), xs4d = vpa(xsd, 2)
xs32r = vpa(xsr, 32), xs32d = vpa(xsd, 32)
d4r = double(xs4r), d4d = double(xs4d)
d32r = double(xs32r), d32d = double(xs32d)
x - d4r, x - d4d
x - d32r, x - d32d
x2 = round(x,2), x32 = round(x, 32)
x2 - xs4r, x2 - xs4d
x32 - xs32r, x32 - xs32d
There is more than one way that a floating point number can be converted to symbolic. The default, sym(number) is the same as sym(number, 'r') which (mostly) converts to rational-ish numbers; the major alternative is sym(number,'d') which constructs a software decimal number.
If you vpa() one of the rational numbers to a lower number of decimal places, then the result will not be as accurate as the original. If you vpa() one of the 'd' numbers, to a lower number of decimal places, then the result is as accurate as the original.
But vpa() of a rational includes an internal change in form, and the number of decimal places specified tells it when it is okay to give up on the conversion.
We can tell from the above tests that when you vpa() a number that is already in software decimal form, either through sym('d') or by a previous vpa(), then although it changes the result of displaying the value, that the value is retained in full precision internally. I do not know at the moment if the same thing holds if you are working with a large enough number of decimal places.
Cyrus Azizzadeh
am 23 Dez. 2021
Thanks again. Indeed because of my expression is more than 25000 character I have to use "vpa" to make them small to observe all of them for copy. In Matlab the the coefficient of variables wrote in rational form and it made my expression very large. Is there any other way to make my expressions smaller than 25000 character?
Walter Roberson
am 23 Dez. 2021
See https://www.mathworks.com/matlabcentral/answers/337896-odd-r-n-output-from-symbolic-multiplication#answer_265002 and note the comment about TEXTWIDTH
This is an approach to get some readable representation of the result, not to get the result in MATLAB form. The result will be in the internal MuPAD language
Cyrus Azizzadeh
am 24 Dez. 2021
Thanks a lot. I'll test that
Rishab
am 15 Dez. 2023
0 Stimmen
just use the double() function
Kategorien
Mehr zu Special Values finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!