<p>My opinion would be that:</p>
<p>Floating point operations are typically resource-consuming and expensive to compute.</p>
<p>I mean, even 1 ADA had to be made a million Lovelace in order for the possibility to have fractional amount of ADA.</p>
<p>I'd suggest you follow the same approach</p>
<ul>
<li>Let the onchain code deal with Integer and carry-out floating point operations off-chain.</li>
</ul>

CC BY-SA 4.0
2021-10-25T11:11:10.437Z
2021-10-25T11:11:10.437Z
Aoaddeola
<p>I used before this for operate with some divisions between Integers and then to truncate to closes integer again.</p>
<p><a href="https://playground.plutus.iohkdev.io/doc/haddock/plutus-tx/html/PlutusTx-Ratio.html#t:Rational" rel="nofollow noreferrer">https://playground.plutus.iohkdev.io/doc/haddock/plutus-tx/html/PlutusTx-Ratio.html#t:Rational</a></p>
<p>Like this:</p>
<pre><code>number1 = 10  
number2 = 3  
result = TxRatio.truncate ( TxRatio.unsafeRatio number1 number2)   
</code></pre>
<p>I think its just too expensive to operate with them on chain, ill not recommend, but you can if you need it.</p>

CC BY-SA 4.0
2023-02-14T16:53:13.417Z
2023-02-14T16:53:13.417Z
Manu Padilla
<p>Floats are well known to be the wrong datatype for encoding anything resembling money, fixed points should be used for such purposes.</p>
<p>Encoding them by <code>Integer</code> (with some hardcoded number of fractional decimals) is prone to errors by mistake, so you better wrap it into custom <code>newtype</code>. That is actually what Plutus <code>Lovelace</code> datatype does.</p>
<p>I am not aware of implementation for full-featured <code>Decimal</code> datatype in Plutus, but they are usually can be avoided in smart-contract logic.</p>

CC BY-SA 4.0
2023-11-03T18:21:01.483Z
2023-11-03T18:21:01.483Z
uhbif19
<p>Plutus provides arithmetic functions for Integers through the modules <code>PlutusTx.Prelude</code> and <code>PlutusTx.Builtins</code>. Are there floating-point arithmetic functions which can be used on chain? If not, why is it so?</p>

<plutus><on-chain>
Does Plutus support floating-point arithmetic for on-chain code?
# Does Plutus support floating-point arithmetic for on-chain code?

(Asked by: username_0 on 2021-10-25)

Plutus provides arithmetic functions for Integers through the modules `PlutusTx.Prelude` and `PlutusTx.Builtins`. Are there floating-point arithmetic functions which can be used on chain? If not, why is it so?

**Tags:** plutus, on-chain

---

## 3 answers

### Answer by username_1 on 2021-10-25. Score: 1

My opinion would be that:

Floating point operations are typically resource-consuming and expensive to compute.

I mean, even 1 ADA had to be made a million Lovelace in order for the possibility to have fractional amount of ADA.

I'd suggest you follow the same approach

* Let the onchain code deal with Integer and carry-out floating point operations off-chain.


---

### Answer by username_2 on 2023-02-14. Score: 1

I used before this for operate with some divisions between Integers and then to truncate to closes integer again.
https://playground.plutus.iohkdev.io/doc/haddock/plutus-tx/html/PlutusTx-Ratio.html#t:Rational
Like this:
number1 = 10  
number2 = 3  
result = TxRatio.truncate ( TxRatio.unsafeRatio number1 number2)   

I think its just too expensive to operate with them on chain, ill not recommend, but you can if you need it.


---

### Answer by username_3 on 2023-11-03. Score: 0

Floats are well known to be the wrong datatype for encoding anything resembling money, fixed points should be used for such purposes.

Encoding them by `Integer` (with some hardcoded number of fractional decimals) is prone to errors by mistake, so you better wrap it into custom `newtype`. That is actually what Plutus `Lovelace` datatype does.

I am not aware of implementation for full-featured `Decimal` datatype in Plutus, but they are usually can be avoided in smart-contract logic.


---

