Float32 scalars, arrays and Tensor bridges
Choose scalar rounding explicitly, compare packed array precision and preserve Float32 or Float64 across detached Tensor copies.
import aner.nn
// Literal inference remains Int64 / Float64.
let default_integer = 42
let default_float = 0.5
let exact: Float32 = 0.5
let rounded = Float32(0.1)
let wider: Float64 = rounded
print(type(default_integer)) // Int64
print(type(default_float)) // Float64
print(type(exact)) // Float32
print(type(wider)) // Float64
print(Float64(rounded) == 0.1) // false
print(type(Float32(1.0) + Float32(2.0))) // Float32
print(type(Float32(1.0) + 2.0)) // Float64
print(type(Float32(5.0) / Float32(2.0))) // Float64
var numeric: Number = rounded
print(type(numeric)) // Float32
numeric = UInt8(12)
print(type(numeric)) // UInt8
print(Int16(Float32(3.9))) // 3
let samples = Array<Float32>(size: 3, fill: 0.5)
samples[1] = Float32(0.1)
print(dtype(samples)) // Float32
print(type(samples.sum())) // Float32
print(type(samples.mean())) // Float64
let precise = Array<Float64>(size: 3, fill: 0.1)
let compact = precise.astype("Float32")
let widened = compact.astype("Float64")
print(dtype(compact)) // Float32
print(dtype(widened)) // Float64
print(dtype(compact.to_float())) // Float64
let matrix = NDArray<Float32>(shape: List(2, 2), fill: 1.5)
let packed_tensor = matrix.to_tensor()
print(packed_tensor.dtype()) // Float32
let flat = Array.from_tensor<Float32>(packed_tensor)
let growable = DynamicArray.from_tensor<Float32>(packed_tensor)
let shaped = NDArray.from_tensor<Float32>(packed_tensor)
print(dtype(flat)) // Float32
print(growable.len()) // 4
print(shaped.shape()) // List(2, 2)
let wide = Array.from_tensor<Float64>(packed_tensor.astype("Float64"))
print(dtype(wide)) // Float64
flat.set(0, Float32(9.0))
print(packed_tensor.value(0, 0)) // 1.5: reverse bridge owns independent storage
// Checked failures, intentionally disabled:
// let inexact: Float32 = 0.1 // R1003: implicit precision loss
// samples.set(0, 0.1) // R1003: explicit Float32(...) rounding required
// let overflow = Array<Float32>(2, Float32(3e38)).scale(Float32(2.0)) // R3001: nonfinite result
// let mismatched = Array.from_tensor<Float64>(packed_tensor) // R3001: exact dtype match required
Make it your experiment.
With a compatible development runtime installed, save this program as examples/float32.aner inside a folder for your experiment. Open a terminal in that folder, then check and run the program.
aner check examples/float32.aner
aner run examples/float32.anerThe aner command must be on your PATH. Follow the installation guide if your terminal cannot find it.
Requires a compatible current native Aner development runtime with scalar Float32, packed Float32 arrays and dtype preserving Tensor bridges. These numeric additions await a matching published binary; the existing research preview may not recognize them. Use matching local VS Code integration for editor help. Development runtimes and extension packages are not offered by this website. The complete example needs no external data, network connection or credentials.
Bundled datasets retain their own source attribution and license notices. Example outcomes are not comparative benchmarks or evidence of clinical validity.
Dataset sources & attribution