Skip to content

Commit 9f0fe30

Browse files
committed
Add RoundMode, sat=True
1 parent 3348d2a commit 9f0fe30

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

src/gfloat/block.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from typing import Iterable
88

99
from .decode import decode_float
10-
from .round import encode_float, round_float
10+
from .round import encode_float, round_float, RoundMode
1111
from .types import FormatInfo
1212

1313

@@ -78,7 +78,10 @@ def decode_block(fi: BlockFormatInfo, block: Iterable[int]) -> Iterable[float]:
7878

7979

8080
def encode_block(
81-
fi: BlockFormatInfo, scale: float, vals: Iterable[float]
81+
fi: BlockFormatInfo,
82+
scale: float,
83+
vals: Iterable[float],
84+
round: RoundMode = RoundMode.TiesToEven,
8285
) -> Iterable[int]:
8386
"""
8487
Encode a :paramref:`block` of bytes into block Format descibed by :paramref:`fi`
@@ -93,22 +96,27 @@ def encode_block(
9396
fi (BlockFormatInfo): Describes the target block format
9497
scale (float): Scale to be recorded in the block
9598
vals (Iterable[float]): Input block
99+
round (RoundMode): Rounding mode to use, defaults to `TiesToEven`
96100
97101
Returns:
98102
A sequence of ints representing the encoded values.
99103
100104
Raises:
101105
ValueError: The scale overflows the target scale encoding format.
102106
"""
103-
# TODO: this should not do any multiplication - the scale is to be recorded not applied.
107+
108+
# TODO: this should really not do any multiplication -
109+
# the scale is to be recorded not applied.
104110
recip_scale = 1 / scale
105111
scale = 1 / recip_scale
106112

107113
if scale > fi.stype.max:
108114
raise ValueError(f"Scaled {scale} too large for {fi.stype}")
109115

116+
sat = True # Saturate elements if out of range
117+
110118
def enc(ty: FormatInfo, x: float) -> int:
111-
return encode_float(ty, round_float(ty, x))
119+
return encode_float(ty, round_float(ty, x, round, sat))
112120

113121
yield enc(fi.stype, scale)
114122

0 commit comments

Comments
 (0)