This is the mail archive of the
newlib@sourceware.org
mailing list for the newlib project.
Re: [PATCH] Print sign of NaN values to nano-vfprintf.
- From: "Richard Earnshaw (lists)" <Richard dot Earnshaw at arm dot com>
- To: jon at beniston dot com, newlib at sourceware dot org
- Date: Wed, 12 Dec 2018 10:04:19 +0000
- Subject: Re: [PATCH] Print sign of NaN values to nano-vfprintf.
- References: <0b9101d49196$539d0bb0$fad72310$@beniston.com>
On 11/12/2018 21:13, jon@beniston.com wrote:
>> diff --git a/newlib/libc/stdio/nano-vfprintf_float.c
> b/newlib/libc/stdio/nano-vfprintf_float.c
>> index 98893e97b..071a09edc 100644
>> --- a/newlib/libc/stdio/nano-vfprintf_float.c
>> +++ b/newlib/libc/stdio/nano-vfprintf_float.c
>> @@ -213,6 +213,8 @@ _printf_float (struct _reent *data,
>> }
>> if (isnan (_fpvalue))
>> {
>> + if (_fpvalue < 0)
>> + pdata->l_buf[0] = '-';
>> if (code <= 'G') /* 'A', 'E', 'F', or 'G'. */
>> cp = "NAN";
>
> This patch doesn't work, as a comparison of a NaN with anything should
> always return false. As per the main printf code, this can be done by
> checking the sign bit instead:
>
> - if (_fpvalue < 0)
> + if (signbit (_fpvalue))
> pdata->l_buf[0] = '-';
>
> Patch attached.
>
That will work better for -0 as well. LGTM
R.
> Cheers,
> Jon
>
>
> 0001-nano-vfprintf_float.c-Fix-check-if-negative-for-nans.patch
>
> From 49593628a465af4e8409ed2aac142b9ed347156e Mon Sep 17 00:00:00 2001
> From: Jon Beniston <jon@beniston.com>
> Date: Tue, 11 Dec 2018 21:03:03 +0000
> Subject: [PATCH] nano-vfprintf_float.c: Fix check if negative for nans.
>
> ---
> newlib/libc/stdio/nano-vfprintf_float.c | 3 ++-
> 1 file changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/newlib/libc/stdio/nano-vfprintf_float.c b/newlib/libc/stdio/nano-vfprintf_float.c
> index 071a09edc..524f67a31 100644
> --- a/newlib/libc/stdio/nano-vfprintf_float.c
> +++ b/newlib/libc/stdio/nano-vfprintf_float.c
> @@ -39,6 +39,7 @@
> #include <string.h>
> #include <limits.h>
> #include <stdint.h>
> +#include <math.h>
> #include <wchar.h>
> #include <sys/lock.h>
> #include <stdarg.h>
> @@ -213,7 +214,7 @@ _printf_float (struct _reent *data,
> }
> if (isnan (_fpvalue))
> {
> - if (_fpvalue < 0)
> + if (signbit (_fpvalue))
> pdata->l_buf[0] = '-';
> if (code <= 'G') /* 'A', 'E', 'F', or 'G'. */
> cp = "NAN";
>