# Multiplication of Decimal with {float/complex/Fraction}

**URL:** <https://discuss.python.org/t/multiplication-of-decimal-with-float-complex-fraction/16316>\
**Category:** Python Help\
**Created:** [June 7, 2022, 8:33am UTC](https://discuss.python.org/t/multiplication-of-decimal-with-float-complex-fraction/16316 "2022-06-07T08:33:08Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![vainaixr](https://avatars.discourse-cdn.com/v4/letter/v/5f8ce5/32.png) [@vainaixr](https://discuss.python.org/u/vainaixr)\
**Post date:** [June 7, 2022, 8:33am UTC](https://discuss.python.org/t/multiplication-of-decimal-with-float-complex-fraction/16316/1 "2022-06-07T08:33:08Z")

</div>

current scenario -

1. 

```auto
from decimal import Decimal
Decimal('0.5') * 2.1

```

gives,

```auto
TypeError: unsupported operand type(s) for *: 'decimal.Decimal' and 'float'

```

1. 

```auto
Decimal('0.5') * complex(1 + 2j)

```

gives,

```auto
TypeError: unsupported operand type(s) for *: 'decimal.Decimal' and 'complex'

```

1. 

```auto
from fractions import Fraction
Decimal('0.5') * Fraction(1, 2)

```

gives,

```auto
TypeError: unsupported operand type(s) for *: 'decimal.Decimal' and 'Fraction'

```

expected scenario -

1. all three work

note -

1. `Fraction` \* {`Fraction`/`float`/`complex`}, `float` \* {`complex`/`float`}, `complex` \* `complex` are all valid
2. only the `Decimal` type appears to have these invalid multiplications

---

<div class="post-metadata">

**Author:** ![ajoino](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/ajoino/32/6907_2.png) [@ajoino](https://discuss.python.org/u/ajoino)\
**Post date:** [June 7, 2022, 1:06pm UTC](https://discuss.python.org/t/multiplication-of-decimal-with-float-complex-fraction/16316/2 "2022-06-07T13:06:25Z")

</div>

The documentation for the decimal module does nowhere suggest that the `Decimal` type supports arithmetic operations between `Decimal` and the builtin numeric types, see [https://docs.python.org/3/library/decimal.html](https://docs.python.org/3/library/decimal.html).

---

<div class="post-metadata">

**Author:** ![zware](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/zware/32/58_2.png) [@zware](https://discuss.python.org/u/zware)\
**Post date:** [June 7, 2022, 3:24pm UTC](https://discuss.python.org/t/multiplication-of-decimal-with-float-complex-fraction/16316/3 "2022-06-07T15:24:36Z")

</div>

`Decimal` is deliberately inoperable with other non-integer numeric types in many situations due to the high likelihood of surprising results. For example, what would you expect the result of `Decimal('0.1') * 0.1` to be? I would naively expect `Decimal('0.01')`, but if you force things by casting the `float` to `Decimal` (as in `Decimal('0.1') * Decimal(0.1)`, note the lack of quotes in the second term), the result is actually `Decimal('0.01000000000000000055511151231')`. Consider also the difference between `Fraction(1, 3).as_integer_ratio()` and `(Decimal(1) / Decimal(3)).as_integer_ratio()`, and the fact that `Decimal` does not have its own support for the complex plane.

In short, `Decimal` refuses the temptation to guess what you want and instead forces you to deal with the inexact conversion of other types to `Decimal` yourself.

---

<div class="post-metadata">

**Author:** ![steven.daprano](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/steven.daprano/32/1083_2.png) [@steven.daprano](https://discuss.python.org/u/steven.daprano)\
**Post date:** [June 7, 2022, 6:49pm UTC](https://discuss.python.org/t/multiplication-of-decimal-with-float-complex-fraction/16316/4 "2022-06-07T18:49:52Z")

</div>

What would be the result of these examples? Justify your answer.

```auto
Decimal('0.5') * 2.1
Decimal('0.5') * complex(1 + 2j)
Decimal('0.5') * Fraction(1, 2)

```

Its not enough to say that these should “work” unless you can explain what you mean by “work”.

---

<div class="post-metadata">

**Author:** ![vainaixr](https://avatars.discourse-cdn.com/v4/letter/v/5f8ce5/32.png) [@vainaixr](https://discuss.python.org/u/vainaixr)\
**Post date:** [June 7, 2022, 7:16pm UTC](https://discuss.python.org/t/multiplication-of-decimal-with-float-complex-fraction/16316/5 "2022-06-07T19:16:45Z")

</div>

based on my inspection,

```auto
print(Fraction(1, 2) * 2.1)
print(Fraction(1, 2) * complex(1 + 2j))
print(Fraction(1, 2) * Fraction(1, 2))
print(Decimal('0.5') * 2)

```

give,

```auto
1.05
(0.5+1j)
1/4
1.0

```

similarly,

```auto
print(Decimal('0.5') * 2.1)
print(Decimal('0.5') * complex(1 + 2j))
print(Decimal('0.5') * Fraction(1, 2))

```

should give,

```auto
1.05
(0.5+1j)
0.25 # or it could be made 
     # Decimal * Fraction gives 0.25, 
     # Fraction * Decimal gives 1/4

```

if we print the output without using print, then,

```auto
(Fraction(1, 2) * 2.1), (Fraction(1, 2) * complex(1 + 2j)), \
(Fraction(1, 2) * Fraction(1, 2))

```

gives,

```auto
(1.05, (0.5+1j), Fraction(1, 4))

```

and,

```auto
(Decimal('0.5') * 2)

```

gives,

```auto
Decimal('1.0')

```

similarly,

```auto
((Decimal('0.5') * 2.1), Decimal('0.5') * Fraction(1, 2))

```

should give,

```auto
(Decimal('1.05'), Decimal('0.25'))

```

plus,

```auto
Fraction(1, 2) * Decimal('0.5')

```

should give,

```auto
Fraction(1, 4)

```

for complex numbers it appears there is nothing like,

```auto
Decimal('1.2 + 2.1j')

```

so,

```auto
Decimal('0.5') * complex(1 + 2j)

```

is a bit confusing, but one possibility is to output,

```auto
(0.5 + 1j)

```

just like it works for `Fraction`

based on some further inspection, I noted down the types obtained after performing this multiplication for the valid cases,

```auto
arg1 | arg2 | result
-------------------------------
complex | complex | complex
complex | float | complex
complex | Fraction | complex
complex | int | complex
Decimal | Decimal | Decimal
Decimal | int | Decimal
float | float | float
float | Fraction | float
float | int | float
Fraction | Fraction | Fraction
Fraction | int | Fraction
int | int | int

```

just like `float * Fraction` returns an object of type `float`, and `complex * Fraction` returns an object of type `complex`, the same could be true for `{float/complex} * Decimal`
