|
16 | 16 |
|
17 | 17 | from .basic import PyccelAstNode, TypedAstNode
|
18 | 18 | from .datatypes import PythonNativeInt, PythonNativeBool, PythonNativeFloat
|
19 |
| -from .datatypes import GenericType, PythonNativeComplex, PrimitiveComplexType |
| 19 | +from .datatypes import GenericType, PythonNativeComplex |
| 20 | +from .datatypes import PrimitiveBooleanType, PrimitiveComplexType |
20 | 21 | from .datatypes import HomogeneousTupleType, InhomogeneousTupleType
|
21 | 22 | from .datatypes import HomogeneousListType, HomogeneousContainerType
|
22 | 23 | from .datatypes import FixedSizeNumericType, HomogeneousSetType, SymbolicType
|
|
53 | 54 | 'PythonPrint',
|
54 | 55 | 'PythonRange',
|
55 | 56 | 'PythonReal',
|
| 57 | + 'PythonRound', |
56 | 58 | 'PythonSet',
|
57 | 59 | 'PythonSetFunction',
|
58 | 60 | 'PythonSum',
|
@@ -480,6 +482,52 @@ def arg(self):
|
480 | 482 | def __str__(self):
|
481 | 483 | return f'float({self.arg})'
|
482 | 484 |
|
| 485 | +# =========================================================================== |
| 486 | +class PythonRound(PyccelFunction): |
| 487 | + """ |
| 488 | + Class representing a call to Python's native round() function. |
| 489 | +
|
| 490 | + Class representing a call to Python's native round() function |
| 491 | + which rounds a float or integer to a given number of decimals. |
| 492 | +
|
| 493 | + Parameters |
| 494 | + ---------- |
| 495 | + number : TypedAstNode |
| 496 | + The number to be rounded. |
| 497 | + ndigits : TypedAstNode, optional |
| 498 | + The number of digits to round to. |
| 499 | + """ |
| 500 | + __slots__ = ('_class_type',) |
| 501 | + name = 'round' |
| 502 | + _rank = 0 |
| 503 | + _shape = None |
| 504 | + _order = None |
| 505 | + |
| 506 | + def __init__(self, number, ndigits = None): |
| 507 | + if ndigits is None or number.class_type.primitive_type is PrimitiveBooleanType(): |
| 508 | + self._class_type = PythonNativeInt() |
| 509 | + else: |
| 510 | + self._class_type = number.class_type |
| 511 | + super().__init__(number, ndigits) |
| 512 | + |
| 513 | + @property |
| 514 | + def arg(self): |
| 515 | + """ |
| 516 | + The number to be rounded. |
| 517 | +
|
| 518 | + The number to be rounded. |
| 519 | + """ |
| 520 | + return self._args[0] |
| 521 | + |
| 522 | + @property |
| 523 | + def ndigits(self): |
| 524 | + """ |
| 525 | + The number of digits to which the argument is rounded. |
| 526 | +
|
| 527 | + The number of digits to which the argument is rounded. |
| 528 | + """ |
| 529 | + return self._args[1] |
| 530 | + |
483 | 531 | #==============================================================================
|
484 | 532 | class PythonInt(PyccelFunction):
|
485 | 533 | """
|
@@ -1664,6 +1712,7 @@ def get_assign_targets(self):
|
1664 | 1712 | 'min' : PythonMin,
|
1665 | 1713 | 'not' : PyccelNot,
|
1666 | 1714 | 'range' : PythonRange,
|
| 1715 | + 'round' : PythonRound, |
1667 | 1716 | 'set' : PythonSetFunction,
|
1668 | 1717 | 'str' : LiteralString,
|
1669 | 1718 | 'sum' : PythonSum,
|
|
0 commit comments