From 53fd9b13d0ffb9f992b571ab5d6ddda58f7b9af3 Mon Sep 17 00:00:00 2001 From: Arun Isaac Date: Thu, 19 Jan 2023 18:05:46 +0000 Subject: search: Use decimals, not floats, to manipulate range limits. Decimals are exact and do not introduce tiny errors in the range limits like floats are wont to do. * gn3/api/search.py: Import Decimal from decimal. (apply_si_suffix): Use decimals, not floats, to manipulate range limits. --- gn3/api/search.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'gn3/api/search.py') diff --git a/gn3/api/search.py b/gn3/api/search.py index 2c11d4c..820b74b 100644 --- a/gn3/api/search.py +++ b/gn3/api/search.py @@ -1,6 +1,7 @@ """Search using Xapian index.""" from collections import namedtuple +from decimal import Decimal import gzip import json from functools import partial, reduce @@ -151,7 +152,7 @@ def apply_si_suffix(location: str) -> int: """Apply SI suffixes kilo, mega, giga and convert to bases.""" suffixes = {"k": 3, "m": 6, "g": 9} if location[-1] in suffixes: - return int(float(location[:-1])*10**suffixes[location[-1].lower()]) + return int(Decimal(location[:-1])*10**suffixes[location[-1].lower()]) else: return int(location) -- cgit v1.2.3