Coverage for hdl_registers/constant/float_constant.py: 100%

18 statements  

« prev     ^ index     » next       coverage.py v7.6.8, created at 2024-12-01 20:50 +0000

1# -------------------------------------------------------------------------------------------------- 

2# Copyright (c) Lukas Vik. All rights reserved. 

3# 

4# This file is part of the hdl-registers project, an HDL register generator fast enough to run 

5# in real time. 

6# https://hdl-registers.com 

7# https://github.com/hdl-registers/hdl-registers 

8# -------------------------------------------------------------------------------------------------- 

9 

10# Standard libraries 

11from typing import Optional 

12 

13# Local folder libraries 

14from .constant import Constant 

15 

16 

17class FloatConstant(Constant): 

18 """ 

19 Represent a floating-point constant. 

20 

21 .. note:: 

22 

23 The ``value`` is stored with its native representation, which is a Python ``float`` 

24 if a decimal value is provided. 

25 The Python ``float`` type is a double-precision value, so the precision in Python matches 

26 the precision in C/C++/VHDL generators. 

27 """ 

28 

29 def __init__(self, name: str, value: float, description: Optional[str] = None): 

30 """ 

31 Arguments: 

32 name: The name of the constant. 

33 value: The constant value. 

34 description: Textual description for the constant. 

35 """ 

36 self.name = name 

37 self.description = "" if description is None else description 

38 

39 self._value = 0.0 

40 # Assign self._value via setter 

41 self.value = value 

42 

43 @property 

44 def value(self) -> float: 

45 """ 

46 Getter for value. 

47 """ 

48 return self._value 

49 

50 @value.setter 

51 def value(self, value: float) -> None: 

52 """ 

53 Setter for value that performs sanity checks. 

54 """ 

55 if not isinstance(value, float): 

56 raise ValueError( 

57 f'Constant "{self.name}" has invalid data type "{type(value)}". Value: "{value}".' 

58 ) 

59 

60 self._value = value 

61 

62 def __repr__(self) -> str: 

63 return f"""{self.__class__.__name__}(\ 

64name={self.name},\ 

65value={self.value},\ 

66description={self.description},\ 

67)"""