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

17 statements  

« prev     ^ index     » next       coverage.py v7.6.12, created at 2025-03-12 11:11 +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 

10from .constant import Constant 

11 

12 

13class FloatConstant(Constant): 

14 """ 

15 Represent a floating-point constant. 

16 

17 .. note:: 

18 

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

20 if a decimal value is provided. 

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

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

23 """ 

24 

25 def __init__(self, name: str, value: float, description: str = "") -> None: 

26 """ 

27 Arguments: 

28 name: The name of the constant. 

29 value: The constant value. 

30 description: Textual description for the constant. 

31 """ 

32 self.name = name 

33 self.description = description 

34 

35 self._value = 0.0 

36 # Assign self._value via setter 

37 self.value = value 

38 

39 @property 

40 def value(self) -> float: 

41 """ 

42 Getter for value. 

43 """ 

44 return self._value 

45 

46 @value.setter 

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

48 """ 

49 Setter for value that performs sanity checks. 

50 """ 

51 if not isinstance(value, float): 

52 raise TypeError( 

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

54 ) 

55 

56 self._value = value 

57 

58 def __repr__(self) -> str: 

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

60name={self.name},\ 

61value={self.value},\ 

62description={self.description},\ 

63)"""