Erlang数字类型 #
一、整数 #
1.1 十进制整数 #
erlang
-module(decimal).
-export([demo/0]).
demo() ->
Positive = 42,
Negative = -42,
Zero = 0,
io:format("Positive: ~p~n", [Positive]),
io:format("Negative: ~p~n", [Negative]),
io:format("Zero: ~p~n", [Zero]),
ok.
1.2 不同进制 #
Erlang支持2到36进制:
erlang
-module(bases).
-export([demo/0]).
demo() ->
Binary = 2#1010,
Octal = 8#77,
Hex = 16#FF,
Base36 = 36#ZZ,
io:format("Binary (2#1010): ~p~n", [Binary]),
io:format("Octal (8#77): ~p~n", [Octal]),
io:format("Hex (16#FF): ~p~n", [Hex]),
io:format("Base36 (36#ZZ): ~p~n", [Base36]),
ok.
进制格式:Base#Digits
erlang
1> 2#1010.
10
2> 8#77.
63
3> 16#FF.
255
4> 16#ff.
255
5> 36#ZZ.
1295
1.3 任意精度 #
Erlang整数没有大小限制:
erlang
-module(big_integers).
-export([demo/0]).
demo() ->
Big = 123456789012345678901234567890,
Bigger = Big * Big,
io:format("Big: ~p~n", [Big]),
io:format("Bigger: ~p~n", [Bigger]),
ok.
1.4 整数运算 #
erlang
-module(int_ops).
-export([demo/0]).
demo() ->
Add = 10 + 5,
Sub = 10 - 5,
Mul = 10 * 5,
Div = 10 div 3,
Rem = 10 rem 3,
Neg = -10,
io:format("10 + 5 = ~p~n", [Add]),
io:format("10 - 5 = ~p~n", [Sub]),
io:format("10 * 5 = ~p~n", [Mul]),
io:format("10 div 3 = ~p~n", [Div]),
io:format("10 rem 3 = ~p~n", [Rem]),
io:format("-10 = ~p~n", [Neg]),
ok.
| 运算符 | 说明 | 示例 |
|---|---|---|
+ |
加法 | 5 + 3 → 8 |
- |
减法 | 5 - 3 → 2 |
* |
乘法 | 5 * 3 → 15 |
div |
整数除法 | 10 div 3 → 3 |
rem |
取余 | 10 rem 3 → 1 |
1.5 位运算 #
erlang
-module(bit_ops).
-export([demo/0]).
demo() ->
A = 16#0F,
B = 16#F0,
And = A band B,
Or = A bor B,
Xor = A bxor B,
NotA = bnot A,
LeftShift = A bsl 4,
RightShift = B bsr 4,
io:format("~p band ~p = ~p~n", [A, B, And]),
io:format("~p bor ~p = ~p~n", [A, B, Or]),
io:format("~p bxor ~p = ~p~n", [A, B, Xor]),
io:format("bnot ~p = ~p~n", [A, NotA]),
io:format("~p bsl 4 = ~p~n", [A, LeftShift]),
io:format("~p bsr 4 = ~p~n", [B, RightShift]),
ok.
| 运算符 | 说明 | 示例 |
|---|---|---|
band |
位与 | 15 band 3 → 3 |
bor |
位或 | 8 bor 4 → 12 |
bxor |
位异或 | 15 bxor 3 → 12 |
bnot |
位非 | bnot 15 → -16 |
bsl |
左移 | 1 bsl 4 → 16 |
bsr |
右移 | 16 bsr 4 → 1 |
二、浮点数 #
2.1 浮点数表示 #
erlang
-module(float_repr).
-export([demo/0]).
demo() ->
Normal = 3.14159,
Scientific = 1.0e10,
Negative = -1.0e-10,
io:format("Normal: ~p~n", [Normal]),
io:format("Scientific: ~p~n", [Scientific]),
io:format("Negative: ~p~n", [Negative]),
ok.
2.2 浮点运算 #
erlang
-module(float_ops).
-export([demo/0]).
demo() ->
Add = 3.14 + 2.86,
Sub = 5.5 - 1.5,
Mul = 2.5 * 4.0,
Div = 10.0 / 3.0,
io:format("3.14 + 2.86 = ~p~n", [Add]),
io:format("5.5 - 1.5 = ~p~n", [Sub]),
io:format("2.5 * 4.0 = ~p~n", [Mul]),
io:format("10.0 / 3.0 = ~p~n", [Div]),
ok.
注意:/ 总是返回浮点数,即使结果是整数:
erlang
1> 10 / 2.
5.0
2> 10 div 2.
5
2.3 浮点精度 #
erlang
-module(float_precision).
-export([demo/0]).
demo() ->
A = 0.1 + 0.2,
B = 0.3,
io:format("0.1 + 0.2 = ~p~n", [A]),
io:format("0.3 = ~p~n", [B]),
io:format("Equal? ~p~n", [A == B]),
io:format("A - B = ~p~n", [A - B]),
ok.
输出:
text
0.1 + 0.2 = 0.30000000000000004
0.3 = 0.3
Equal? false
A - B = 5.551115123125783e-17
2.4 特殊值 #
erlang
-module(special_floats).
-export([demo/0]).
demo() ->
PosInf = 1.0 / 0.0,
NegInf = -1.0 / 0.0,
NaN = 0.0 / 0.0,
io:format("Positive Infinity: ~p~n", [PosInf]),
io:format("Negative Infinity: ~p~n", [NegInf]),
io:format("NaN: ~p~n", [NaN]),
ok.
三、数学函数 #
3.1 基本数学函数 #
erlang
-module(math_basic).
-export([demo/0]).
demo() ->
Abs = abs(-42),
Min = min(10, 20),
Max = max(10, 20),
io:format("abs(-42) = ~p~n", [Abs]),
io:format("min(10, 20) = ~p~n", [Min]),
io:format("max(10, 20) = ~p~n", [Max]),
ok.
3.2 math模块函数 #
erlang
-module(math_module).
-export([demo/0]).
demo() ->
Sqrt = math:sqrt(16),
Pow = math:pow(2, 10),
Exp = math:exp(1),
Log = math:log(math:e()),
Log10 = math:log10(100),
Pi = math:pi(),
io:format("sqrt(16) = ~p~n", [Sqrt]),
io:format("pow(2, 10) = ~p~n", [Pow]),
io:format("exp(1) = ~p~n", [Exp]),
io:format("log(e) = ~p~n", [Log]),
io:format("log10(100) = ~p~n", [Log10]),
io:format("pi = ~p~n", [Pi]),
ok.
3.3 三角函数 #
erlang
-module(trig_funcs).
-export([demo/0]).
demo() ->
Pi = math:pi(),
Sin = math:sin(Pi / 2),
Cos = math:cos(0),
Tan = math:tan(Pi / 4),
Asin = math:asin(1),
Acos = math:acos(0),
Atan = math:atan(1),
io:format("sin(pi/2) = ~p~n", [Sin]),
io:format("cos(0) = ~p~n", [Cos]),
io:format("tan(pi/4) = ~p~n", [Tan]),
io:format("asin(1) = ~p~n", [Asin]),
io:format("acos(0) = ~p~n", [Acos]),
io:format("atan(1) = ~p~n", [Atan]),
ok.
3.4 双曲函数 #
erlang
-module(hyperbolic).
-export([demo/0]).
demo() ->
Sinh = math:sinh(1),
Cosh = math:cosh(1),
Tanh = math:tanh(1),
io:format("sinh(1) = ~p~n", [Sinh]),
io:format("cosh(1) = ~p~n", [Cosh]),
io:format("tanh(1) = ~p~n", [Tanh]),
ok.
3.5 取整函数 #
erlang
-module(rounding).
-export([demo/0]).
demo() ->
Float = 3.7,
Round = round(Float),
Trunc = trunc(Float),
Ceil = ceil(Float),
Floor = floor(Float),
io:format("round(~p) = ~p~n", [Float, Round]),
io:format("trunc(~p) = ~p~n", [Float, Trunc]),
io:format("ceil(~p) = ~p~n", [Float, Ceil]),
io:format("floor(~p) = ~p~n", [Float, Floor]),
NegFloat = -3.7,
io:format("~nround(~p) = ~p~n", [NegFloat, round(NegFloat)]),
io:format("trunc(~p) = ~p~n", [NegFloat, trunc(NegFloat)]),
io:format("ceil(~p) = ~p~n", [NegFloat, ceil(NegFloat)]),
io:format("floor(~p) = ~p~n", [NegFloat, floor(NegFloat)]),
ok.
| 函数 | 说明 | 示例 |
|---|---|---|
round/1 |
四舍五入 | round(3.7) → 4 |
trunc/1 |
向零取整 | trunc(3.7) → 3 |
ceil/1 |
向上取整 | ceil(3.1) → 4 |
floor/1 |
向下取整 | floor(3.9) → 3 |
四、类型转换 #
4.1 整数与浮点数 #
erlang
-module(num_conversion).
-export([demo/0]).
demo() ->
Int = 42,
Float = float(Int),
io:format("float(~p) = ~p~n", [Int, Float]),
F = 3.7,
Round = round(F),
Trunc = trunc(F),
io:format("round(~p) = ~p~n", [F, Round]),
io:format("trunc(~p) = ~p~n", [F, Trunc]),
ok.
4.2 数字与字符串 #
erlang
-module(str_conversion).
-export([demo/0]).
demo() ->
Int = 42,
IntList = integer_to_list(Int),
IntBinary = integer_to_binary(Int),
io:format("integer_to_list(~p) = ~p~n", [Int, IntList]),
io:format("integer_to_binary(~p) = ~p~n", [Int, IntBinary]),
Float = 3.14159,
FloatList = float_to_list(Float),
FloatBinary = float_to_binary(Float),
io:format("float_to_list(~p) = ~p~n", [Float, FloatList]),
io:format("float_to_binary(~p) = ~p~n", [Float, FloatBinary]),
ListInt = list_to_integer("123"),
ListFloat = list_to_float("3.14"),
io:format("list_to_integer(\"123\") = ~p~n", [ListInt]),
io:format("list_to_float(\"3.14\") = ~p~n", [ListFloat]),
ok.
4.3 格式化输出 #
erlang
-module(format_nums).
-export([demo/0]).
demo() ->
Pi = math:pi(),
io:format("Default: ~p~n", [Pi]),
io:format("Float: ~f~n", [Pi]),
io:format("Precision: ~.4f~n", [Pi]),
io:format("Scientific: ~e~n", [Pi]),
io:format("Width: ~10.4f~n", [Pi]),
ok.
五、随机数 #
5.1 rand模块 #
erlang
-module(random_nums).
-export([demo/0]).
demo() ->
rand:seed_default(),
Uniform = rand:uniform(),
UniformN = rand:uniform(100),
io:format("Uniform [0,1): ~p~n", [Uniform]),
io:format("Uniform [1,100]: ~p~n", [UniformN]),
Range = random_range(10, 20),
io:format("Range [10,20]: ~p~n", [Range]),
ok.
random_range(Min, Max) ->
Min - 1 + rand:uniform(Max - Min + 1).
5.2 随机列表 #
erlang
-module(random_list).
-export([random_list/1]).
random_list(0) -> [];
random_list(N) when N > 0 ->
[rand:uniform(100) | random_list(N - 1)].
5.3 随机种子 #
erlang
-module(random_seed).
-export([demo/0]).
demo() ->
rand:seed_default(),
A = rand:uniform(),
rand:seed_default(),
B = rand:uniform(),
io:format("A: ~p~n", [A]),
io:format("B: ~p~n", [B]),
io:format("Same? ~p~n", [A == B]),
ok.
六、进制转换 #
6.1 整数到不同进制 #
erlang
-module(base_convert).
-export([int_to_base/2, demo/0]).
int_to_base(N, Base) when Base >= 2, Base =< 36 ->
Digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ",
int_to_base(N, Base, Digits, []).
int_to_base(0, _Base, _Digits, Acc) -> Acc;
int_to_base(N, Base, Digits, Acc) ->
Digit = lists:nth(N rem Base + 1, Digits),
int_to_base(N div Base, Base, Digits, [Digit | Acc]).
demo() ->
Num = 255,
io:format("~p in binary: ~s~n", [Num, int_to_base(Num, 2)]),
io:format("~p in octal: ~s~n", [Num, int_to_base(Num, 8)]),
io:format("~p in hex: ~s~n", [Num, int_to_base(Num, 16)]),
ok.
6.2 使用io_lib格式化 #
erlang
1> io_lib:format("~.2B", [255]).
["11111111"]
2> io_lib:format("~.8B", [255]).
["377"]
3> io_lib:format("~.16B", [255]).
["FF"]
七、实用函数 #
7.1 判断奇偶 #
erlang
-module(odd_even).
-export([is_odd/1, is_even/1]).
is_odd(N) -> N rem 2 =:= 1.
is_even(N) -> N rem 2 =:= 0.
7.2 阶乘 #
erlang
-module(factorial).
-export([factorial/1]).
factorial(0) -> 1;
factorial(N) when N > 0 -> N * factorial(N - 1).
7.3 斐波那契 #
erlang
-module(fibonacci).
-export([fib/1]).
fib(0) -> 0;
fib(1) -> 1;
fib(N) when N > 1 -> fib(N - 1) + fib(N - 2).
7.4 最大公约数 #
erlang
-module(gcd).
-export([gcd/2]).
gcd(A, 0) -> A;
gcd(A, B) -> gcd(B, A rem B).
7.5 最小公倍数 #
erlang
-module(lcm).
-export([lcm/2]).
lcm(A, B) -> (A * B) div gcd:gcd(A, B).
八、总结 #
本章学习了:
- 整数的各种进制表示
- 整数运算和位运算
- 浮点数表示和运算
- 数学函数
- 类型转换
- 随机数生成
- 进制转换
- 实用数学函数
准备好学习原子类型了吗?让我们进入下一章。
最后更新:2026-03-27