Coverage for hdl_registers/constant/float_constant.py: 100%
18 statements
« prev ^ index » next coverage.py v7.6.9, created at 2024-12-19 20:51 +0000
« prev ^ index » next coverage.py v7.6.9, created at 2024-12-19 20:51 +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# --------------------------------------------------------------------------------------------------
10# Standard libraries
11from typing import Optional
13# Local folder libraries
14from .constant import Constant
17class FloatConstant(Constant):
18 """
19 Represent a floating-point constant.
21 .. note::
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 """
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
39 self._value = 0.0
40 # Assign self._value via setter
41 self.value = value
43 @property
44 def value(self) -> float:
45 """
46 Getter for value.
47 """
48 return self._value
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 )
60 self._value = value
62 def __repr__(self) -> str:
63 return f"""{self.__class__.__name__}(\
64name={self.name},\
65value={self.value},\
66description={self.description},\
67)"""