Skip to content

Commit 72dbfa8

Browse files
miaobyteclaude
andcommitted
docs: add numtypes and precision tutorial examples
Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 0ba2d38 commit 72dbfa8

2 files changed

Lines changed: 109 additions & 0 deletions

File tree

tutorial/01-basics/numtypes.kv

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# numtypes: 全谱数字类型创建/转换算子(fix-021)
2+
# 语义: int8/16/32/64、uint8/16/32/64、float32/float64 十个算子既创建也转换;
3+
# int/float 是 int64/float64 别名。float→int 截断向零;窄化=补码回绕(同 Go/Rust as、C 转换);
4+
# 窄类型进入算术后提升至 int64/float64 运算域(C 整型提升风格),TLV kind 保持声明精度落盘
5+
# 期望输出:
6+
# 3.0
7+
# 0
8+
# 44
9+
# 255
10+
# 4464
11+
# -2147483648
12+
# 4294967295
13+
# 18446744073709551615
14+
# 0.1
15+
# 16777216.0
16+
# 45
17+
# -2
18+
def main() -> () {
19+
f = float32(3) # = 等价于 <-
20+
print(f)
21+
i <- int8(0.1)
22+
print(i)
23+
int8(300) -> w
24+
print(w)
25+
26+
print(uint8(-1))
27+
print(int16(70000))
28+
print(int32(2147483648))
29+
print(uint32(-1))
30+
print(uint64(18446744073709551615))
31+
32+
p32 = float32(0.1) # = 等价于 <-(float32 精度域)
33+
print(p32)
34+
print(float32(16777217))
35+
36+
sum <- w + 1
37+
print(sum)
38+
int16(-2) -> n
39+
print(n)
40+
}
41+
42+
main()

tutorial/01-basics/precision.kv

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# precision: 数字精度与类型提升
2+
# 语义: int 与 float 是语言面两种数字字面量(运行时 XValue 另有 int8~uint64/float32 等 kind)。
3+
# int op int → 原生 int64 运算与比较,绝不经 float64 中转(fix-020,对齐 C/Go/Rust);
4+
# 任一侧 float → float64 提升(混合比较为 C 式 double 提升)
5+
# 期望输出:
6+
# 5
7+
# 5.5
8+
# 3.0
9+
# 3
10+
# -4
11+
# 3.5
12+
# 1
13+
# 3
14+
# 7.0
15+
# 1000.0
16+
# 0.025
17+
# 0.30000000000000004
18+
# true
19+
# 9223372036854775807
20+
# 9223372036854775806
21+
# 9007199254740993
22+
# false
23+
def main() -> () {
24+
a = 2 + 3 # = 等价于 <-
25+
print(a)
26+
b <- 2 + 3.5
27+
print(b)
28+
1.5 * 2 -> c
29+
print(c)
30+
31+
q = 7 / 2 # = 等价于 <-(整除:两侧均 int)
32+
print(q)
33+
-9 / 2 -> qz
34+
print(qz)
35+
fq <- 7.0 / 2
36+
print(fq)
37+
r = 7 % 3 # = 等价于 <-
38+
print(r)
39+
40+
ti <- int(3.99)
41+
print(ti)
42+
float(7) -> tf
43+
print(tf)
44+
45+
sci = 1e3 # = 等价于 <-(科学计数法字面量恒为 float)
46+
print(sci)
47+
small <- 2.5e-2
48+
print(small)
49+
50+
0.1 + 0.2 -> sum
51+
print(sum)
52+
53+
eq = 3 == 3.0 # = 等价于 <-(跨类型数值比较:值提升后相等)
54+
print(eq)
55+
56+
big <- 9223372036854775807
57+
print(big)
58+
59+
big - 1 -> bm
60+
print(bm)
61+
precise = 9007199254740993 + 0 # = 等价于 <-(2^53+1:int 算术不经 float,精度保真)
62+
print(precise)
63+
same <- big == 9223372036854775806
64+
print(same)
65+
}
66+
67+
main()

0 commit comments

Comments
 (0)