5

Floating point arithmetic is nondeterministic, so blockchain network may not reach agreement on the state of the chain.

I am using the following code in my smart contract to calculate roots:

let mint_value = (juror_stake as f64).powf(0.8) as u128 + 1;

Are such codes allowed? If not how to do floating-point arithmetic.

Amiya Behera
  • 2,210
  • 19
  • 32

2 Answers2

3

Floating point should not be used to store and make computations for money because of rounding errors. (e.g. type .1 + .2 == .3 in your browser's console). I think that if it compiles, runs, and does what you intended, then you can use it. As long as you're storing and doing most calculations with tokens as u128.

Lucio M. Tato
  • 5,639
  • 2
  • 31
  • 30
2

In NEAR Wasm VM we support floating numbers. We use wasmer for current mainnet VM which has a special handling for non-deterministic behavior of floats. So you can use it if you need it.

Evgeny Kuzyakov
  • 1,078
  • 3
  • 5