https://handwiki.org/wiki/index.php?title=DMelt:JMathlab/2_Numbers&feed=atom&action=history DMelt:JMathlab/2 Numbers - Revision history 2022-01-22T14:33:14Z Revision history for this page on the wiki MediaWiki 1.35.1 https://handwiki.org/wiki/index.php?title=DMelt:JMathlab/2_Numbers&diff=281&oldid=prev imported>Jworkorg at 22:39, 17 December 2016 2016-12-17T22:39:48Z <p></p> <p><b>New page</b></p><div>=== Numbers ==<br /> <br /> Numbers are entered in the usual Computer format: with optional decimal point and decimal exponent following the letter e (or E). The numbers 5364 and<br /> &lt;math&gt;-1.723478265342 10^{12}&lt;/math&gt; should be entered like:<br /> <br /> &lt;jcode lang=&quot;matlab&quot;&gt;<br /> &gt;&gt; 5364<br /> ans = 5364<br /> &gt;&gt; -1.723478265342e12<br /> ans = -1.7235E12<br /> &lt;/jcode&gt;<br /> <br /> Most of the time these will be stored as floating point data (double, IEEE standard 754). They are rounded to 5 significant digits for display, but for calculations the full precision of this format is always preserved (15-16 decimal digits). By switching the format (format long) all significant places are displayed.<br /> <br /> &lt;jcode lang=&quot;matlab&quot;&gt;<br /> &gt;&gt; format long<br /> &gt;&gt; -1.723478265342e12<br /> ans = -1.723478265342E12<br /> &lt;/jcode&gt;<br /> <br /> As an extension Jasymca offers the command format Base Number, which is used to display numbers in a system with arbitrary Base with any Number of significant digits. To display numbers with \$15\$ digits in the binary system we type:<br /> <br /> &lt;jcode lang=&quot;matlab&quot;&gt;<br /> &gt;&gt; format 2 15<br /> &gt;&gt; -1.723478265342e12<br /> ans = -1.1001000101001E40<br /> &lt;/jcode&gt;<br /> Using format short returns the display mode to default (short decimal). It should be emphasized, that none of the format commands influences the internal representation and accuracy of floating point numbers.<br /> <br /> Numbers, which are entered without decimal point and exponent, and which are larger than &lt;math&gt;10^{15}&lt;/math&gt; are stored as exact rational datatype. These numbers are internally represented as quotient of two variable length integers (java datatype BigInteger), which allows you to perform calculations without any rounding errors. In the first case of the following example a floating point number is generated, in the second case an exact rational:<br /> <br /> &lt;jcode lang=&quot;matlab&quot;&gt;<br /> &gt;&gt; 10000000000000001.<br /> ans = 1.0E16<br /> &gt;&gt; 10000000000000001<br /> ans = 10000000000000001<br /> &lt;/jcode&gt;<br /> <br /> Each floating point number Z can be converted to an exact number using the command rat(Z). The conversion is accomplished by continued fraction expansion with an accuracy determined by the variable ratepsilon (default: &lt;math&gt;10^{-8}&lt;/math&gt;).<br /> <br /> &lt;jcode lang=&quot;matlab&quot;&gt;<br /> &gt;&gt; rat(0.33333333333333333)<br /> ans = 1/3<br /> &lt;/jcode&gt;<br /> <br /> Operations between exact and floating point numbers always lead to the promotion of floating point numbers. Calculations can be performed without rounding errors by rationalizing just the first number.<br /> <br /> &lt;jcode lang=&quot;matlab&quot;&gt;<br /> &gt;&gt; 1/21/525/21/5*7*175*63*15-1<br /> ans = -4.4409E-16<br /> &gt;&gt; rat(1)/21/525/21/5*7*175*63*15-1<br /> ans = 0<br /> &lt;/jcode&gt;<br /> <br /> Conversely, the command float(Z) converts numbers into floating point format. Both commands also work for composite datatypes, like polynomials and matrices, whose coefficients are transformed in one step. Irrational function values of exact numbers and constants like pi remain unevaluated until the float-command is issued.<br /> <br /> &lt;jcode lang=&quot;matlab&quot;&gt;<br /> &gt;&gt; sqrt(2)<br /> ans = 1.4142<br /> &gt;&gt; sqrt(rat(2))<br /> ans = sqrt(2)<br /> &gt;&gt; float(ans)<br /> ans = 1.4142<br /> &lt;/jcode&gt;<br /> <br /> The exact datatype is useful especially for unstable problems, like solving systems of linear equations with ill-conditioned matrix. The Hilbert-matrix is an extreme example:<br /> <br /> &lt;jcode lang=&quot;matlab&quot;&gt;<br /> &gt;&gt; det( hilb(20)*invhilb(20) )<br /> ans = 1 % correct<br /> &gt;&gt; det( float(hilb(20))*float(invhilb(20)) )<br /> ans = 1.6713E151 % slightly wrong<br /> &lt;/jcode&gt;<br /> <br /> Imaginary numbers are marked with an immediately following i or j. This will work even if the predefined variables i and j have been overwritten.<br /> &lt;jcode lang=&quot;matlab&quot;&gt;<br /> &gt;&gt; 2+3i<br /> ans = 2+3i<br /> &lt;/jcode&gt;<br /> <br /> How to print out the output values if you are working with the script, rather than with the prompt. Just use &quot;printf&quot; method:<br /> &lt;jcode lang=&quot;matlab&quot;&gt;<br /> x=log(sqrt(854)); % natural logarithm<br /> printf('Answer=%f\n', x);<br /> &lt;/jcode&gt;</div> imported>Jworkorg