Skip to content

Commit c7f72f2

Browse files
Fix truncation logic (#136)
Pulled out of #135, where I bit off too much.
1 parent c2c9e73 commit c7f72f2

File tree

3 files changed

+6
-15
lines changed

3 files changed

+6
-15
lines changed

src/celpy/celtypes.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@
190190
import logging
191191
import re
192192
from functools import reduce, wraps
193-
from math import fsum
193+
from math import fsum, trunc
194194
from typing import (
195195
Any,
196196
Callable,
@@ -551,7 +551,7 @@ class IntType(int):
551551
ValueError: overflow
552552
553553
>>> IntType(DoubleType(1.9))
554-
IntType(2)
554+
IntType(1)
555555
>>> IntType(DoubleType(-123.456))
556556
IntType(-123)
557557
"""
@@ -568,7 +568,7 @@ def __new__(
568568
# Used by protobuf.
569569
return super().__new__(cls, cast(int, source.get(StringType("value"))))
570570
elif isinstance(source, (float, DoubleType)):
571-
convert = int64(round)
571+
convert = int64(trunc)
572572
elif isinstance(source, TimestampType):
573573
convert = int64(lambda src: src.timestamp())
574574
elif isinstance(source, (str, StringType)) and source[:2] in {"0x", "0X"}:
@@ -735,7 +735,7 @@ def __new__(
735735
if isinstance(source, UintType):
736736
return source
737737
elif isinstance(source, (float, DoubleType)):
738-
convert = uint64(round)
738+
convert = uint64(trunc)
739739
elif isinstance(source, TimestampType):
740740
convert = uint64(lambda src: src.timestamp())
741741
elif isinstance(source, (str, StringType)) and source[:2] in {"0x", "0X"}:

tests/test_celtypes.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ def test_double_type():
111111
def test_int_type():
112112
i_42 = IntType(42)
113113
i_max = IntType(9223372036854775807)
114-
assert IntType(DoubleType(1.9)) == IntType(2)
114+
assert IntType(DoubleType(1.9)) == IntType(1)
115115
assert IntType(DoubleType(-123.456)) == IntType(-123)
116116
assert IntType(TimestampType("2009-02-13T23:31:30Z")) == 1234567890
117117
assert IntType("0x2a") == 42
@@ -160,7 +160,7 @@ def test_int_type():
160160
def test_uint_type():
161161
u_42 = UintType(42)
162162
u_max = UintType(18446744073709551615)
163-
assert UintType(DoubleType(1.9)) == UintType(2)
163+
assert UintType(DoubleType(1.9)) == UintType(1)
164164
with pytest.raises(ValueError):
165165
assert UintType(DoubleType(-123.456)) == UintType(-123)
166166
assert UintType(TimestampType("2009-02-13T23:31:30Z")) == 1234567890

tools/wip.toml

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -227,16 +227,7 @@ not_ne_uint_double_nan = "@wip"
227227
timestamp = "@wip"
228228

229229
[conversions.int]
230-
double_half_neg = "@wip"
231-
double_half_pos = "@wip"
232230
double_int_min_range = "@wip"
233-
double_truncate = "@wip"
234-
double_truncate_neg = "@wip"
235-
236-
[conversions.uint]
237-
double_half = "@wip"
238-
double_truncate = "@wip"
239-
240231
[dynamic.any]
241232
literal = "@wip"
242233

0 commit comments

Comments
 (0)