From: Markus Armbruster <armbru@redhat.com>
To: Eric Blake <eblake@redhat.com>
Cc: David Hildenbrand <david@redhat.com>,
qemu-devel@nongnu.org, Paolo Bonzini <pbonzini@redhat.com>,
Michael Roth <mdroth@linux.vnet.ibm.com>
Subject: Re: [Qemu-devel] [PATCH v1 1/9] cutils: add qemu_strtod() and qemu_strtod_finite()
Date: Thu, 15 Nov 2018 17:22:58 +0100 [thread overview]
Message-ID: <874lcicntp.fsf@dusky.pond.sub.org> (raw)
In-Reply-To: <5e2769ed-f017-d684-da9e-115d31c3189b@redhat.com> (Eric Blake's message of "Thu, 15 Nov 2018 08:23:59 -0600")
Eric Blake <eblake@redhat.com> writes:
> On 11/15/18 8:04 AM, David Hildenbrand wrote:
>> Let's provide a wrapper for strtod().
>>
>> Signed-off-by: David Hildenbrand <david@redhat.com>
>> ---
>> include/qemu/cutils.h | 2 ++
>> util/cutils.c | 38 ++++++++++++++++++++++++++++++++++++++
>> 2 files changed, 40 insertions(+)
>>
>
>
>> +
>> +/**
>> + * Convert string @nptr to a finite double.
>> + *
>> + * Works like qemu_strtoul(), except it stores +/-HUGE_VAL on
>> + * overflow/underflow. "NaN" or "inf" are rejcted with -EINVAL.
>
> s/rejcted/rejected/
Also, just overflow. Floating-point underflow is when a computation's
mathematical result is too close to zero to be represented without
extraordinary rounding error.
Skip this paragraph unless you're ready to nerd out. IEEE 754 section
7.5 defines underflow to happen
[...] either
a) after rounding — when a non-zero result computed as though the
exponent range were unbounded would lie strictly between ±b^emin,
or
b) before rounding — when a non-zero result computed as though both
the exponent range and the precision were unbounded would lie
strictly between ±b^emin.
where b^emin is the smallest normal number.
The "Works like qemu_strtoul()" is a bit lazy. I guess it works like
qemu_strtoul() in the sense that it adds to strtod() what qemu_strtoul()
adds to strtoul(). I consciously didn't take a similar shortcut in
commit 4295f879bec: I documented both qemu_strtol() and qemu_strtoul()
in longhand, and used "Works like" shorthand only where that's actually
the case: qemu_strtoll() works like qemu_strtol(), and qemu_strtoull()
works like qemu_strtoul(). I'd prefer longhand for qemu_strtod(). It
costs us a few lines, but it results in a clearer contract.
>> + */
>> +int qemu_strtod_finite(const char *nptr, const char **endptr, double *result)
>> +{
>> + int ret = qemu_strtod(nptr, endptr, result);
>
> On overflow, result is set to HUGE_VAL (aka "inf") with ret set to
> -ERANGE. (The C standard uses HUGE_VAL rather than directly requiring
> infinity on overflow, in order to cater to museum platforms where the
> largest representable double is still finite; but no one develops qemu
> on a non-IEEE machine these days so we know that HUGE_VAL == INF).
Aside: museum clauses like this one make the standard much harder to
read than necessary. I wish they'll purge them from C2X.
>> +
>> + if (!ret && !isfinite(*result)) {
>> + return -EINVAL;
>> + }
qemu_strtol() & friends leave *result alone when they return -EINVAL.
This one doesn't. Unlikely to hurt anyone, but I'd prefer to keep them
consistent.
> This check means that overflow ("1e9999") fails with -ERANGE, while
> actual infinity ("inf") fails with -EINVAL, letting the user
> distinguish between the two. Still, I wonder if assigning a
> non-finite value into result on -ERANGE is the wisest course of
> action. We'll just have to see in the next patches that use this.
I guess it's about as "wise" as qemu_strtol() storing LONG_MAX on
integer overflow.
I'm fine with the semantics David picked, as long as they're spelled out
in the function contract.
> With the typo fix,
>
> Reviewed-by: Eric Blake <eblake@redhat.com>
next prev parent reply other threads:[~2018-11-15 16:23 UTC|newest]
Thread overview: 34+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-11-15 14:04 [Qemu-devel] [PATCH v1 0/9] qapi: rewrite string-input-visitor David Hildenbrand
2018-11-15 14:04 ` [Qemu-devel] [PATCH v1 1/9] cutils: add qemu_strtod() and qemu_strtod_finite() David Hildenbrand
2018-11-15 14:23 ` Eric Blake
2018-11-15 16:22 ` Markus Armbruster [this message]
2018-11-15 17:25 ` David Hildenbrand
2018-11-15 18:02 ` Eric Blake
2018-11-15 21:57 ` David Hildenbrand
2018-11-15 14:04 ` [Qemu-devel] [PATCH v1 2/9] cutils: use qemu_strtod_finite() in do_strtosz() David Hildenbrand
2018-11-15 14:36 ` Eric Blake
2018-11-15 16:41 ` Markus Armbruster
2018-11-15 17:59 ` David Hildenbrand
2018-11-15 14:04 ` [Qemu-devel] [PATCH v1 3/9] qapi: use qemu_strtod_finite() in string-input-visitor David Hildenbrand
2018-11-15 14:37 ` Eric Blake
2018-11-15 14:39 ` David Hildenbrand
2018-11-15 16:48 ` Markus Armbruster
2018-11-15 21:54 ` David Hildenbrand
2018-11-15 14:04 ` [Qemu-devel] [PATCH v1 4/9] qapi: use qemu_strtod_finite() in qobject-input-visitor David Hildenbrand
2018-11-15 14:45 ` Eric Blake
2018-11-16 14:46 ` Markus Armbruster
2018-11-15 14:04 ` [Qemu-devel] [PATCH v1 5/9] test-string-input-visitor: add more tests David Hildenbrand
2018-11-15 17:13 ` Eric Blake
2018-11-15 17:32 ` David Hildenbrand
2018-11-15 18:46 ` Markus Armbruster
2018-11-15 14:04 ` [Qemu-devel] [PATCH v1 6/9] qapi: rewrite string-input-visitor David Hildenbrand
2018-11-16 10:10 ` Markus Armbruster
2018-11-19 14:12 ` David Hildenbrand
2018-11-19 19:51 ` Markus Armbruster
2018-11-19 21:22 ` David Hildenbrand
2018-11-15 14:04 ` [Qemu-devel] [PATCH v1 7/9] test-string-input-visitor: use virtual walk David Hildenbrand
2018-11-16 14:48 ` Markus Armbruster
2018-11-15 14:05 ` [Qemu-devel] [PATCH v1 8/9] test-string-input-visitor: split off uint64 list tests David Hildenbrand
2018-11-16 14:51 ` Markus Armbruster
2018-11-15 14:05 ` [Qemu-devel] [PATCH v1 9/9] test-string-input-visitor: add range overflow tests David Hildenbrand
2018-11-16 14:51 ` Markus Armbruster
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=874lcicntp.fsf@dusky.pond.sub.org \
--to=armbru@redhat.com \
--cc=david@redhat.com \
--cc=eblake@redhat.com \
--cc=mdroth@linux.vnet.ibm.com \
--cc=pbonzini@redhat.com \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.