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

17 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-04-29 06:41 +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 See :ref:`constant_float` for details. 

17 

18 .. note:: 

19 

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

21 if a fractional value is provided. 

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

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

24 """ 

25 

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

27 """ 

28 Arguments: 

29 name: The name of the constant. 

30 value: The constant value. 

31 description: Textual description for the constant. 

32 """ 

33 self.name = name 

34 self.description = description 

35 

36 self._value = 0.0 

37 # Assign self._value via setter 

38 self.value = value 

39 

40 @property 

41 def value(self) -> float: 

42 """ 

43 Getter for value. 

44 """ 

45 return self._value 

46 

47 @value.setter 

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

49 """ 

50 Setter for value that performs sanity checks. 

51 """ 

52 if not isinstance(value, float): 

53 raise TypeError( 

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

55 ) 

56 

57 self._value = value 

58 

59 def __repr__(self) -> str: 

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

61name={self.name},\ 

62value={self.value},\ 

63description={self.description},\ 

64)"""