English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Erlang Numbers

In Erlang, number literals have2There are two types, integers and floating-point numbers. Here are some examples to illustrate how to use integers and floating-point numbers in Erlang.

Integer− The following program shows an example of using the number data type as an integer. This program shows2Addition of integers.

Instance

-module(helloworld). 
-export([start/0]). 
start() -> 
   io:fwrite("~w",[1+1]).

The output of the above program is as follows:

2

Floating-point number − The following program shows an example of using the number data type as a floating-point number. The program shows2Addition of integers.

-module(helloworld). 
-export([start/0]). 
start() -> 
   io:fwrite("~f~n",[1.1+1.2]), 
   io:fwrite("~e~n",[1.1+1.2]).

The output of the above program is as follows:

2.300000
2.30000e+0

For the above program, the following key points should be noted-

  • When the -f option is specified, the parameter is a floating-point number, written as[-ddd.dddAmong them, precision refers to the number of decimal places. The default precision is6.

  • Specify the ~e option when the parameter is a floating-point number, written as[-]d.ddde+-dddwhere precision is the number of digits to be written. The default precision is6.

Mathematical Functions of Numbers

The following mathematical functions can be used with numbers in Erlang. Please note that all Erlang mathematical functions exist in the math library. Therefore, all the following examples will use the import statement to import all methods from the math library.

Serial NumberMathematical Functions and Descriptions
1

sin

This method returns the sine of the specified value.

2

cos

This method returns the cosine of the specified value.

3

tan

This method returns the tangent of the specified value.

4

asin

This method returns the arcsine of the specified value.

5

acos

This method returns the arccosine of the specified value.

6

atan

This method returns the arctangent of the specified value.

7exp

This method returns the exponent of the specified value.

8

log

This method returns the logarithm of the specified value.

9

abs

This method returns the absolute value of the specified number.

10

float

This method converts the number to a floating-point value.

11

Is_float

This method checks if the number is a floating-point value.

12

Is_integer

This method checks if the number is an integer value.