include(joinpath(@__DIR__, "observations.jl"))
probe("number_default", (7,7.0), (typeof(7),typeof(7.0),Sys.WORD_SIZE,Int===Int64,UInt===UInt64),
(Int64,Float64,64,true,true))
for (T, signed, bits) in ((Int8,true,8),(UInt8,false,8),(Int16,true,16),(UInt16,false,16),
(Int32,true,32),(UInt32,false,32),(Int64,true,64),(UInt64,false,64),
(Int128,true,128),(UInt128,false,128))
low = signed ? -(big(2)^(bits-1)) : big(0)
high = big(2)^(signed ? bits-1 : bits)-1
probe("number_type_"*string(T), (T,bits),
(T <: Signed,8*sizeof(T),BigInt(typemin(T)),BigInt(typemax(T))), (signed,bits,low,high))
end
probe("number_type_Bool", Bool, (8*sizeof(Bool),typemin(Bool),typemax(Bool),Int(false),Int(true)),
(8,false,true,0,1))
probe("number_base_literals", "hex, binary, octal, separators and >128-bit hexadecimal",
(typeof(0x7),typeof(0x135),typeof(0b110),typeof(0o123),0o123,0x1a_bc,10_000,0.000_000_005,
typeof(0x100000000000000000000000000000000),0x100000000000000000000000000000000 == big(2)^128),
(UInt8,UInt16,UInt8,UInt8,0x53,0x1abc,10000,5e-9,BigInt,true))
probe("number_wrap", (typemax(Int64),1),
(typemax(Int64)+1,typemin(Int64),outcome(() -> Base.Checked.checked_add(typemax(Int64),1))),
(-9223372036854775808,-9223372036854775808,"OverflowError"))
probe("number_bigint", (10,19),
(10^19,big(10)^19,big"987654321012345678909876543210" == parse(BigInt,"987654321012345678909876543210")),
(-8446744073709551616,big"10000000000000000000",true))
probe("number_division_errors", (7,0,typemin(Int64),-1),
(outcome(() -> div(7,0)),outcome(() -> div(typemin(Int64),-1)),
outcome(() -> rem(7,0)),outcome(() -> mod(7,0))),
("DivideError","DivideError","DivideError","DivideError"))
for (T,bits) in ((Float16,16),(Float32,32),(Float64,64))
probe("number_type_"*string(T), T, (8*sizeof(T),isinf(typemin(T)),isinf(typemax(T)),
signbit(typemin(T)),signbit(typemax(T))), (bits,true,true,true,false))
end
probe("number_float_literals", "decimal, exponent, Float32, Float16, binary exponent",
(7.,.25,-1.25,1e5,2.5E-4,typeof(2.5f-4),Float32(-1.5),
0x1p0,0x1.8p3,0x.4p-1,typeof(0x.4p-1),sizeof(Float16(4)),2*Float16(4)),
(7.0,.25,-1.25,100000.0,.00025,Float32,-1.5f0,1.0,12.0,.125,Float64,2,Float16(8)))
probe("number_signed_zero", (0.0,-0.0),
(0.0 == -0.0,isequal(0.0,-0.0),first(bitstring(0.0)),first(bitstring(-0.0)),1/(-0.0)),
(true,false,'0','1',-Inf))
probe("number_posinf", (Inf16,Inf32,Inf), (map(isinf,(Inf16,Inf32,Inf)),1/0,500+Inf,Inf*Inf,1/Inf),
((true,true,true),Inf,Inf,Inf,0.0))
probe("number_neginf", (-Inf16,-Inf32,-Inf), (map(signbit,(-Inf16,-Inf32,-Inf)),-5/0),
((true,true,true),-Inf))
probe("number_nan", (NaN16,NaN32,NaN),
(map(isnan,(NaN16,NaN32,NaN)),map(isnan,(0/0,Inf-Inf,Inf/Inf,0*Inf)),NaN==NaN,NaN!=NaN,isequal(NaN,NaN)),
((true,true,true),(true,true,true,true),false,true,true))
x = 1.25f0
probe("number_spacing", (Float32,Float64,0.0,1.0,1000.0,x),
(eps(Float32)==2.0^-23,eps(Float64)==2.0^-52,eps()==eps(Float64),eps(1.0)==eps(),
eps(1000.0)>eps(1.0),eps(0.0)==nextfloat(0.0),nextfloat(x)-x,x-prevfloat(x)),
(true,true,true,true,true,true,eps(Float32),eps(Float32)))
before = precision(BigFloat)
at_40 = setprecision(BigFloat,40) do
(precision(BigFloat), round(BigFloat(1)+parse(BigFloat,"0.1"); digits=8) ≈ 1.1)
end
probe("number_bigfloat", (before,40,"0.1",2,66,3),
(before,at_40,precision(BigFloat),BigFloat(2)^66/3*3 ≈ BigFloat(2)^66),
(256,(40,true),256,true))
rounded = [BigFloat(text,RoundNearest;precision=3) for text in ("1.375","1.625","1.6251")]
probe("number_round_ties", ("1.375","1.625","1.6251",3), Float64.(rounded), [1.5,1.5,1.75])
probe("number_zero", (Float32,1.0), (zero(Float32),typeof(zero(Float32)),zero(1.0),typeof(zero(1.0))),
(0.0f0,Float32,0.0,Float64))
probe("number_one", (Int32,BigFloat), (one(Int32),typeof(one(Int32)),one(BigFloat),typeof(one(BigFloat))),
(Int32(1),Int32,BigFloat(1),BigFloat))
n = 3
probe("number_coefficient", n,
(2n^2-3n+1,2(n-1)^2-3(n-1)+1,-2n,2^3n,3/2n,outcome(() -> (n-1)(n+1))),
(10,3,-6,512,0.5,"MethodError"))
probe("number_conversion", (127,128,3.14,127.4,127.6),
(Int8(127),convert(Int8,127),outcome(() -> Int8(128)),outcome(() -> Int8(3.14)),
127%Int8,128%Int8,round(Int8,127.4),outcome(() -> round(Int8,127.6)),Float32(1e100)),
(Int8(127),Int8(127),"InexactError","InexactError",Int8(127),Int8(-128),Int8(127),"InexactError",Inf32))
本例需在 Julia 1.10.13 官方便携版,Windows x64,本机隔离目录 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Julia 1.10.13 官方便携版,Windows x64,本机隔离目录
入口文件:projects/julia-containers-and-numbers/numbers.jl
本地运行步骤快速上手
- 点击上方“保存源码”保存为
numbers.jl,或下载右侧课程工程包。 - 确认本地已准备 Julia 1.10.13 官方便携版,Windows x64,本机隔离目录。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联24章) · 含当前章节或实例 · mbskycom202612a205.zip
课程关联工程包(登记关联3章) · 含当前章节或实例 · mbskycom202612a207.zip