XRPL-Labs / xrpld-hooks

ISC License
95 stars 28 forks source link

float_divide and float_multiply gives different results than the actual #21

Closed chalith closed 1 year ago

chalith commented 3 years ago

Issue Description

When the float_divide or float_multiply is used in calculations, the resulting value is invalid.

Steps to Reproduce

In the hooks contract I used:

// Result should be 12 / 1 = 12
trace_float(SBUF("Division - "), float_divide(float_set(0, 12), float_set(0, 1)));

// Result should be 12 * 1 = 12
trace_float(SBUF("Multiplication - "), float_multiply(float_set(0, 12), float_set(0, 1)));

Expected Result

  1. For the division, result should be 12 / 1 = 12
  2. For the multiplication, result should be 12 * 1 = 12

Actual Result

Division - Float 1199999999999999*10^(-14) Multiplication - Float 1199999999720603*10^(-14)

Workaround

We used this as a workaround.

float f = (float)5 / (float)2;
int64_t res = f * 1E10;
trace_float(SBUF("Division - "), float_set(-10, res));

Took the pure c float division first. Then multiplied the result with 10^10. Then gave that (result * 10^10) as the mantissa and -10 as exponent to the float_set

Environment

Ubuntu 18.04 rippled version 1.6.0-hooks hooks-chaining branch

RichardAH commented 1 year ago

This is fixed as of the most recent version on v2