Coverage for trlc/math.py: 89%

14 statements  

« prev     ^ index     » next       coverage.py v7.7.1, created at 2025-03-27 00:52 +0000

1#!/usr/bin/env python3 

2# 

3# TRLC - Treat Requirements Like Code 

4# Copyright (C) 2022 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) 

5# 

6# This file is part of the TRLC Python Reference Implementation. 

7# 

8# TRLC is free software: you can redistribute it and/or modify it 

9# under the terms of the GNU General Public License as published by 

10# the Free Software Foundation, either version 3 of the License, or 

11# (at your option) any later version. 

12# 

13# TRLC is distributed in the hope that it will be useful, but WITHOUT 

14# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 

15# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public 

16# License for more details. 

17# 

18# You should have received a copy of the GNU General Public License 

19# along with TRLC. If not, see <https://www.gnu.org/licenses/>. 

20 

21from fractions import Fraction 

22from math import floor, ceil 

23 

24 

25def remainder(lhs, rhs): 

26 assert isinstance(lhs, int) 

27 assert isinstance(rhs, int) 

28 

29 mod = abs(lhs) % abs(rhs) 

30 

31 if lhs >= 0: 

32 return mod 

33 else: 

34 return -mod 

35 

36 

37def round_nearest_away(value): 

38 assert isinstance(value, Fraction) 

39 

40 if value >= Fraction(0): 40 ↛ 43line 40 didn't jump to line 43 because the condition on line 40 was always true

41 return floor(value + Fraction(1, 2)) 

42 else: 

43 return ceil(value - Fraction(1, 2))