JMathLabTutorial:3 Arithmetic

From jWork.ORG
Jump to: navigation, search
Limitted access. Reguest membership or login to this link first if you are already a member


First, we will learn how to print a variable which keeps a number. In our example, the variable "x" is assigned to 1000

x=1000; printf('%f',x)

Click [Output] to see what you are just printed on the screen. 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]-1.723478265342 10^{12}[/math] should be entered like

The basic arithmetic operations are marked with the usual symbols (+ - * / ) . Exponention is performed with the accent character (^). Multiplication and division precede addition and subtraction; any order of evaluation can be forced by parenthesis.

+ addition
- subtraction
* multiplication
/ division
\ left division
^ power
' conjugate transpose

a=3.23*(14-2^5)/(15-(3^3-2^3)); printf('%f\n',a)
a=4.5e-23/0.0000013; printf('%f\n',a)
a=17.4^((3-2.13^1.2)^0.16); printf('%f\n',a)
a=17.23e4/(1.12-17.23e4/(1.12-17.23e4/1.12));  printf('%f\n',a)


The relational operators

< less than
> greater than
<= less than or equal
>= greater than or equal
== equal
~= not equal.

Note that "=" is used in an assignment statement while "==" is used in a relation. Relations may be connected or quantified by the logical operators

& and
| or
~ not.

When applied to scalars, a relation is actually the scalar 1 or 0 depending on whether the relation is true or false.

Logical true is the number 1, false is 0.


A=1;B=1;C=1;   % semicolon suppresses output.
a= !(A&B)|(B&C) == (C~=A)

Also, eps is defined:

a=eps;            printf('%f\n',a)
a=1+eps>1;        printf('%f\n',a)
a=1+eps/2>1;      printf('%f\n',a)

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
x=-1.723478265342e12; printf('%f',x)

As an extension it 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
x=-1.723478265342e12; printf('%f',x)

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 <m>10^{15}</m> are stored as exact rational data type. These numbers are internally represented as quotient of two variable length integers (java data type 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:


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: $10^{-8}$).

x=rat(0.33333333333333333); printf('%f',x)

The rat() function is a part of symbolic calculation; in such cases we will need to specify "syms". You can comment it out, but the code should contain the keyword to let the engine know that we are interested in symbolic evaluation.

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.

x=1/21/525/21/5*7*175*63*15-1;  printf('%f',x)

x=rat(1)/21/525/21/5*7*175*63*15-1; printf('%f',x)

Conversely, the command float(Z) converts numbers into floating point format. Both commands also work for composite data types, like polynomials and matrices, whose coefficients are transformed in one step. Irrational function values of exact numbers and constants like pi remain non-evaluated until the float-command is issued.

x=sqrt(2);      printf('%f\n',x)
x=sqrt(rat(2)); printf('%f\n',x)
x=float(10);    printf('%f\n',x)

You can also use comments in the output:

x=log(sqrt(854));   printf('Answer=%f', x);

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.

printf('number=%f', x);