lettura simple

Mathematical constants in Octave

How do you write pi in Octave? Euler's number? The infinity symbol? In this lesson, I'll show you how.

Some mathematical constants are predefined in Octave.

For example, the constant pi is assigned the value of PI Greek ( π )

It'ss the ratio of a circle's circumference to its diameter, approximately equal to 3.14159.

>> pi
ans = 3.1416

The constant e is the Euler's number ( e )

>> e
ans = 2.7183

The inf constant is the infinity symbol ( ∞ )

>> 1+Inf
ans = Inf

The constants i and j are the imaginary unit of complex numbers

ans = 0+1i

ans = 0+1j

In this case Octave uses two different letters, because in math textbooks the imaginary unit is often denoted by the letter "i". In engineering texts, on the other hand, it is indicated with the letter "j".

Warning. Octave does not prevent the letters e, i, j from being used as session variables. For example, if you type e=2 on the command line, the new value (2) is overwritten with the previous one (2.7183). I personally advise against doing this, because the constant could be called in other parts of the program as Euler's number.
an example of assigning values to constants
So for the whole session you will no longer be able to use the letter "e" to recall Euler's number. If you forget, you could make huge mistakes in the calculation. The same goes for pi and other mathematical constants. Therefore, never use the letters pi, i, j, e, Inf as variables.

Another very useful symbol in mathematical calculations is NaN which means Not A Number

This symbol appears when you try to perform indeterminate mathematical operations

For example, if you compute the ratio zero to zero, Octave outputs NaN

>> 0/0
ans = NaN

Report a mistake or post a question