DMelt:JMathlab/2 Numbers

From HandWiki
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
Member

= Numbers

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 [math]\displaystyle{ -1.723478265342 10^{12} }[/math] should be entered like:

>> 5364
ans = 5364
>> -1.723478265342e12
ans = -1.7235E12

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.

>> format long
>> -1.723478265342e12
ans = -1.723478265342E12

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:

>> format 2 15
>> -1.723478265342e12
ans = -1.1001000101001E40

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.

Numbers, which are entered without decimal point and exponent, and which are larger than [math]\displaystyle{ 10^{15} }[/math] 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:

>> 10000000000000001.
ans = 1.0E16
>> 10000000000000001
ans = 10000000000000001

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: [math]\displaystyle{ 10^{-8} }[/math]).

>> rat(0.33333333333333333)
ans = 1/3

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.

>> 1/21/525/21/5*7*175*63*15-1
ans = -4.4409E-16
>> rat(1)/21/525/21/5*7*175*63*15-1
ans = 0

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.

>> sqrt(2)
ans = 1.4142
>> sqrt(rat(2))
ans = sqrt(2)
>> float(ans)
ans = 1.4142

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:

>> det( hilb(20)*invhilb(20) )
ans = 1           % correct
>> det( float(hilb(20))*float(invhilb(20)) )
ans = 1.6713E151  % slightly wrong

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.

>> 2+3i
ans = 2+3i

How to print out the output values if you are working with the script, rather than with the prompt. Just use "printf" method:

x=log(sqrt(854));        % natural logarithm
printf('Answer=%f\n', x);