Good catch!

On 1/21/22, fft <f...@feedforward.com.cn> wrote:
> Hi team's
> I was troubled by a strange problem whole day,and at last,i found is about
> the nuttx math lib function float frexpf(float x, int *exponent),when the
> input x is postive, there is no problem,but when the input x is negative,the
> result is wrong! I tested with inpu x=60 and x=-60:
> x = 60;&nbsp; &nbsp;
> float sig = frexpf(x, &amp;e);
> sig = 0.9375
> e = 6;
>
> that's right!
> x = -60;&nbsp; &nbsp;
> float sig = frexpf(x, &amp;e);
> sig = -0
> e = 2147483647;
> that's wrong!
>
> I check online in stackoverflow,there's a recommended
> design:https://stackoverflow.com/questions/40416682/portable-way-to-serialize-float-as-32-bit-integer
> According to the recommended design, i change float frexpf(float x, int
> *exponent)
>
> from:
> float frexpf(float x, int *exponent)
> {
> &nbsp; *exponent = (int)ceilf(log2f(x));
> &nbsp; return x / ldexpf(1.0F, *exponent);
> }
> to:
> float frexpf(float x, int *exponent)
> {
> &nbsp; *exponent = (int)ceilf(log2f(fabsf(x)));
> &nbsp; return x / ldexpf(1.0F, *exponent);
> }
> and the problem is solved,i commit a pull request to solve the problem
> ,please help me to check is right or not, thanks!
> https://github.com/apache/incubator-nuttx/pull/5307
>
>
> Best regards,
> Zou

Reply via email to