30 lines
1.0 KiB
Go
30 lines
1.0 KiB
Go
// Copyright (c) 2026 Petr Balvín <opensource@petrbalvin.org> (https://petrbalvin.org)
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package core
|
|
|
|
import "testing"
|
|
|
|
// TestCumProdHalfNarrowsCarryPerStep pins the float16 cumulative
|
|
// product's carry discipline: every step narrows the running product to
|
|
// half before combining, exactly as the stored element narrows, so each
|
|
// step multiplies the rounded carry. The input 67, 65, 6 makes the
|
|
// discipline visible: 67 times 65 is 4355 in float64, which narrows to
|
|
// 4356, and 4356 times 6 rounds to 26144; a carry left in float64 would
|
|
// give 4355 times 6 = 26130, which rounds to 26128.
|
|
func TestCumProdHalfNarrowsCarryPerStep(t *testing.T) {
|
|
a := mustFromHalves(t, []uint16{
|
|
HalfFromFloat64(67), HalfFromFloat64(65), HalfFromFloat64(6),
|
|
}, 3)
|
|
out, err := CumProd(a, 0)
|
|
if err != nil {
|
|
t.Fatalf("CumProd: %v", err)
|
|
}
|
|
want := []float64{67, 4356, 26144}
|
|
for i, w := range want {
|
|
if got := HalfToFloat64(out.RawHalves()[i]); got != w {
|
|
t.Fatalf("element %d = %v, want %v", i, got, w)
|
|
}
|
|
}
|
|
}
|