Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

minimal-printf doesn't display decimals between -1 to 0 correctly #13783

Closed
Linguanghsou opened this issue Oct 19, 2020 · 3 comments · Fixed by #13548
Closed

minimal-printf doesn't display decimals between -1 to 0 correctly #13783

Linguanghsou opened this issue Oct 19, 2020 · 3 comments · Fixed by #13548

Comments

@Linguanghsou
Copy link

Linguanghsou commented Oct 19, 2020

Description of defect

The output of:

    double d = -0.00139;
    printf("d = %f\r\n",d);

with minimal-printf is:
d = 0.001391
but it should be:
d = -0.001391

with setting "target.printf_lib": "std" the output is as expected.

Target(s) affected by this defect ?

NUCLEO_F413ZH

Toolchain(s) (name and version) displaying this defect ?

gcc-arm-none-eabi-9-2019-q4-major

What version of Mbed-os are you using (tag or sha) ?

mbed-os-6.3.0

What version(s) of tools are you using. List all that apply (E.g. mbed-cli)

mbed-cli

How is this defect reproduced ?

double d = -0.00139;
printf("d = %f\r\n",d);
@ciarmcom
Copy link
Member

Thank you for raising this detailed GitHub issue. I am now notifying our internal issue triagers.
Internal Jira reference: https://jira.arm.com/browse/IOTOSM-2528

@helpfulchicken
Copy link

helpfulchicken commented Nov 4, 2020

This issue is also affecting me. I'm having to duplicate print calls for negative and positive cases to deal with this.

Target(s) affected by this defect ?
NRF52_DK

Toolchain(s) (name and version) displaying this defect ?
gcc-arm-none-eabi-9-2019-q4-major

What version of Mbed-os are you using (tag or sha) ?
mbed-os-6.4.0 (#ad40b1b2672d)

What version(s) of tools are you using. List all that apply (E.g. mbed-cli)
mbed-cli 1.10.4

Additional details
I looked into the source code, and I found where this is happening.
In the function "mbed_minimal_formatted_string_double", the integer part of the floating point value is take using this line:

MBED_SIGNED_STORAGE integer = value;

This works fine as long as negative values are less than or equal to -1.0.
Otherwise, for values between 0.0 and -1.0, the integer component is "-0", which loses the sign when being saved in a signed integer format.
Again, for the sake of clarity, writing a floating point value to a signed integer results in loss of sign information if the value in question is between 0.0 and 1.0.

Suggested solution
After this line:

MBED_SIGNED_STORAGE integer = value;

Insert this code:

/* write sign if integer part is zero and value is negative */
if ((value < 0.0) && (integer == 0))
{
    /* write sign */
    mbed_minimal_putchar(buffer, length, result, '-', stream);
}

That will add the sign character in this scenario, without interfering with the existing method of printing.

@0xc0170
Copy link
Contributor

0xc0170 commented Nov 4, 2020

Thanks @helpfulchicken . I commented in the #13784 (fix for this), if we should consider your fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants